Skip to content

Commit e71af63

Browse files
authored
Multithreading for command system
1 parent b4e971e commit e71af63

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ public interface SlashCommand {
9292
* Triggered by the command system when a slash command corresponding to this implementation
9393
* (based on {@link #getData()} has been triggered.
9494
* <p>
95+
* This method may be called multi-threaded. In particular, there are no guarantees that it will
96+
* be executed on the same thread repeatedly or on the same thread that other event methods have
97+
* been called on.
98+
* <p>
9599
* Details are available in the given event and the event also enables implementations to
96100
* respond to it.
97101
* <p>
@@ -134,6 +138,10 @@ public interface SlashCommand {
134138
* Triggered by the command system when a button corresponding to this implementation (based on
135139
* {@link #getData()} has been clicked.
136140
* <p>
141+
* This method may be called multi-threaded. In particular, there are no guarantees that it will
142+
* be executed on the same thread repeatedly or on the same thread that other event methods have
143+
* been called on.
144+
* <p>
137145
* Details are available in the given event and the event also enables implementations to
138146
* respond to it.
139147
* <p>
@@ -150,6 +158,10 @@ public interface SlashCommand {
150158
* Triggered by the command system when a selection menu corresponding to this implementation
151159
* (based on {@link #getData()} has been clicked.
152160
* <p>
161+
* This method may be called multi-threaded. In particular, there are no guarantees that it will
162+
* be executed on the same thread repeatedly or on the same thread that other event methods have
163+
* been called on.
164+
* <p>
153165
* Details are available in the given event and the event also enables implementations to
154166
* respond to it.
155167
* <p>

application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.togetherjava.tjbot.db.Database;
2323

2424
import java.util.*;
25+
import java.util.concurrent.ExecutorService;
26+
import java.util.concurrent.Executors;
2527
import java.util.function.Function;
2628
import java.util.stream.Collectors;
2729

@@ -40,6 +42,7 @@
4042
public final class CommandSystem extends ListenerAdapter implements SlashCommandProvider {
4143
private static final Logger logger = LoggerFactory.getLogger(CommandSystem.class);
4244
private static final String RELOAD_COMMAND = "reload";
45+
private static final ExecutorService COMMAND_SERVICE = Executors.newCachedThreadPool();
4346
private final Map<String, SlashCommand> nameToSlashCommands;
4447

4548
/**
@@ -80,7 +83,9 @@ public CommandSystem(@NotNull Database database) {
8083
public void onReady(@NotNull ReadyEvent event) {
8184
// Register reload on all guilds
8285
logger.debug("JDA is ready, registering reload command");
83-
event.getJDA().getGuildCache().forEach(this::registerReloadCommand);
86+
event.getJDA()
87+
.getGuildCache()
88+
.forEach(guild -> COMMAND_SERVICE.execute(() -> registerReloadCommand(guild)));
8489
// NOTE We do not have to wait for reload to complete for the command system to be ready
8590
// itself
8691
logger.debug("Command system is now ready");
@@ -90,21 +95,22 @@ public void onReady(@NotNull ReadyEvent event) {
9095
public void onSlashCommand(@NotNull SlashCommandEvent event) {
9196
logger.debug("Received slash command '{}' (#{}) on guild '{}'", event.getName(),
9297
event.getId(), event.getGuild());
93-
requireSlashCommand(event.getName()).onSlashCommand(event);
98+
COMMAND_SERVICE.execute(() -> requireSlashCommand(event.getName()).onSlashCommand(event));
9499
}
95100

96101
@Override
97102
public void onButtonClick(@NotNull ButtonClickEvent event) {
98103
logger.debug("Received button click '{}' (#{}) on guild '{}'", event.getComponentId(),
99104
event.getId(), event.getGuild());
100-
forwardComponentCommand(event, SlashCommand::onButtonClick);
105+
COMMAND_SERVICE.execute(() -> forwardComponentCommand(event, SlashCommand::onButtonClick));
101106
}
102107

103108
@Override
104109
public void onSelectionMenu(@NotNull SelectionMenuEvent event) {
105110
logger.debug("Received selection menu event '{}' (#{}) on guild '{}'",
106111
event.getComponentId(), event.getId(), event.getGuild());
107-
forwardComponentCommand(event, SlashCommand::onSelectionMenu);
112+
COMMAND_SERVICE
113+
.execute(() -> forwardComponentCommand(event, SlashCommand::onSelectionMenu));
108114
}
109115

110116
private void registerReloadCommand(@NotNull Guild guild) {

0 commit comments

Comments
 (0)