Search in sources :

Example 1 with GuildMessageReactionAddEvent

use of net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent in project Gary by help-chat.

the class EventUtils method pullReaction.

public static Future<MessageReaction> pullReaction(Message message, User user) {
    JDA jda = message.getJDA();
    CompletableFuture<MessageReaction> future = new CompletableFuture<>();
    GuildMessageReactionAddEvent e = (GuildMessageReactionAddEvent) pullEvent(EventsEnum.MESSAGE_REACTION_ADD, jda);
    while (message.getChannel().retrieveMessageById(e.getMessageId()).complete() != message || e.getUser() != user) {
        e = (GuildMessageReactionAddEvent) pullEvent(EventsEnum.MESSAGE_REACTION_ADD, jda);
    }
    future.complete(e.getReaction());
    return future;
}
Also used : MessageReaction(net.dv8tion.jda.api.entities.MessageReaction) CompletableFuture(java.util.concurrent.CompletableFuture) JDA(net.dv8tion.jda.api.JDA) GuildMessageReactionAddEvent(net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent)

Example 2 with GuildMessageReactionAddEvent

use of net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent in project Gary by help-chat.

the class RMSReactionHandler method execute.

@Override
protected void execute(GenericEvent event) {
    GuildMessageReactionAddEvent e = (GuildMessageReactionAddEvent) event;
    if (e.getChannel().getName().equalsIgnoreCase("rate-my-server") && !e.getUser().isBot()) {
        Message message = e.getChannel().retrieveMessageById(e.getMessageId()).complete();
        List<MessageReaction> reactions = new ArrayList<>(message.getReactions());
        // this exception will never happen, so can be ignored
        // noinspection OptionalGetWithoutIsPresent
        reactions.remove(reactions.stream().filter(r -> r.toString().equals(e.getReaction().toString())).findFirst().get());
        if (reactions.stream().anyMatch(d -> d.retrieveUsers().complete().contains(e.getUser()))) {
            e.getReaction().removeReaction(e.getUser()).queue();
        }
    }
}
Also used : GEvent(me.piggypiglet.gary.core.handlers.GEvent) Message(net.dv8tion.jda.api.entities.Message) List(java.util.List) GuildMessageReactionAddEvent(net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent) EventsEnum(me.piggypiglet.gary.core.objects.enums.EventsEnum) MessageReaction(net.dv8tion.jda.api.entities.MessageReaction) GenericEvent(net.dv8tion.jda.api.events.GenericEvent) ArrayList(java.util.ArrayList) MessageReaction(net.dv8tion.jda.api.entities.MessageReaction) Message(net.dv8tion.jda.api.entities.Message) GuildMessageReactionAddEvent(net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent) ArrayList(java.util.ArrayList)

Example 3 with GuildMessageReactionAddEvent

use of net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent in project botdarr by shayaantx.

the class DiscordBootstrap method init.

@Override
public void init() throws Exception {
    try {
        ChatClientBootstrap.ApisAndCommandConfig config = buildConfig();
        ChatClientResponseBuilder<DiscordResponse> responseBuilder = new DiscordResponseBuilder();
        JDA jda = JDABuilder.createDefault((Config.getProperty(Config.Constants.DISCORD_TOKEN))).addEventListeners(new ListenerAdapter() {

            @Override
            public void onGenericEvent(@Nonnull GenericEvent event) {
                super.onGenericEvent(event);
            }

            @Override
            public void onReady(@Nonnull ReadyEvent event) {
                LogManager.getLogger("com.botdarr.clients.discord").info("Connected to discord");
                ChatClient<DiscordResponse> chatClient = new DiscordChatClient(event.getJDA());
                // start the scheduler threads that send notifications and cache data periodically
                initScheduling(chatClient, responseBuilder, config.getApis());
                super.onReady(event);
            }

            @Override
            public void onGuildMessageReactionAdd(@Nonnull GuildMessageReactionAddEvent event) {
                if (event.getReactionEmote().getName().equalsIgnoreCase(THUMBS_UP_EMOTE)) {
                    MessageHistory.MessageRetrieveAction me = event.getChannel().getHistoryAround(event.getMessageId(), 1);
                    me.queue(messageHistory -> {
                        List<Message> messageHistories = messageHistory.getRetrievedHistory();
                        messageLoop: for (Message message : messageHistories) {
                            List<MessageEmbed> embeds = message.getEmbeds();
                            for (MessageEmbed.Field field : embeds.get(0).getFields()) {
                                String name = field.getName();
                                if (Strings.isEmpty(name)) {
                                    continue;
                                }
                                if (name.equals(ADD_MOVIE_COMMAND_FIELD_PREFIX) || name.equals(ADD_SHOW_COMMAND_FIELD_PREFIX) || name.equals(ADD_ARTIST_COMMAND_FIELD_PREFIX)) {
                                    // capture/process the command
                                    handleCommand(event.getJDA(), field.getValue(), event.getUser().getName(), event.getChannel().getName());
                                    break messageLoop;
                                }
                            }
                        }
                    });
                }
                super.onGuildMessageReactionAdd(event);
            }

            @Override
            public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
                handleCommand(event.getJDA(), event.getMessage().getContentStripped(), event.getAuthor().getName(), event.getChannel().getName());
                LogManager.getLogger("com.botdarr.clients.discord").debug(event.getMessage().getContentRaw());
                super.onMessageReceived(event);
            }

            private void handleCommand(JDA jda, String message, String author, String channelName) {
                // build chat client
                DiscordChatClient discordChatClient = new DiscordChatClient(jda);
                // capture/process command
                Scheduler.getScheduler().executeCommand(() -> {
                    runAndProcessCommands(message, author, responseBuilder, chatClientResponse -> {
                        discordChatClient.sendMessage(chatClientResponse, channelName);
                    });
                    return null;
                });
            }

            private static final String THUMBS_UP_EMOTE = "\uD83D\uDC4D";
        }).build();
        jda.awaitReady();
    } catch (Throwable e) {
        LogManager.getLogger("com.botdarr.clients.discord").error("Error caught during main", e);
        throw e;
    }
}
Also used : Message(net.dv8tion.jda.api.entities.Message) Nonnull(javax.annotation.Nonnull) JDA(net.dv8tion.jda.api.JDA) GuildMessageReactionAddEvent(net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent) ChatClientBootstrap(com.botdarr.clients.ChatClientBootstrap) MessageReceivedEvent(net.dv8tion.jda.api.events.message.MessageReceivedEvent) ListenerAdapter(net.dv8tion.jda.api.hooks.ListenerAdapter) MessageHistory(net.dv8tion.jda.api.entities.MessageHistory) ReadyEvent(net.dv8tion.jda.api.events.ReadyEvent) GenericEvent(net.dv8tion.jda.api.events.GenericEvent) List(java.util.List)

Example 4 with GuildMessageReactionAddEvent

use of net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent in project pokeraidbot by magnusmickelsson.

the class EventLoggingListener method onEvent.

@Override
public void onEvent(GenericEvent event) {
    if (LOGGER.isTraceEnabled()) {
        if (event instanceof GuildMessageReactionAddEvent) {
            final GuildMessageReactionAddEvent reactionAddEvent = (GuildMessageReactionAddEvent) event;
            LOGGER.trace("Reaction: " + reactionAddEvent.getUser() + " - " + reactionAddEvent.getReaction() + " - " + reactionAddEvent.getReactionEmote());
        } else if (event instanceof GuildMessageReceivedEvent) {
            final GuildMessageReceivedEvent guildMessageReceivedEvent = (GuildMessageReceivedEvent) event;
            LOGGER.trace("Message from " + guildMessageReceivedEvent.getAuthor() + ": " + guildMessageReceivedEvent.getMessage());
        } else if (event instanceof GuildMessageEmbedEvent) {
            final GuildMessageEmbedEvent guildMessageReceivedEvent = (GuildMessageEmbedEvent) event;
            final List<MessageEmbed> messageEmbeds = guildMessageReceivedEvent.getMessageEmbeds();
            for (MessageEmbed embed : messageEmbeds) LOGGER.trace("Embed message from " + embed.getAuthor() + ": " + String.valueOf(embed.getTitle()) + " - " + String.valueOf(embed.getDescription()));
        }
    }
}
Also used : MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) GuildMessageReactionAddEvent(net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent) GuildMessageEmbedEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageEmbedEvent) GuildMessageReceivedEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent)

Example 5 with GuildMessageReactionAddEvent

use of net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent in project pokeraidbot by magnusmickelsson.

the class EmoticonSignUpMessageListener method onEvent.

@Override
public void onEvent(GenericEvent event) {
    String reactionMessageId = null;
    User user = null;
    if (emoteMessageId == null && infoMessageId == null) {
        LOGGER.trace("This listener haven't received a emote message id or info message id yet.");
        return;
    }
    try {
        if (event instanceof GuildMessageReactionAddEvent) {
            final GuildMessageReactionAddEvent reactionEvent = (GuildMessageReactionAddEvent) event;
            user = reactionEvent.getUser();
            // If the bot added any reactions, don't respond to them
            if (user.isBot())
                return;
            reactionMessageId = reactionEvent.getReaction().getMessageId();
            if (emoteMessageId == null || !emoteMessageId.equals(reactionMessageId)) {
                return;
            }
            // If this is a reaction for a user that just triggered an error with his/her reaction, skip it
            if (user.getName().equals(userHadError)) {
                userHadError = null;
                return;
            }
            final MessageReaction.ReactionEmote emote = reactionEvent.getReaction().getReactionEmote();
            if (emote != null) {
                switch(emote.getName()) {
                    case Emotes.ONE:
                        addToSignUp(user, 0, 0, 0, 1);
                        break;
                    case Emotes.TWO:
                        addToSignUp(user, 0, 0, 0, 2);
                        break;
                    case Emotes.THREE:
                        addToSignUp(user, 0, 0, 0, 3);
                        break;
                    case Emotes.FOUR:
                        addToSignUp(user, 0, 0, 0, 4);
                        break;
                    case Emotes.FIVE:
                        addToSignUp(user, 0, 0, 0, 5);
                        break;
                    default:
                }
            }
        } else if (event instanceof GuildMessageReactionRemoveEvent) {
            final GuildMessageReactionRemoveEvent reactionEvent = (GuildMessageReactionRemoveEvent) event;
            // If the bot added any reactions, don't respond to them
            user = reactionEvent.getUser();
            if (user.isBot())
                return;
            // If this is a reaction for a user that just triggered an error with his/her reaction, skip it
            if (user.getName().equals(userHadError)) {
                userHadError = null;
                return;
            }
            reactionMessageId = reactionEvent.getReaction().getMessageId();
            if (!emoteMessageId.equals(reactionMessageId)) {
                return;
            }
            final MessageReaction.ReactionEmote emote = reactionEvent.getReaction().getReactionEmote();
            if (emote != null) {
                switch(emote.getName()) {
                    case Emotes.ONE:
                        removeFromSignUp(user, 0, 0, 0, 1);
                        break;
                    case Emotes.TWO:
                        removeFromSignUp(user, 0, 0, 0, 2);
                        break;
                    case Emotes.THREE:
                        removeFromSignUp(user, 0, 0, 0, 3);
                        break;
                    case Emotes.FOUR:
                        removeFromSignUp(user, 0, 0, 0, 4);
                        break;
                    case Emotes.FIVE:
                        removeFromSignUp(user, 0, 0, 0, 5);
                        break;
                    default:
                }
            }
        }
    } catch (Throwable t) {
        if (event instanceof GenericGuildMessageReactionEvent) {
            final GenericGuildMessageReactionEvent guildMessageReactionEvent = (GenericGuildMessageReactionEvent) event;
            reactionMessageId = guildMessageReactionEvent.getReaction().getMessageId();
            if (emoteMessageId == null) {
                LOGGER.warn("Emote message ID = null, this should get cleaned up!" + " Event: " + printInfoAbout(event));
                return;
            }
            if (!emoteMessageId.equals(reactionMessageId)) {
                LOGGER.warn("We got a guild reaction event throwing exception, but not one we were listening for!" + " Event: " + printInfoAbout(event));
                return;
            }
            // Since we got an error, remove last reaction
            if (reactionMessageId != null && reactionMessageId.equals(emoteMessageId)) {
                // Do this with a slight delay to prevent graphical glitches client side.
                guildMessageReactionEvent.getReaction().removeReaction(user).queueAfter(30, TimeUnit.MILLISECONDS);
                userHadError = user.getName();
            }
            if (user != null && t.getMessage() != null) {
                MessageBuilder messageBuilder = new MessageBuilder();
                if (!t.getMessage().contains(user.getAsMention())) {
                    messageBuilder.append(user.getAsMention()).append(": ");
                }
                messageBuilder.append(t.getMessage());
                guildMessageReactionEvent.getChannel().sendMessage(messageBuilder.build()).queue();
            } else {
                LOGGER.warn("We have a situation where user " + user + " or exception (of type " + t.getClass().getSimpleName() + ") message is null! Event: " + printInfoAbout(event));
            }
        } else {
            LOGGER.warn("Exception in event listener! Event: " + printInfoAbout(event));
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Stacktrace:", t);
            }
        }
    }
}
Also used : MessageReaction(net.dv8tion.jda.api.entities.MessageReaction) User(net.dv8tion.jda.api.entities.User) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) GuildMessageReactionRemoveEvent(net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionRemoveEvent) GuildMessageReactionAddEvent(net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent) GenericGuildMessageReactionEvent(net.dv8tion.jda.api.events.message.guild.react.GenericGuildMessageReactionEvent)

Aggregations

GuildMessageReactionAddEvent (net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent)8 Message (net.dv8tion.jda.api.entities.Message)4 MessageReaction (net.dv8tion.jda.api.entities.MessageReaction)4 List (java.util.List)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 JDA (net.dv8tion.jda.api.JDA)2 MessageBuilder (net.dv8tion.jda.api.MessageBuilder)2 MessageEmbed (net.dv8tion.jda.api.entities.MessageEmbed)2 GenericEvent (net.dv8tion.jda.api.events.GenericEvent)2 GuildMessageReactionRemoveEvent (net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionRemoveEvent)2 NotNull (org.jetbrains.annotations.NotNull)2 WebhookEmbed (club.minnced.discord.webhook.send.WebhookEmbed)1 WebhookEmbedBuilder (club.minnced.discord.webhook.send.WebhookEmbedBuilder)1 WebhookMessage (club.minnced.discord.webhook.send.WebhookMessage)1 WebhookMessageBuilder (club.minnced.discord.webhook.send.WebhookMessageBuilder)1 ChatClientBootstrap (com.botdarr.clients.ChatClientBootstrap)1 EmbedCache (com.github.kaktushose.jda.commands.embeds.EmbedCache)1 ErrorCategory (com.mongodb.ErrorCategory)1 MongoWriteException (com.mongodb.MongoWriteException)1 com.mongodb.client.model (com.mongodb.client.model)1