Skip to content

Commit

Permalink
⚡ Enhanced slash command un-registration system for slash structures
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsTheSky committed Jan 11, 2025
1 parent 9c1fa6f commit decd16d
Showing 1 changed file with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.session.ReadyEvent;
import net.dv8tion.jda.api.exceptions.RateLimitedException;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
Expand Down Expand Up @@ -348,17 +349,32 @@ public void shutdown() {
}

private void cleanupRegisteredCommands() {
new ArrayList<>(registeredCommands).forEach(command -> {
try {
if (command.getGuildId() == null) {
deleteGlobalCommand(command);
} else {
deleteLocalCommand(command);
// gather all guilds, if any, to clear
Set<String> guilds = new HashSet<>();
registeredCommands.forEach(cmd -> {
if (cmd.getGuildId() != null)
guilds.add(cmd.getGuildId());
});

// delete all commands
try {
for (String guildId : guilds) {
var guild = bot.getInstance().getGuildById(guildId);
if (guild == null) {
DiSky.debug("Guild " + guildId + " not found, skipping command deletion");
continue;
}
} catch (Exception ex) {
DiSky.debug("Failed to delete command " + command.getName() + ": " + ex.getMessage());

var commands = guild.retrieveCommands().complete(true);
for (Command cmd : commands)
guild.deleteCommandById(cmd.getId()).complete(true);
}
});
var commands = bot.getInstance().retrieveCommands().complete(true);
for (Command cmd : commands)
bot.getInstance().deleteCommandById(cmd.getId()).complete(true);
} catch (RateLimitedException ex) {
DiSky.debug("Failed to delete all commands: " + ex.getMessage());
}
}

// Debug Methods
Expand Down

0 comments on commit decd16d

Please sign in to comment.