001//
002// MIT License
003//
004// Copyright (c) 2021 Alexander Söderberg & Contributors
005//
006// Permission is hereby granted, free of charge, to any person obtaining a copy
007// of this software and associated documentation files (the "Software"), to deal
008// in the Software without restriction, including without limitation the rights
009// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010// copies of the Software, and to permit persons to whom the Software is
011// furnished to do so, subject to the following conditions:
012//
013// The above copyright notice and this permission notice shall be included in all
014// copies or substantial portions of the Software.
015//
016// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
022// SOFTWARE.
023//
024package cloud.commandframework.jda;
025
026import net.dv8tion.jda.api.entities.ChannelType;
027import net.dv8tion.jda.api.entities.MessageChannel;
028import net.dv8tion.jda.api.entities.User;
029import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
030import org.checkerframework.checker.nullness.qual.NonNull;
031import org.checkerframework.checker.nullness.qual.Nullable;
032
033import java.util.Objects;
034import java.util.Optional;
035
036/**
037 * Wrapper for {@link MessageReceivedEvent}
038 */
039public class JDACommandSender {
040
041    private final MessageReceivedEvent event;
042    private final User user;
043    private final MessageChannel channel;
044
045    /**
046     * Construct a JDA Command Sender
047     *
048     * @param event   Message received event
049     * @param user    Sending user
050     * @param channel Channel sent in
051     */
052    protected JDACommandSender(
053            final @Nullable MessageReceivedEvent event,
054            final @NonNull User user,
055            final @NonNull MessageChannel channel
056    ) {
057        this.event = event;
058        this.user = user;
059        this.channel = channel;
060    }
061
062    /**
063     * Create a JDA Command Sender from a {@link MessageReceivedEvent}
064     *
065     * @param event Message Received Event
066     * @return Constructed JDA Command Sender
067     */
068    public static @NonNull JDACommandSender of(final @NonNull MessageReceivedEvent event) {
069        if (event.isWebhookMessage()) {
070            return new JDACommandSender(event, event.getAuthor(), event.getChannel());
071        } else if (event.isFromType(ChannelType.PRIVATE)) {
072            return new JDAPrivateSender(event, event.getAuthor(), event.getPrivateChannel());
073        }
074
075        return new JDAGuildSender(event, Objects.requireNonNull(event.getMember()), event.getTextChannel());
076    }
077
078    /**
079     * Get the message receive event
080     *
081     * @return Optional of the message receive event
082     * @since 1.1.0
083     */
084    public final @NonNull Optional<MessageReceivedEvent> getEvent() {
085        return Optional.ofNullable(this.event);
086    }
087
088    /**
089     * Get the user the command sender represents
090     *
091     * @return User that sent the message
092     * @since 1.1.0
093     */
094    public final @NonNull User getUser() {
095        return this.user;
096    }
097
098    /**
099     * Get the channel the user sent the message in
100     *
101     * @return Channel that the message was sent in
102     */
103    public final @NonNull MessageChannel getChannel() {
104        return this.channel;
105    }
106
107}