Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,7 +37,7 @@
* <p>
* 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,7 +57,7 @@
* <p>
* 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -122,7 +120,7 @@ public void onReady(@NotNull final ReadyEvent event) {
* <p>
* 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
*/
Expand Down Expand Up @@ -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<Message> getStatusMessageIn(@NotNull TextChannel channel) {
if (!channelIdToMessageIdForStatus.containsKey(channel.getIdLong())) {
return findExistingStatusMessage(channel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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
Expand Down