22
22
import org .togetherjava .tjbot .db .Database ;
23
23
24
24
import java .util .*;
25
+ import java .util .concurrent .ExecutorService ;
26
+ import java .util .concurrent .Executors ;
25
27
import java .util .function .Function ;
26
28
import java .util .stream .Collectors ;
27
29
40
42
public final class CommandSystem extends ListenerAdapter implements SlashCommandProvider {
41
43
private static final Logger logger = LoggerFactory .getLogger (CommandSystem .class );
42
44
private static final String RELOAD_COMMAND = "reload" ;
45
+ private static final ExecutorService COMMAND_SERVICE = Executors .newCachedThreadPool ();
43
46
private final Map <String , SlashCommand > nameToSlashCommands ;
44
47
45
48
/**
@@ -80,7 +83,9 @@ public CommandSystem(@NotNull Database database) {
80
83
public void onReady (@ NotNull ReadyEvent event ) {
81
84
// Register reload on all guilds
82
85
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 )));
84
89
// NOTE We do not have to wait for reload to complete for the command system to be ready
85
90
// itself
86
91
logger .debug ("Command system is now ready" );
@@ -90,21 +95,22 @@ public void onReady(@NotNull ReadyEvent event) {
90
95
public void onSlashCommand (@ NotNull SlashCommandEvent event ) {
91
96
logger .debug ("Received slash command '{}' (#{}) on guild '{}'" , event .getName (),
92
97
event .getId (), event .getGuild ());
93
- requireSlashCommand (event .getName ()).onSlashCommand (event );
98
+ COMMAND_SERVICE . execute (() -> requireSlashCommand (event .getName ()).onSlashCommand (event ) );
94
99
}
95
100
96
101
@ Override
97
102
public void onButtonClick (@ NotNull ButtonClickEvent event ) {
98
103
logger .debug ("Received button click '{}' (#{}) on guild '{}'" , event .getComponentId (),
99
104
event .getId (), event .getGuild ());
100
- forwardComponentCommand (event , SlashCommand ::onButtonClick );
105
+ COMMAND_SERVICE . execute (() -> forwardComponentCommand (event , SlashCommand ::onButtonClick ) );
101
106
}
102
107
103
108
@ Override
104
109
public void onSelectionMenu (@ NotNull SelectionMenuEvent event ) {
105
110
logger .debug ("Received selection menu event '{}' (#{}) on guild '{}'" ,
106
111
event .getComponentId (), event .getId (), event .getGuild ());
107
- forwardComponentCommand (event , SlashCommand ::onSelectionMenu );
112
+ COMMAND_SERVICE
113
+ .execute (() -> forwardComponentCommand (event , SlashCommand ::onSelectionMenu ));
108
114
}
109
115
110
116
private void registerReloadCommand (@ NotNull Guild guild ) {
0 commit comments