Skip to content

Commit

Permalink
fix: Namespace issue with slash commands
Browse files Browse the repository at this point in the history
  • Loading branch information
bendavies99 committed Aug 3, 2023
1 parent fbfebf2 commit 567d1b3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
40 changes: 22 additions & 18 deletions server/src/main/java/net/babblebot/command/CommandRegistry.java
Expand Up @@ -121,7 +121,7 @@ public void registerDiscordSlashCommands(String namespace, List<ICommand> comman
DiscordFacade facade = application.get(DiscordFacade.class);
DiscordCommandFactory factory = application.get(DiscordCommandFactory.class);
List<SlashCommandData> commandData = commands.stream()
.map(factory::createSlashCommand)
.map(c -> factory.createSlashCommand(namespace, c))
.collect(Collectors.toList());
commandData.parallelStream().forEach(cd -> {
facade.getClient().upsertCommand(cd).queue();
Expand All @@ -133,25 +133,29 @@ public void removeAllDanglingDiscordSlashCommands(IApplication application)
{
DiscordFacade facade = application.get(DiscordFacade.class);
facade.getClient().retrieveCommands().complete().parallelStream().forEach(c -> {
if (commands.values().stream()
.flatMap(Collection::stream)
.noneMatch(rc -> rc.getAliases()[0].equalsIgnoreCase(c.getName())))
{
log.info("Deleting Dangling command: {} {}", c.getName(), c.getDescription());
c.delete().queue();
}
commands.forEach((k, v) -> {
String namespace = k == null ? "" : k;
if (v.stream()
.noneMatch(rc -> (namespace + rc.getAliases()[0]).equalsIgnoreCase(c.getName())))
{
log.info("Deleting Dangling command: {} {}", c.getName(), c.getDescription());
c.delete().queue();
}
});
});
facade.getClient().getGuilds().parallelStream().forEach(g ->
g.retrieveCommands().complete().parallelStream().forEach(c -> {
if (commands.values().stream()
.flatMap(Collection::stream)
.noneMatch(rc -> rc.getAliases()[0].equalsIgnoreCase(c.getName())))
{
log.info("Deleting Dangling command: {} {} in guild: {}", c.getName(),
c.getDescription(),
g.getName());
c.delete().queue();
}
commands.forEach((k, v) -> {
String namespace = k == null ? "" : k;
if (v.stream()
.noneMatch(
rc -> (namespace + rc.getAliases()[0]).equalsIgnoreCase(c.getName())))
{
log.info("Deleting Dangling command in guild: {} {} (Guild:{})", c.getName(),
c.getDescription(), g.getName());
c.delete().queue();
}
});
}));
}

Expand Down Expand Up @@ -402,7 +406,7 @@ public List<ICommand> getCommands(String type)
* @param namespace the command namespace
* @param commandName the alias of the command
* @return {@link Optional} of a {@link ICommand} if a command is found
* @since 3.0.0-rc.28
* @since 3.0.0-rc.28
*/
public Optional<ICommand> findCommand(ICommandContext ctx, String namespace, String commandName)
{
Expand Down
Expand Up @@ -52,10 +52,11 @@ public class DiscordCommandFactory
{
private final JDA client;

public SlashCommandData createSlashCommand(ICommand command)
public SlashCommandData createSlashCommand(String namespace, ICommand command)
{
SlashCommandData commandData =
Commands.slash(command.getAliases()[0].toLowerCase(Locale.ROOT), command.getDescription());
Commands.slash(namespace + command.getAliases()[0].toLowerCase(Locale.ROOT),
command.getDescription());

Arrays.stream(command.getCommandParams()).forEach(commandParam -> {
//TODO: Support autocomplete options
Expand Down

0 comments on commit 567d1b3

Please sign in to comment.