From c370b5420c7254da61e115d476e0ecb06c8f0c38 Mon Sep 17 00:00:00 2001 From: Tais993 Date: Wed, 12 Jan 2022 09:32:11 +0100 Subject: [PATCH 1/3] Added event listener support for (slash) commands They get registered on creation of a CommandSystem instance, this means the ReadyEvent triggers too --- .../tjbot/commands/SlashCommand.java | 3 +- .../tjbot/commands/SlashCommandAdapter.java | 3 +- .../tjbot/commands/free/FreeCommand.java | 50 +++++++++---------- .../tjbot/commands/system/CommandSystem.java | 26 ++++++---- 4 files changed, 43 insertions(+), 39 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java index c8644c1147..42dbc1c7a1 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java @@ -5,6 +5,7 @@ import net.dv8tion.jda.api.events.interaction.ButtonClickEvent; import net.dv8tion.jda.api.events.interaction.SelectionMenuEvent; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.hooks.EventListener; import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.components.ButtonStyle; import net.dv8tion.jda.api.interactions.components.ComponentInteraction; @@ -36,7 +37,7 @@ *

* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}. */ -public interface SlashCommand { +public interface SlashCommand extends EventListener { /** * Gets the name of the command. diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java index 9b461b50fe..6bde9b319f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java @@ -4,6 +4,7 @@ import net.dv8tion.jda.api.events.interaction.ButtonClickEvent; import net.dv8tion.jda.api.events.interaction.SelectionMenuEvent; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.interactions.commands.build.CommandData; import org.jetbrains.annotations.NotNull; import org.togetherjava.tjbot.commands.componentids.ComponentId; @@ -56,7 +57,7 @@ *

* and registration of an instance of that class in {@link Commands}. */ -public abstract class SlashCommandAdapter implements SlashCommand { +public abstract class SlashCommandAdapter extends ListenerAdapter implements SlashCommand { private final String name; private final String description; private final SlashCommandVisibility visibility; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/free/FreeCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/free/FreeCommand.java index 707b3e24c8..55ed925f42 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/free/FreeCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/free/FreeCommand.java @@ -6,11 +6,9 @@ import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.TextChannel; -import net.dv8tion.jda.api.events.GenericEvent; import net.dv8tion.jda.api.events.ReadyEvent; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; -import net.dv8tion.jda.api.hooks.EventListener; import net.dv8tion.jda.api.requests.RestAction; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; @@ -54,7 +52,7 @@ * channel may be one of the monitored channels however it is recommended that a different channel * is used. */ -public final class FreeCommand extends SlashCommandAdapter implements EventListener { +public final class FreeCommand extends SlashCommandAdapter { private static final Logger logger = LoggerFactory.getLogger(FreeCommand.class); private static final String STATUS_TITLE = "**__CHANNEL STATUS__**\n\n"; @@ -122,7 +120,7 @@ public void onReady(@NotNull final ReadyEvent event) { *

* If this is called on from a channel that was not configured for monitoring (see * {@link FreeCommandConfig}) the user will receive an ephemeral message stating such. - * + * * @param event the event that triggered this * @throws IllegalStateException if this method is called for a Global Slash Command */ @@ -285,31 +283,31 @@ private void checkBusyStatusAllChannels(@NotNull JDA jda) { * * @param event the generic event that includes the 'onGuildMessageReceived'. */ + + @Override - public void onEvent(@NotNull GenericEvent event) { - if (event instanceof GuildMessageReceivedEvent guildEvent) { - if (guildEvent.isWebhookMessage() || guildEvent.getAuthor().isBot()) { - return; - } - if (!channelMonitor.isMonitoringChannel(guildEvent.getChannel().getIdLong())) { - logger.debug( - "Channel is not being monitored, ignoring message received in {} from {}", - guildEvent.getChannel().getName(), guildEvent.getAuthor()); - return; - } - if (channelMonitor.isChannelBusy(guildEvent.getChannel().getIdLong())) { - logger.debug( - "Channel status is currently busy, ignoring message received in {} from {}", - guildEvent.getChannel().getName(), guildEvent.getAuthor()); - return; - } - channelMonitor.setChannelBusy(guildEvent.getChannel().getIdLong(), - guildEvent.getAuthor().getIdLong()); - displayStatus(channelMonitor.getStatusChannelFor(guildEvent.getGuild())); - guildEvent.getMessage().reply(UserStrings.NEW_QUESTION.message()).queue(); + public void onGuildMessageReceived(@NotNull GuildMessageReceivedEvent event) { + if (event.isWebhookMessage() || event.getAuthor().isBot()) { + return; } + if (!channelMonitor.isMonitoringChannel(event.getChannel().getIdLong())) { + logger.debug("Channel is not being monitored, ignoring message received in {} from {}", + event.getChannel().getName(), event.getAuthor()); + return; + } + if (channelMonitor.isChannelBusy(event.getChannel().getIdLong())) { + logger.debug( + "Channel status is currently busy, ignoring message received in {} from {}", + event.getChannel().getName(), event.getAuthor()); + return; + } + channelMonitor.setChannelBusy(event.getChannel().getIdLong(), + event.getAuthor().getIdLong()); + displayStatus(channelMonitor.getStatusChannelFor(event.getGuild())); + event.getMessage().reply(UserStrings.NEW_QUESTION.message()).queue(); } + private @NotNull Optional getStatusMessageIn(@NotNull TextChannel channel) { if (!channelIdToMessageIdForStatus.containsKey(channel.getIdLong())) { return findExistingStatusMessage(channel); @@ -364,4 +362,4 @@ private void initStatusMessageChannels(@NotNull final JDA jda) { } return channel; } -} +} \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java index 4be65e20ae..860a566c45 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java @@ -74,13 +74,15 @@ public CommandSystem(@NotNull JDA jda, @NotNull Database database) { componentIdStore = new ComponentIdStore(database); componentIdStore.addComponentIdRemovedListener(CommandSystem::onComponentIdRemoved); componentIdParser = uuid -> componentIdStore.get(UUID.fromString(uuid)); - nameToSlashCommands.values() - .forEach(slashCommand -> slashCommand - .acceptComponentIdGenerator(((componentId, lifespan) -> { - UUID uuid = UUID.randomUUID(); - componentIdStore.putOrThrow(uuid, componentId, lifespan); - return uuid.toString(); - }))); + nameToSlashCommands.values().forEach(slashCommand -> { + slashCommand.acceptComponentIdGenerator(((componentId, lifespan) -> { + UUID uuid = UUID.randomUUID(); + componentIdStore.putOrThrow(uuid, componentId, lifespan); + return uuid.toString(); + })); + + jda.addEventListener(slashCommand); + }); if (logger.isInfoEnabled()) { logger.info("Available commands: {}", nameToSlashCommands.keySet()); @@ -99,10 +101,11 @@ public CommandSystem(@NotNull JDA jda, @NotNull Database database) { @Override public void onReady(@NotNull ReadyEvent event) { + JDA jda = event.getJDA(); + // Register reload on all guilds logger.debug("JDA is ready, registering reload command"); - event.getJDA() - .getGuildCache() + jda.getGuildCache() .forEach(guild -> COMMAND_SERVICE.execute(() -> registerReloadCommand(guild))); // NOTE We do not have to wait for reload to complete for the command system to be ready // itself @@ -110,8 +113,9 @@ public void onReady(@NotNull ReadyEvent event) { // Propagate the onReady event to all commands // NOTE 'registerReloadCommands' will not be finished running, this does not wait for it - nameToSlashCommands.values() - .forEach(command -> COMMAND_SERVICE.execute(() -> command.onReady(event))); + nameToSlashCommands.values().forEach(command -> { + COMMAND_SERVICE.execute(() -> command.onReady(event)); + }); } @Override From ab3709fa124502cae4b68b5f698cc0aa351120c5 Mon Sep 17 00:00:00 2001 From: Tais993 Date: Mon, 17 Jan 2022 13:31:00 +0100 Subject: [PATCH 2/3] Ran Spotless --- .../java/org/togetherjava/tjbot/commands/free/FreeCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/free/FreeCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/free/FreeCommand.java index 55ed925f42..984578b4bd 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/free/FreeCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/free/FreeCommand.java @@ -362,4 +362,4 @@ private void initStatusMessageChannels(@NotNull final JDA jda) { } return channel; } -} \ No newline at end of file +} From 6f11950409bef2ed2fccdb0e4b67da4be1e6145a Mon Sep 17 00:00:00 2001 From: Tais993 <49957334+Tais993@users.noreply.github.com> Date: Mon, 17 Jan 2022 04:46:17 -0800 Subject: [PATCH 3/3] Sonarcloud fix? --- .../togetherjava/tjbot/commands/system/CommandSystem.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java index 860a566c45..c9fc408b8f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java @@ -113,9 +113,8 @@ public void onReady(@NotNull ReadyEvent event) { // Propagate the onReady event to all commands // NOTE 'registerReloadCommands' will not be finished running, this does not wait for it - nameToSlashCommands.values().forEach(command -> { - COMMAND_SERVICE.execute(() -> command.onReady(event)); - }); + nameToSlashCommands.values() + .forEach(command -> COMMAND_SERVICE.execute(() -> command.onReady(event))); } @Override