/
ButtonListener.java
38 lines (31 loc) · 1.24 KB
/
ButtonListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.almightyalpaca.discord.jdabutler.commands;
import com.almightyalpaca.discord.jdabutler.util.FixedSizeCache;
import net.dv8tion.jda.api.events.GenericEvent;
import net.dv8tion.jda.api.events.interaction.ButtonClickEvent;
import net.dv8tion.jda.api.hooks.EventListener;
import net.dv8tion.jda.api.interactions.components.ButtonInteraction;
import java.util.function.Consumer;
public class ButtonListener implements EventListener {
private final FixedSizeCache<String, Consumer<? super ButtonInteraction>> listeners = new FixedSizeCache<>(100);
@Override
public void onEvent(GenericEvent event)
{
if (event instanceof ButtonClickEvent)
onButton((ButtonClickEvent) event);
}
private void onButton(ButtonClickEvent event)
{
Consumer<? super ButtonInteraction> callback = listeners.find(prefix -> event.getComponentId().startsWith(prefix));
if (callback == null)
{
event.reply("This menu timed out!").setEphemeral(true).queue();
return;
}
event.deferEdit().queue();
callback.accept(event);
}
public void addListener(String prefix, Consumer<? super ButtonInteraction> callback)
{
listeners.add(prefix, callback);
}
}