2222 * THE SOFTWARE.
2323 */
2424
25- package com .readonlydev .command . client ;
25+ package com .readonlydev .command ;
2626
2727import java .time .OffsetDateTime ;
2828import java .time .temporal .ChronoUnit ;
5151import org .slf4j .LoggerFactory ;
5252
5353import com .readonlydev .api .ClientInterface ;
54- import com .readonlydev .command .Command ;
5554import com .readonlydev .command .Command .Category ;
56- import com .readonlydev .command .CommandListener ;
5755import com .readonlydev .command .arg .parse .ArgumentIndex ;
5856import com .readonlydev .command .ctx .ContextMenu ;
5957import com .readonlydev .command .ctx .MessageContextMenu ;
6058import com .readonlydev .command .ctx .UserContextMenu ;
6159import com .readonlydev .command .event .CommandEvent ;
6260import com .readonlydev .command .event .MessageContextMenuEvent ;
61+ import com .readonlydev .command .event .SlashCommandEvent ;
6362import com .readonlydev .command .event .UserContextMenuEvent ;
64- import com .readonlydev .command .slash .SlashCommand ;
65- import com .readonlydev .command .slash .SlashCommandEvent ;
6663import com .readonlydev .common .utils .FixedSizeCache ;
6764import com .readonlydev .common .utils .SafeIdUtil ;
6865import com .readonlydev .settings .GuildSettingsManager ;
@@ -131,6 +128,8 @@ public class Client implements ClientInterface, EventListener
131128 private final ScheduledExecutorService executor ;
132129 private final GuildSettingsManager <?> manager ;
133130
131+ private final LinkedList <SlashCommand > globalCommands ;
132+
134133 private final LinkedList <SlashCommand > totalSlashCommands ;
135134 private final HashMap <String , Integer > slashCommandIndex ;
136135
@@ -208,6 +207,7 @@ public Client(
208207 this .slashCommandIndex = new HashMap <>();
209208 this .totalContextMenus = new LinkedList <>();
210209 this .contextMenuIndex = new HashMap <>();
210+ this .globalCommands = new LinkedList <>();
211211 this .commands = new ArrayList <>();
212212 this .categoryToCommandListMap = new HashMap <>();
213213 this .categoryToCommandListMap .put (new Category ("Uncategorized" ), new ArrayList <>());
@@ -267,6 +267,7 @@ public Client(
267267 for (SlashCommand command : globalSlashCommands )
268268 {
269269 indexSlashCommand (command );
270+ globalCommands .add (command );
270271 }
271272
272273 // Load context menus
@@ -277,6 +278,8 @@ public Client(
277278
278279 for (ServerCommands serverCmds : serverCommands )
279280 {
281+ this .serverCommands .add (serverCmds );
282+
280283 for (SlashCommand serverSlashCmd : serverCmds .getSlashCommands ())
281284 {
282285 indexSlashCommand (serverSlashCmd );
@@ -316,7 +319,7 @@ public List<ServerCommands> getServerCommands()
316319 @ Override
317320 public List <SlashCommand > getGlobalSlashCommands ()
318321 {
319- return totalSlashCommands . stream (). filter ( SlashCommand :: isGlobalCommand ). toList () ;
322+ return globalCommands ;
320323 }
321324
322325 @ Override
@@ -718,10 +721,7 @@ private void onReady(ReadyEvent event)
718721 manager .init (event .getJDA ());
719722 }
720723
721- if (!serverCommands .isEmpty ())
722- {
723- upsertServerCommands (event .getJDA ());
724- }
724+ upsertServerCommands (event .getJDA ());
725725
726726 upsertGlobalSlashCommands (event .getJDA ());
727727 }
@@ -730,24 +730,34 @@ public void upsertServerCommands(JDA jda)
730730 {
731731 if (serverCommands .isEmpty ())
732732 {
733+ LOG .info ("ServerCommands is empty. Skipping guildCommandUpsert" );
733734 return ;
734735 }
735736
736737 for (ServerCommands server : serverCommands )
737738 {
738739 List <CommandData > data = new ArrayList <>();
739740
740- data .addAll (server .getSlashCommands ().stream ().map (SlashCommand ::getCommandData ).toList ());
741- data .addAll (server .getContextMenus ().stream ().map (ContextMenu ::getCommandData ).toList ());
741+ for (SlashCommand cmd : server .getSlashCommands ())
742+ {
743+ data .add (cmd .build ());
744+ }
745+
746+ for (ContextMenu ctx : server .getContextMenus ())
747+ {
748+ data .add (ctx .build ());
749+ }
742750
743751 // Attempt to retrieve the provided guild
744- Guild guild = jda .getGuildById (server .getServerId ());
752+ Guild guild = jda .getGuildById (server .getGuildIdLong ());
745753 if (guild == null )
746754 {
747- LOG .error ("Specified guild '{}' is null! Slash Commands will NOT be added!" , server .getServerId ());
755+ LOG .error ("Specified guild '{}' is null! Slash Commands will NOT be added!" , server .getGuildIdLong ());
748756 return ;
749757 } else
750758 {
759+ guild .updateCommands ().queue ();
760+
751761 //@noformat
752762 guild .updateCommands ().addCommands (data ).queue (
753763 priv -> LOG .info ("Successfully added " + server .getSlashCommands ().size () + " slash commands and " + server .getContextMenus ().size () + " menus to server " + guild .getName ()),
@@ -760,9 +770,11 @@ public void upsertServerCommands(JDA jda)
760770 public void upsertGlobalSlashCommands (JDA jda )
761771 {
762772 // Get all commands
763- List <SlashCommand > globalCommands = totalSlashCommands .stream ().filter (SlashCommand ::isGlobalCommand ).toList ();
764773 List <CommandData > data = new ArrayList <>();
765- data .addAll (globalCommands .stream ().map (SlashCommand ::getCommandData ).toList ());
774+ for (SlashCommand cmd : globalCommands )
775+ {
776+ data .add (cmd .build ());
777+ }
766778
767779 jda .updateCommands ().addCommands (data ).queue (commands -> LOG .info ("Successfully added " + globalCommands .size () + " global slash commands!" ));
768780 }
@@ -988,6 +1000,7 @@ private void onSlashCommand(SlashCommandInteractionEvent event)
9881000 listener .onSlashCommand (commandEvent , command );
9891001 }
9901002 uses .put (command .getName (), uses .getOrDefault (command .getName (), 0 ) + 1 );
1003+
9911004 command .run (commandEvent );
9921005 // Command is done
9931006 }
0 commit comments