@@ -34,13 +34,17 @@ public CommandManager() {
34
34
35
35
@ SuppressWarnings ("unused" )
36
36
public void registerCommand (Command cmd ) {
37
+ if (started ) {
38
+ BotMaster .getLogger ().error ("Can't register a new command." );
39
+ return ;
40
+ }
41
+
37
42
if (this .commands .get (cmd .getName ().toLowerCase ()) == null ) {
38
43
this .commands .put (cmd .getName ().toLowerCase (), cmd );
39
44
addCommandData (cmd );
40
45
}
41
46
else BotMaster .getLogger ().warn ("The command cannot be registered correctly because it already exists." );
42
47
43
- if (started ) registerSlashCommands ();
44
48
}
45
49
46
50
protected void registerSlashCommands () {
@@ -50,67 +54,77 @@ protected void registerSlashCommands() {
50
54
BotMaster .getLogger ().info ("Loading commands. (Global) (takes 1 hour to update)" );
51
55
52
56
BotMaster .getBotMaster ().getJda ().retrieveCommands ().queue (existingCommands -> {
53
- List <Long > commandsToDelete = existingCommands .stream ()
54
- .filter (existingCommand -> commandsData .stream ().anyMatch (commandData -> commandData .getName ().equals (existingCommand .getName ())))
55
- .map (ISnowflake ::getIdLong )
56
- .toList ();
57
-
58
- if (!commandsToDelete .isEmpty ()) {
59
- CompletableFuture <Void > deletionFuture = CompletableFuture .allOf (
60
- commandsToDelete .stream ()
61
- .map (commandId -> CompletableFuture .runAsync (() ->
62
- BotMaster .getBotMaster ().getJda ().deleteCommandById (commandId ).queue ()))
63
- .toArray (CompletableFuture []::new )
64
- );
65
-
66
- deletionFuture .thenRun (() ->
67
- BotMaster .getBotMaster ().getJda ().updateCommands ().addCommands (this .commandsData ).queue (
68
- success -> BotMaster .getLogger ().info ("Global commands registered successfully." ),
69
- failure -> BotMaster .getLogger ().error ("Failed to register global commands." , failure )
70
- )
71
- );
72
- } else {
73
- BotMaster .getBotMaster ().getJda ().updateCommands ().addCommands (this .commandsData ).queue (
74
- success -> BotMaster .getLogger ().info ("Global commands registered successfully." ),
75
- failure -> BotMaster .getLogger ().error ("Failed to register global commands." , failure )
76
- );
77
- }
78
- });
79
-
80
- } else {
81
- BotMaster .getLogger ().info ("Loading commands on {} guilds. (Local)" , BotMaster .getBotMaster ().getJda ().getGuilds ().size ());
82
-
83
- if (BotMaster .getBotMaster ().getJda ().getGuilds ().size () >= 50 ) {
84
- BotMaster .getLogger ().warn ("You could consider starting to use \" Global\" commands to avoid a high performance load when starting the bot." );
85
- }
86
-
87
- for (Guild guild : BotMaster .getBotMaster ().getJda ().getGuilds ()) {
88
- guild .retrieveCommands ().queue (existingCommands -> {
57
+ if (existingCommands != null && commandsData != null ) {
89
58
List <Long > commandsToDelete = existingCommands .stream ()
90
- .filter (existingCommand -> commandsData .stream ().anyMatch (commandData -> commandData .getName ().equals (existingCommand .getName ())))
59
+ .filter (existingCommand -> commandsData .stream ().noneMatch (commandData -> commandData .getName ().equals (existingCommand .getName ())))
91
60
.map (ISnowflake ::getIdLong )
92
61
.toList ();
93
62
94
63
if (!commandsToDelete .isEmpty ()) {
95
64
CompletableFuture <Void > deletionFuture = CompletableFuture .allOf (
96
65
commandsToDelete .stream ()
97
66
.map (commandId -> CompletableFuture .runAsync (() ->
98
- guild .deleteCommandById (commandId ).queue ()))
67
+ BotMaster .getBotMaster ().getJda ().deleteCommandById (commandId ).queue (
68
+ success -> {},
69
+ throwable -> {}
70
+ )))
99
71
.toArray (CompletableFuture []::new )
100
72
);
101
73
102
74
deletionFuture .thenRun (() ->
103
- guild .updateCommands ().addCommands (this .commandsData ).queue (
104
- success -> BotMaster .getLogger ().info ("Commands registered successfully on guild: {}" , guild . getName () ),
105
- failure -> BotMaster .getLogger ().error ("Failed to register commands on guild: {}" , guild . getName () , failure )
75
+ BotMaster . getBotMaster (). getJda () .updateCommands ().addCommands (this .commandsData ).queue (
76
+ success -> BotMaster .getLogger ().info ("Global commands registered successfully." ),
77
+ failure -> BotMaster .getLogger ().error ("Failed to register global commands." , failure )
106
78
)
107
79
);
108
80
} else {
109
- guild .updateCommands ().addCommands (this .commandsData ).queue (
110
- success -> BotMaster .getLogger ().info ("Commands registered successfully on guild: {}" , guild . getName () ),
111
- failure -> BotMaster .getLogger ().error ("Failed to register commands on guild: {}" , guild . getName () , failure )
81
+ BotMaster . getBotMaster (). getJda () .updateCommands ().addCommands (this .commandsData ).queue (
82
+ success -> BotMaster .getLogger ().info ("Global commands registered successfully." ),
83
+ failure -> BotMaster .getLogger ().error ("Failed to register global commands." , failure )
112
84
);
113
85
}
86
+ }
87
+ });
88
+
89
+ } else {
90
+ BotMaster .getLogger ().info ("Loading commands on {} guilds. (Local)" , BotMaster .getBotMaster ().getJda ().getGuilds ().size ());
91
+
92
+ if (BotMaster .getBotMaster ().getJda ().getGuilds ().size () >= 50 ) {
93
+ BotMaster .getLogger ().warn ("You could consider starting to use \" Global\" commands to avoid a high performance load when starting the bot." );
94
+ }
95
+
96
+ for (Guild guild : BotMaster .getBotMaster ().getJda ().getGuilds ()) {
97
+ guild .retrieveCommands ().queue (existingCommands -> {
98
+ if (existingCommands != null && commandsData != null ) {
99
+ List <Long > commandsToDelete = existingCommands .stream ()
100
+ .filter (existingCommand -> commandsData .stream ().noneMatch (commandData -> commandData .getName ().equals (existingCommand .getName ())))
101
+ .map (ISnowflake ::getIdLong )
102
+ .toList ();
103
+
104
+ if (!commandsToDelete .isEmpty ()) {
105
+ CompletableFuture <Void > deletionFuture = CompletableFuture .allOf (
106
+ commandsToDelete .stream ()
107
+ .map (commandId -> CompletableFuture .runAsync (() ->
108
+ guild .deleteCommandById (commandId ).queue (
109
+ success -> {},
110
+ throwable -> {}
111
+ )))
112
+ .toArray (CompletableFuture []::new )
113
+ );
114
+
115
+ deletionFuture .thenRun (() ->
116
+ guild .updateCommands ().addCommands (this .commandsData ).queue (
117
+ success -> BotMaster .getLogger ().info ("Commands registered successfully on guild: {}" , guild .getName ()),
118
+ failure -> BotMaster .getLogger ().error ("Failed to register commands on guild: {}" , guild .getName (), failure )
119
+ )
120
+ );
121
+ } else {
122
+ guild .updateCommands ().addCommands (this .commandsData ).queue (
123
+ success -> BotMaster .getLogger ().info ("Commands registered successfully on guild: {}" , guild .getName ()),
124
+ failure -> BotMaster .getLogger ().error ("Failed to register commands on guild: {}" , guild .getName (), failure )
125
+ );
126
+ }
127
+ }
114
128
});
115
129
}
116
130
}
0 commit comments