44import i .fran2019 .BotMaster .API .implementations .Command ;
55import i .fran2019 .BotMaster .BotMaster ;
66import net .dv8tion .jda .api .entities .Guild ;
7+ import net .dv8tion .jda .api .entities .ISnowflake ;
78import net .dv8tion .jda .api .events .guild .GuildJoinEvent ;
89import net .dv8tion .jda .api .events .interaction .command .SlashCommandInteractionEvent ;
910import net .dv8tion .jda .api .hooks .ListenerAdapter ;
1617
1718import java .lang .reflect .Field ;
1819import java .util .*;
20+ import java .util .concurrent .CompletableFuture ;
1921
2022public class CommandManager extends ListenerAdapter {
2123 private boolean started = false ;
@@ -30,6 +32,7 @@ public CommandManager() {
3032 this .commandsData = new ArrayList <>();
3133 }
3234
35+ @ SuppressWarnings ("unused" )
3336 public void registerCommand (Command cmd ) {
3437 if (this .commands .get (cmd .getName ().toLowerCase ()) == null ) {
3538 this .commands .put (cmd .getName ().toLowerCase (), cmd );
@@ -42,31 +45,72 @@ public void registerCommand(Command cmd) {
4245
4346 protected void registerSlashCommands () {
4447 if (!this .started ) this .started = true ;
48+
4549 if (BotMaster .getBotMaster ().getConfigManager ().COMMANDS_SLASH_REGISTER .equalsIgnoreCase ("global" )) {
4650 BotMaster .getLogger ().info ("Loading commands. (Global) (takes 1 hour to update)" );
51+
4752 BotMaster .getBotMaster ().getJda ().retrieveCommands ().queue (existingCommands -> {
48- existingCommands .forEach (existingCommand -> {
49- for (CommandData commandData : commandsData ) {
50- if (commandData .getName ().equals (existingCommand .getName ())) {
51- BotMaster .getBotMaster ().getJda ().deleteCommandById (existingCommand .getIdLong ()).queue ();
52- }
53- }
54- });
55- BotMaster .getBotMaster ().getJda ().updateCommands ().addCommands (this .commandsData ).queue ();
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+ }
5678 });
79+
5780 } else {
5881 BotMaster .getLogger ().info ("Loading commands on {} guilds. (Local)" , BotMaster .getBotMaster ().getJda ().getGuilds ().size ());
59- if (BotMaster .getBotMaster ().getJda ().getGuilds ().size () >= 50 ) BotMaster .getLogger ().warn ("You could consider starting to use \" Global\" commands to avoid a high performance load when starting the bot." );
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+
6087 for (Guild guild : BotMaster .getBotMaster ().getJda ().getGuilds ()) {
6188 guild .retrieveCommands ().queue (existingCommands -> {
62- existingCommands .forEach (existingCommand -> {
63- for (CommandData commandData : commandsData ) {
64- if (commandData .getName ().equals (existingCommand .getName ())) {
65- guild .deleteCommandById (existingCommand .getIdLong ()).queue ();
66- }
67- }
68- });
69- guild .updateCommands ().addCommands (this .commandsData ).queue ();
89+ List <Long > commandsToDelete = existingCommands .stream ()
90+ .filter (existingCommand -> commandsData .stream ().anyMatch (commandData -> commandData .getName ().equals (existingCommand .getName ())))
91+ .map (ISnowflake ::getIdLong )
92+ .toList ();
93+
94+ if (!commandsToDelete .isEmpty ()) {
95+ CompletableFuture <Void > deletionFuture = CompletableFuture .allOf (
96+ commandsToDelete .stream ()
97+ .map (commandId -> CompletableFuture .runAsync (() ->
98+ guild .deleteCommandById (commandId ).queue ()))
99+ .toArray (CompletableFuture []::new )
100+ );
101+
102+ 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 )
106+ )
107+ );
108+ } 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 )
112+ );
113+ }
70114 });
71115 }
72116 }
0 commit comments