Skip to content

Commit

Permalink
Move button listener to dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Jun 4, 2021
1 parent 4c928b3 commit e8ca07b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
Expand Up @@ -8,12 +8,14 @@
import com.google.common.util.concurrent.MoreExecutors;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.ShutdownEvent;
import net.dv8tion.jda.api.events.interaction.ButtonClickEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageDeleteEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
Expand All @@ -28,12 +30,13 @@ public class Dispatcher extends ListenerAdapter
private final Set<Command> commands = ConcurrentHashMap.newKeySet();
private final ExecutorService pool = Executors.newCachedThreadPool(MiscUtils.newThreadFactory("command-runner", false));
private final ReactionListenerRegistry reactListReg = new ReactionListenerRegistry();
private final ButtonListener buttonListener = new ButtonListener();

public Dispatcher()
{
this.registerCommand(new BuildGradleCommand());
this.registerCommand(new ChangelogCommand());
this.registerCommand(new DocsCommand(this.reactListReg));
this.registerCommand(new DocsCommand(this.reactListReg, this.buttonListener));
this.registerCommand(new EvalCommand());
this.registerCommand(new GradleCommand());
this.registerCommand(new GradleProjectCommand());
Expand All @@ -58,6 +61,11 @@ public Set<Command> getCommands()
return Collections.unmodifiableSet(new HashSet<>(this.commands));
}

@Override
public void onButtonClick(@NotNull ButtonClickEvent event) {
buttonListener.onEvent(event);
}

@Override
public void onGuildMessageReceived(final GuildMessageReceivedEvent event)
{
Expand Down
Expand Up @@ -27,12 +27,12 @@ public class DocsCommand extends ReactionCommand
{
private static final int RESULTS_PER_PAGE = 5;
private static final String[] ALIASES = new String[]{"documentation", "doc", "jdoc", "jdocs"};
private final ButtonListener buttons = new ButtonListener();
private boolean first = true;
private final ButtonListener buttons;

public DocsCommand(ReactionListenerRegistry registry)
public DocsCommand(ReactionListenerRegistry registry, ButtonListener buttons)
{
super(registry);
this.buttons = buttons;
}

private static Message getDocMessage(String jDocBase, Documentation documentation)
Expand Down Expand Up @@ -143,12 +143,6 @@ private static EmbedBuilder getDefaultEmbed()
@Override
public void dispatch(final User sender, final TextChannel channel, final Message message, final String content, final GuildMessageReceivedEvent event)
{
if (first)
{
sender.getJDA().addEventListener(buttons);
first = false;
}

if (content.trim().isEmpty())
{
reply(event, "See the docs here: " + JDocUtil.JDOCBASE);
Expand Down

0 comments on commit e8ca07b

Please sign in to comment.