From 567d1b3e0b9acf6d3ec75a5f49bcbde5de75da0f Mon Sep 17 00:00:00 2001 From: Ben Davies Date: Thu, 3 Aug 2023 13:55:46 +0100 Subject: [PATCH] fix: Namespace issue with slash commands --- .../babblebot/command/CommandRegistry.java | 40 ++++++++++--------- .../obj/factories/DiscordCommandFactory.java | 5 ++- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/server/src/main/java/net/babblebot/command/CommandRegistry.java b/server/src/main/java/net/babblebot/command/CommandRegistry.java index 6d827a8..3a38206 100644 --- a/server/src/main/java/net/babblebot/command/CommandRegistry.java +++ b/server/src/main/java/net/babblebot/command/CommandRegistry.java @@ -121,7 +121,7 @@ public void registerDiscordSlashCommands(String namespace, List comman DiscordFacade facade = application.get(DiscordFacade.class); DiscordCommandFactory factory = application.get(DiscordCommandFactory.class); List commandData = commands.stream() - .map(factory::createSlashCommand) + .map(c -> factory.createSlashCommand(namespace, c)) .collect(Collectors.toList()); commandData.parallelStream().forEach(cd -> { facade.getClient().upsertCommand(cd).queue(); @@ -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(); + } + }); })); } @@ -402,7 +406,7 @@ public List 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 findCommand(ICommandContext ctx, String namespace, String commandName) { diff --git a/server/src/main/java/net/babblebot/discord/obj/factories/DiscordCommandFactory.java b/server/src/main/java/net/babblebot/discord/obj/factories/DiscordCommandFactory.java index dbf7db6..b66a36a 100644 --- a/server/src/main/java/net/babblebot/discord/obj/factories/DiscordCommandFactory.java +++ b/server/src/main/java/net/babblebot/discord/obj/factories/DiscordCommandFactory.java @@ -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