From afa1b077b660242f4e177d6adff9131c967f4fc8 Mon Sep 17 00:00:00 2001 From: Olivia Date: Sun, 19 Dec 2021 22:11:26 -0600 Subject: [PATCH] Optimized Slash Command Upsert (#34) --- .../jdautilities/command/SlashCommand.java | 21 ------ .../command/impl/CommandClientImpl.java | 66 +++++++++++++------ 2 files changed, 45 insertions(+), 42 deletions(-) diff --git a/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommand.java b/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommand.java index 74b96280..f43627f2 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommand.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommand.java @@ -124,15 +124,6 @@ public abstract class SlashCommand extends Command */ protected SlashCommand[] children = new SlashCommand[0]; - /** - * The ID of the server you want guildOnly tied to. - * This means the slash command will only work and show up in the specified Guild. - * If this is null, guildOnly will still be processed, however an ephemeral message will be sent telling them to move. - * @deprecated This will be removed in favor of {@link CommandClientBuilder#forceGuildOnly(String)}. Please use that instead. - * @see CommandClientBuilder#forceGuildOnly(String) - */ - protected String guildId = null; - /** * The subcommand/child group this is associated with. * Will be in format {@code / }. @@ -392,18 +383,6 @@ public CommandClient getClient() return client; } - /** - * Gets the associated Guild ID for Guild Only command. - * - * @deprecated This will be removed in favor of {@link CommandClientBuilder#forceGuildOnly(String)}. Please use that instead. - * @see CommandClientBuilder#forceGuildOnly(String) - * @return the ID for the specific Guild - */ - public String getGuildId() - { - return guildId; - } - /** * Gets the enabled roles for this Slash Command. * A user MUST have a role for a command to be ran. diff --git a/command/src/main/java/com/jagrosh/jdautilities/command/impl/CommandClientImpl.java b/command/src/main/java/com/jagrosh/jdautilities/command/impl/CommandClientImpl.java index eeeaca64..0da46da0 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/impl/CommandClientImpl.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/impl/CommandClientImpl.java @@ -579,32 +579,56 @@ private void onReady(ReadyEvent event) // Upsert slash commands, if not manual if (!manualUpsert) { - for (SlashCommand command : slashCommands) - { - CommandData data = command.buildCommandData(); - - if (forcedGuildId != null || (command.isGuildOnly() && command.getGuildId() != null)) { - String guildId = forcedGuildId != null ? forcedGuildId : command.getGuildId(); - Guild guild = event.getJDA().getGuildById(guildId); - if (guild == null) { - LOG.error("Could not find guild with specified ID: " + forcedGuildId + ". Not going to upsert."); - continue; - } - List privileges = command.buildPrivileges(this); - guild.upsertCommand(data).queue(command1 -> { - slashCommandIds.add(command1.getId()); - if (!privileges.isEmpty()) - command1.updatePrivileges(guild, privileges).queue(); - }); - } else { - event.getJDA().upsertCommand(data).queue(command1 -> slashCommandIds.add(command1.getId())); - } - } + upsertSlashCommands(event.getJDA()); } sendStats(event.getJDA()); } + private void upsertSlashCommands(JDA jda) + { + // Get all commands + List data = new ArrayList<>(); + List slashCommands = getSlashCommands(); + Map slashCommandMap = new HashMap<>(); + + // Build the command and privilege data + for (SlashCommand command : slashCommands) + { + data.add(command.buildCommandData()); + slashCommandMap.put(command.getName(), command); + } + + // Upsert the commands + if (forcedGuildId != null) + { + // Attempt to retrieve the provided guild + Guild server = jda.getGuildById(forcedGuildId); + if (server == null) + { + LOG.error("Server used for slash command testing is null! Slash Commands will NOT be added!"); + return; + } + // Upsert the commands + their privileges + server.updateCommands().addCommands(data) + .queue(commands -> { + Map> privileges = new HashMap<>(); + for (net.dv8tion.jda.api.interactions.commands.Command command : commands) + { + SlashCommand slashCommand = slashCommandMap.get(command.getName()); + privileges.put(command.getId(), slashCommand.buildPrivileges(this)); + } + server.updateCommandPrivileges(privileges) + .queue(priv -> LOG.debug("Successfully added" + commands.size() + "slash commands!")); + }, error -> LOG.error("Could not upsert commands! Does the bot have the applications.commands scope?" + error)); + } + else + { + jda.updateCommands().addCommands(data) + .queue(commands -> LOG.debug("Successfully added" + commands.size() + "slash commands!")); + } + } + private void onMessageReceived(MessageReceivedEvent event) { // Return if it's a bot