Skip to content

Commit

Permalink
Optimized Slash Command Upsert (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chew authored Dec 20, 2021
1 parent cd1812f commit afa1b07
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 /<parent name> <subcommandGroup name> <subcommand name>}.
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommandPrivilege> 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<CommandData> data = new ArrayList<>();
List<SlashCommand> slashCommands = getSlashCommands();
Map<String, SlashCommand> 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<String, Collection<? extends CommandPrivilege>> 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
Expand Down

0 comments on commit afa1b07

Please sign in to comment.