From f6840076be1ed9a508dbd0208f6b2283cd8cc823 Mon Sep 17 00:00:00 2001 From: Gyoo Date: Thu, 8 Sep 2022 07:55:18 +0200 Subject: [PATCH 01/14] Use slash-command mentions (#559) --- .../tjbot/commands/help/AskCommand.java | 2 +- .../tjbot/commands/help/HelpSystemHelper.java | 7 +++-- .../commands/help/ImplicitAskListener.java | 30 +++++++++---------- .../tjbot/commands/utils/MessageUtils.java | 26 ++++++++++++++++ 4 files changed, 45 insertions(+), 20 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java index 4d8d8aa1aa..53c14515cb 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java @@ -128,7 +128,7 @@ private RestAction handleEvent(InteractionHook eventHook, ThreadChannel helper.writeHelpThreadToDatabase(author, threadChannel); return sendInitialMessage(guild, threadChannel, author, title, category) .flatMap(any -> notifyUser(eventHook, threadChannel)) - .flatMap(any -> helper.sendExplanationMessage(threadChannel)); + .flatMap(any -> helper.sendExplanationMessage(guild, threadChannel)); } private RestAction sendInitialMessage(Guild guild, ThreadChannel threadChannel, diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java index d285f280c0..cae20888a6 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java @@ -16,7 +16,7 @@ import net.dv8tion.jda.internal.requests.CompletedRestAction; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - +import org.togetherjava.tjbot.commands.utils.MessageUtils; import org.togetherjava.tjbot.config.Config; import org.togetherjava.tjbot.config.HelpSystemConfig; import org.togetherjava.tjbot.db.Database; @@ -92,7 +92,7 @@ public HelpSystemHelper(JDA jda, Config config, Database database) { categoryRoleSuffix = helpConfig.getCategoryRoleSuffix(); } - RestAction sendExplanationMessage(MessageChannel threadChannel) { + RestAction sendExplanationMessage(Guild guild, MessageChannel threadChannel) { boolean useCodeSyntaxExampleImage = true; InputStream codeSyntaxExampleData = AskCommand.class.getResourceAsStream("/" + CODE_SYNTAX_EXAMPLE_PATH); @@ -117,7 +117,8 @@ RestAction sendExplanationMessage(MessageChannel threadChannel) { **provide details**, context, more code, examples and maybe some screenshots. \ With enough info, someone knows the answer for sure."""), HelpSystemHelper.embedWith( - "Don't forget to close your thread using the command **/help-thread close** when your question has been answered, thanks.")); + "Don't forget to close your thread using the command **%s close** when your question has been answered, thanks." + .formatted(MessageUtils.mentionSlashCommand(guild, "help-thread")))); MessageCreateAction action = threadChannel.sendMessage(message); if (useCodeSyntaxExampleImage) { diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java index 17a49dbf64..0c49f5b584 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java @@ -17,6 +17,7 @@ import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.MessageReceiverAdapter; +import org.togetherjava.tjbot.commands.utils.MessageUtils; import org.togetherjava.tjbot.config.Config; import java.time.Instant; @@ -121,8 +122,8 @@ private boolean handleIsNotOnCooldown(Message message) { message.getChannel() .sendMessage(""" %s Please use %s to follow up on your question, \ - or use `/ask` to ask a new questions, thanks.""" - .formatted(author.getAsMention(), threadDescription)) + or use %s to ask a new questions, thanks.""".formatted(author.getAsMention(), + threadDescription, MessageUtils.mentionSlashCommand(message.getGuild(), "ask"))) .flatMap(any -> message.delete()) .queue(); return false; @@ -166,7 +167,7 @@ private RestAction handleEvent(ThreadChannel threadChannel, Message message, return sendInitialMessage(threadChannel, message, title) .flatMap(any -> notifyUser(threadChannel, message)) .flatMap(any -> message.delete()) - .flatMap(any -> helper.sendExplanationMessage(threadChannel)) + .flatMap(any -> helper.sendExplanationMessage(message.getGuild(), threadChannel)) .onSuccess(any -> helper.scheduleUncategorizedAdviceCheck(threadChannel.getIdLong(), author.getIdLong())); } @@ -183,26 +184,23 @@ private static MessageCreateAction sendInitialMessage(ThreadChannel threadChanne .build(); MessageCreateData threadMessage = new MessageCreateBuilder() - .setContent( - """ - %s has a question about '**%s**' and will send the details now. + .setContent(""" + %s has a question about '**%s**' and will send the details now. - Please use `/help-thread change category` to greatly increase the visibility of the question.""" - .formatted(author, title)) - .setEmbeds(embed) - .build(); + Please use **%s change category** to greatly increase the visibility of the question.""" + .formatted(author, title, MessageUtils.mentionSlashCommand(originalMessage.getGuild(), + "help-thread"))).setEmbeds(embed).build(); return threadChannel.sendMessage(threadMessage); } private static MessageCreateAction notifyUser(IMentionable threadChannel, Message message) { return message.getChannel() - .sendMessage( - """ - %s Please use `/ask` to ask questions. Don't worry though, I created %s for you. \ - Please continue there, thanks.""" - .formatted(message.getAuthor().getAsMention(), - threadChannel.getAsMention())); + .sendMessage(""" + %s Please use %s to ask questions. Don't worry though, I created %s for you. \ + Please continue there, thanks.""".formatted(message.getAuthor().getAsMention(), + MessageUtils.mentionSlashCommand(message.getGuild(), "ask"), + threadChannel.getAsMention())); } private static void handleFailure(Throwable exception) { diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java index 10e8436763..73f7546fb8 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java @@ -1,11 +1,14 @@ package org.togetherjava.tjbot.commands.utils; +import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.interactions.components.ActionRow; import net.dv8tion.jda.api.interactions.components.buttons.Button; import net.dv8tion.jda.api.utils.MarkdownSanitizer; +import org.togetherjava.tjbot.commands.help.CloseCommand; import java.util.List; +import java.util.concurrent.atomic.AtomicLong; /** * Utility methods for {@link Message}. @@ -56,4 +59,27 @@ public static String escapeMarkdown(String text) { return afterEscape.replace("\\```", "\\`\\`\\`"); } + /** + * Converts a slash command text to a mentioned slash command, which you can directly click on + * in Discord + * + * @param guild guild that owns the command + * @param command command name + * @return Formatted string for mentioned slash command + * @throws IllegalArgumentException when the command name doesn't match with the guild's + * commands + */ + public static String mentionSlashCommand(Guild guild, String command) { + AtomicLong commandId = new AtomicLong(); + guild.retrieveCommands() + .queue(commands -> commands.stream() + .filter(c -> c.getName().equalsIgnoreCase(command)) + .findFirst() + .ifPresentOrElse(c -> commandId.set(c.getIdLong()), () -> { + throw new IllegalArgumentException("Command %s does not exist in guild %s" + .formatted(command, guild.getId())); + })); + return String.format("", command, commandId.get()); + } + } From 5b17c49d26977afe64355b8ef22450e0a85e596d Mon Sep 17 00:00:00 2001 From: Gyoo Date: Wed, 21 Sep 2022 10:59:53 +0200 Subject: [PATCH 02/14] PR review changes --- .../tjbot/commands/help/AskCommand.java | 4 +-- .../tjbot/commands/help/HelpSystemHelper.java | 9 ++++-- .../commands/help/ImplicitAskListener.java | 18 ++++++++---- .../tjbot/commands/utils/MessageUtils.java | 28 +++++++++---------- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java index 53c14515cb..2c1eb97e09 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java @@ -48,7 +48,7 @@ */ public final class AskCommand extends SlashCommandAdapter { private static final Logger logger = LoggerFactory.getLogger(AskCommand.class); - + public static final String COMMAND_NAME = "ask"; private static final String TITLE_OPTION = "title"; private static final String CATEGORY_OPTION = "category"; private final HelpSystemHelper helper; @@ -128,7 +128,7 @@ private RestAction handleEvent(InteractionHook eventHook, ThreadChannel helper.writeHelpThreadToDatabase(author, threadChannel); return sendInitialMessage(guild, threadChannel, author, title, category) .flatMap(any -> notifyUser(eventHook, threadChannel)) - .flatMap(any -> helper.sendExplanationMessage(guild, threadChannel)); + .flatMap(any -> helper.sendExplanationMessage(threadChannel)); } private RestAction sendInitialMessage(Guild guild, ThreadChannel threadChannel, diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java index cae20888a6..99e2a9bd45 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java @@ -92,7 +92,7 @@ public HelpSystemHelper(JDA jda, Config config, Database database) { categoryRoleSuffix = helpConfig.getCategoryRoleSuffix(); } - RestAction sendExplanationMessage(Guild guild, MessageChannel threadChannel) { + RestAction sendExplanationMessage(GuildMessageChannel threadChannel) { boolean useCodeSyntaxExampleImage = true; InputStream codeSyntaxExampleData = AskCommand.class.getResourceAsStream("/" + CODE_SYNTAX_EXAMPLE_PATH); @@ -117,8 +117,11 @@ RestAction sendExplanationMessage(Guild guild, MessageChannel threadCha **provide details**, context, more code, examples and maybe some screenshots. \ With enough info, someone knows the answer for sure."""), HelpSystemHelper.embedWith( - "Don't forget to close your thread using the command **%s close** when your question has been answered, thanks." - .formatted(MessageUtils.mentionSlashCommand(guild, "help-thread")))); + "Don't forget to close your thread using the command %s when your question has been answered, thanks." + .formatted(MessageUtils + .mentionSlashCommand(threadChannel.getGuild(), + CloseCommand.COMMAND_NAME) + .complete()))); MessageCreateAction action = threadChannel.sendMessage(message); if (useCodeSyntaxExampleImage) { diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java index 0c49f5b584..0fd44dac4a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java @@ -123,7 +123,9 @@ private boolean handleIsNotOnCooldown(Message message) { .sendMessage(""" %s Please use %s to follow up on your question, \ or use %s to ask a new questions, thanks.""".formatted(author.getAsMention(), - threadDescription, MessageUtils.mentionSlashCommand(message.getGuild(), "ask"))) + threadDescription, + MessageUtils.mentionSlashCommand(message.getGuild(), AskCommand.COMMAND_NAME) + .complete())) .flatMap(any -> message.delete()) .queue(); return false; @@ -167,7 +169,7 @@ private RestAction handleEvent(ThreadChannel threadChannel, Message message, return sendInitialMessage(threadChannel, message, title) .flatMap(any -> notifyUser(threadChannel, message)) .flatMap(any -> message.delete()) - .flatMap(any -> helper.sendExplanationMessage(message.getGuild(), threadChannel)) + .flatMap(any -> helper.sendExplanationMessage(threadChannel)) .onSuccess(any -> helper.scheduleUncategorizedAdviceCheck(threadChannel.getIdLong(), author.getIdLong())); } @@ -187,9 +189,12 @@ private static MessageCreateAction sendInitialMessage(ThreadChannel threadChanne .setContent(""" %s has a question about '**%s**' and will send the details now. - Please use **%s change category** to greatly increase the visibility of the question.""" - .formatted(author, title, MessageUtils.mentionSlashCommand(originalMessage.getGuild(), - "help-thread"))).setEmbeds(embed).build(); + Please use %s to greatly increase the visibility of the question.""".formatted( + author, title, + MessageUtils + .mentionSlashCommand(originalMessage.getGuild(), + ChangeHelpCategoryCommand.COMMAND_NAME) + .complete())).setEmbeds(embed).build(); return threadChannel.sendMessage(threadMessage); } @@ -199,7 +204,8 @@ private static MessageCreateAction notifyUser(IMentionable threadChannel, Messag .sendMessage(""" %s Please use %s to ask questions. Don't worry though, I created %s for you. \ Please continue there, thanks.""".formatted(message.getAuthor().getAsMention(), - MessageUtils.mentionSlashCommand(message.getGuild(), "ask"), + MessageUtils.mentionSlashCommand(message.getGuild(), AskCommand.COMMAND_NAME) + .complete(), threadChannel.getAsMention())); } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java index 73f7546fb8..3ee8161e4c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java @@ -2,13 +2,13 @@ import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.interactions.commands.Command; import net.dv8tion.jda.api.interactions.components.ActionRow; import net.dv8tion.jda.api.interactions.components.buttons.Button; +import net.dv8tion.jda.api.requests.RestAction; import net.dv8tion.jda.api.utils.MarkdownSanitizer; -import org.togetherjava.tjbot.commands.help.CloseCommand; import java.util.List; -import java.util.concurrent.atomic.AtomicLong; /** * Utility methods for {@link Message}. @@ -61,25 +61,23 @@ public static String escapeMarkdown(String text) { /** * Converts a slash command text to a mentioned slash command, which you can directly click on - * in Discord + * in Discord. * - * @param guild guild that owns the command - * @param command command name - * @return Formatted string for mentioned slash command + * @param guild the {@link Guild} that contains the command + * @param command the command's name + * @return Formatted string for the mentioned slash command * @throws IllegalArgumentException when the command name doesn't match with the guild's * commands */ - public static String mentionSlashCommand(Guild guild, String command) { - AtomicLong commandId = new AtomicLong(); - guild.retrieveCommands() - .queue(commands -> commands.stream() + public static RestAction mentionSlashCommand(Guild guild, String command) { + return guild.retrieveCommands().map(commands -> { + Command foundCommand = commands.stream() .filter(c -> c.getName().equalsIgnoreCase(command)) .findFirst() - .ifPresentOrElse(c -> commandId.set(c.getIdLong()), () -> { - throw new IllegalArgumentException("Command %s does not exist in guild %s" - .formatted(command, guild.getId())); - })); - return String.format("", command, commandId.get()); + .orElseThrow(() -> new IllegalArgumentException( + "Command %s does not exist in guild %s".formatted(command, guild.getId()))); + return String.format("", command, foundCommand.getIdLong()); + }); } } From 30e35bb1a75b169c111fbd6749383ef7521fca77 Mon Sep 17 00:00:00 2001 From: Gyoo Date: Wed, 21 Sep 2022 14:23:48 +0200 Subject: [PATCH 03/14] Use flatMap() instead of complete() for mentionSlashCommand calls --- .../tjbot/commands/help/HelpSystemHelper.java | 11 ++-- .../commands/help/ImplicitAskListener.java | 56 ++++++++++--------- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java index 99e2a9bd45..e6c34fef86 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java @@ -93,6 +93,12 @@ public HelpSystemHelper(JDA jda, Config config, Database database) { } RestAction sendExplanationMessage(GuildMessageChannel threadChannel) { + return MessageUtils.mentionSlashCommand(threadChannel.getGuild(), CloseCommand.COMMAND_NAME) + .flatMap(command -> sendExplanationMessage(threadChannel, command)); + } + + private RestAction sendExplanationMessage(GuildMessageChannel threadChannel, + String command) { boolean useCodeSyntaxExampleImage = true; InputStream codeSyntaxExampleData = AskCommand.class.getResourceAsStream("/" + CODE_SYNTAX_EXAMPLE_PATH); @@ -118,10 +124,7 @@ RestAction sendExplanationMessage(GuildMessageChannel threadChannel) { With enough info, someone knows the answer for sure."""), HelpSystemHelper.embedWith( "Don't forget to close your thread using the command %s when your question has been answered, thanks." - .formatted(MessageUtils - .mentionSlashCommand(threadChannel.getGuild(), - CloseCommand.COMMAND_NAME) - .complete()))); + .formatted(command))); MessageCreateAction action = threadChannel.sendMessage(message); if (useCodeSyntaxExampleImage) { diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java index 0fd44dac4a..4658fa916d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java @@ -119,15 +119,15 @@ private boolean handleIsNotOnCooldown(Message message) { String threadDescription = lastHelpThread == null ? "your previously created help thread" : lastHelpThread.getAsMention(); - message.getChannel() - .sendMessage(""" - %s Please use %s to follow up on your question, \ - or use %s to ask a new questions, thanks.""".formatted(author.getAsMention(), - threadDescription, - MessageUtils.mentionSlashCommand(message.getGuild(), AskCommand.COMMAND_NAME) - .complete())) - .flatMap(any -> message.delete()) + MessageUtils.mentionSlashCommand(message.getGuild(), AskCommand.COMMAND_NAME) + .map(command -> message.getChannel() + .sendMessage(""" + %s Please use %s to follow up on your question, \ + or use %s to ask a new questions, thanks.""" + .formatted(author.getAsMention(), threadDescription, command)) + .flatMap(any -> message.delete())) .queue(); + return false; } @@ -174,7 +174,7 @@ private RestAction handleEvent(ThreadChannel threadChannel, Message message, author.getIdLong())); } - private static MessageCreateAction sendInitialMessage(ThreadChannel threadChannel, + private static RestAction sendInitialMessage(ThreadChannel threadChannel, Message originalMessage, String title) { String content = originalMessage.getContentRaw(); Member author = originalMessage.getMember(); @@ -185,28 +185,30 @@ private static MessageCreateAction sendInitialMessage(ThreadChannel threadChanne .setColor(HelpSystemHelper.AMBIENT_COLOR) .build(); - MessageCreateData threadMessage = new MessageCreateBuilder() - .setContent(""" - %s has a question about '**%s**' and will send the details now. + return MessageUtils + .mentionSlashCommand(originalMessage.getGuild(), ChangeHelpCategoryCommand.COMMAND_NAME) + .flatMap(command -> { + MessageCreateData threadMessage = new MessageCreateBuilder() + .setContent(""" + %s has a question about '**%s**' and will send the details now. - Please use %s to greatly increase the visibility of the question.""".formatted( - author, title, - MessageUtils - .mentionSlashCommand(originalMessage.getGuild(), - ChangeHelpCategoryCommand.COMMAND_NAME) - .complete())).setEmbeds(embed).build(); + Please use %s to greatly increase the visibility of the question.""" + .formatted(author, title, command)).setEmbeds(embed).build(); + + return threadChannel.sendMessage(threadMessage); + }); - return threadChannel.sendMessage(threadMessage); } - private static MessageCreateAction notifyUser(IMentionable threadChannel, Message message) { - return message.getChannel() - .sendMessage(""" - %s Please use %s to ask questions. Don't worry though, I created %s for you. \ - Please continue there, thanks.""".formatted(message.getAuthor().getAsMention(), - MessageUtils.mentionSlashCommand(message.getGuild(), AskCommand.COMMAND_NAME) - .complete(), - threadChannel.getAsMention())); + private static RestAction notifyUser(IMentionable threadChannel, Message message) { + return MessageUtils.mentionSlashCommand(message.getGuild(), AskCommand.COMMAND_NAME) + .flatMap(command -> message.getChannel() + .sendMessage( + """ + %s Please use %s to ask questions. Don't worry though, I created %s for you. \ + Please continue there, thanks.""" + .formatted(message.getAuthor().getAsMention(), command, + threadChannel.getAsMention()))); } private static void handleFailure(Throwable exception) { From 613e311f288a2b7c2e80c9504ecd231ab1e2d543 Mon Sep 17 00:00:00 2001 From: Gyoo Date: Wed, 5 Oct 2022 13:40:26 +0200 Subject: [PATCH 04/14] Mention subcommands for HelpThreadCommand --- .../tjbot/commands/help/HelpSystemHelper.java | 40 ++++++++++++------- .../commands/help/HelpThreadCommand.java | 6 ++- .../commands/help/ImplicitAskListener.java | 17 ++++---- .../tjbot/commands/utils/MessageUtils.java | 20 +++++----- 4 files changed, 51 insertions(+), 32 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java index e6c34fef86..1f60fcbe73 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java @@ -7,6 +7,7 @@ import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel; +import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; import net.dv8tion.jda.api.requests.RestAction; import net.dv8tion.jda.api.requests.restaction.MessageCreateAction; @@ -93,12 +94,15 @@ public HelpSystemHelper(JDA jda, Config config, Database database) { } RestAction sendExplanationMessage(GuildMessageChannel threadChannel) { - return MessageUtils.mentionSlashCommand(threadChannel.getGuild(), CloseCommand.COMMAND_NAME) - .flatMap(command -> sendExplanationMessage(threadChannel, command)); + return MessageUtils + .mentionSlashCommand(threadChannel.getGuild(), HelpThreadCommand.COMMAND_NAME, + HelpThreadCommand.Subcommand.CLOSE.getCommandName()) + .flatMap(closeCommandMention -> sendExplanationMessage(threadChannel, + closeCommandMention)); } private RestAction sendExplanationMessage(GuildMessageChannel threadChannel, - String command) { + String closeCommandMention) { boolean useCodeSyntaxExampleImage = true; InputStream codeSyntaxExampleData = AskCommand.class.getResourceAsStream("/" + CODE_SYNTAX_EXAMPLE_PATH); @@ -124,7 +128,7 @@ private RestAction sendExplanationMessage(GuildMessageChannel threadCha With enough info, someone knows the answer for sure."""), HelpSystemHelper.embedWith( "Don't forget to close your thread using the command %s when your question has been answered, thanks." - .formatted(command))); + .formatted(closeCommandMention))); MessageCreateAction action = threadChannel.sendMessage(message); if (useCodeSyntaxExampleImage) { @@ -307,16 +311,24 @@ private void executeUncategorizedAdviceCheck(long threadChannelId, long authorId } // Still no category, send advice - MessageEmbed embed = HelpSystemHelper.embedWith( - """ - Hey there 👋 You have to select a category for your help thread, otherwise nobody can see your question. - Please use the `/help-thread change category` slash-command and pick what fits best, thanks 🙂 - """); - MessageCreateData message = new MessageCreateBuilder().setContent(author.getAsMention()) - .setEmbeds(embed) - .build(); - - return threadChannel.sendMessage(message); + return MessageUtils + .mentionSlashCommand(threadChannel.getGuild(), HelpThreadCommand.COMMAND_NAME, + HelpThreadCommand.CHANGE_SUBCOMMAND, + HelpThreadCommand.Subcommand.CHANGE_CATEGORY.getCommandName()) + .flatMap(command -> { + MessageEmbed embed = HelpSystemHelper.embedWith( + """ + Hey there 👋 You have to select a category for your help thread, otherwise nobody can see your question. + Please use the %s slash-command and pick what fits best, thanks 🙂 + """ + .formatted(command)); + MessageCreateData message = + new MessageCreateBuilder().setContent(author.getAsMention()) + .setEmbeds(embed) + .build(); + + return threadChannel.sendMessage(message); + }); }).queue(); } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadCommand.java index ee54b83d08..36bd4dfd74 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadCommand.java @@ -45,6 +45,8 @@ public final class HelpThreadCommand extends SlashCommandAdapter { private static final String CHANGE_CATEGORY_OPTION = "category"; private static final String CHANGE_TITLE_OPTION = "title"; private static final String CHANGE_TITLE_SUBCOMMAND = "title"; + public static final String CHANGE_SUBCOMMAND = "change"; + public static final String COMMAND_NAME = "help-thread"; private final HelpSystemHelper helper; private final Map nameToSubcommand; @@ -58,7 +60,7 @@ public final class HelpThreadCommand extends SlashCommandAdapter { * @param helper the helper to use */ public HelpThreadCommand(Config config, HelpSystemHelper helper) { - super("help-thread", "Help thread specific commands", CommandVisibility.GUILD); + super(COMMAND_NAME, "Help thread specific commands", CommandVisibility.GUILD); OptionData categoryChoices = new OptionData(OptionType.STRING, CHANGE_CATEGORY_OPTION, "new category", true); @@ -73,7 +75,7 @@ public HelpThreadCommand(Config config, HelpSystemHelper helper) { .addOption(OptionType.STRING, CHANGE_TITLE_OPTION, "new title", true); SubcommandGroupData changeCommands = - new SubcommandGroupData("change", "Change the details of this help thread") + new SubcommandGroupData(CHANGE_SUBCOMMAND, "Change the details of this help thread") .addSubcommands(changeCategory, changeTitle); getData().addSubcommandGroups(changeCommands); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java index 4658fa916d..8b965ea316 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java @@ -10,7 +10,6 @@ import net.dv8tion.jda.api.exceptions.ErrorResponseException; import net.dv8tion.jda.api.requests.ErrorResponse; import net.dv8tion.jda.api.requests.RestAction; -import net.dv8tion.jda.api.requests.restaction.MessageCreateAction; import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder; import net.dv8tion.jda.api.utils.messages.MessageCreateData; import org.slf4j.Logger; @@ -120,7 +119,7 @@ private boolean handleIsNotOnCooldown(Message message) { : lastHelpThread.getAsMention(); MessageUtils.mentionSlashCommand(message.getGuild(), AskCommand.COMMAND_NAME) - .map(command -> message.getChannel() + .flatMap(command -> message.getChannel() .sendMessage(""" %s Please use %s to follow up on your question, \ or use %s to ask a new questions, thanks.""" @@ -186,14 +185,18 @@ private static RestAction sendInitialMessage(ThreadChannel threadChanne .build(); return MessageUtils - .mentionSlashCommand(originalMessage.getGuild(), ChangeHelpCategoryCommand.COMMAND_NAME) + .mentionSlashCommand(originalMessage.getGuild(), HelpThreadCommand.COMMAND_NAME, + HelpThreadCommand.CHANGE_SUBCOMMAND, + HelpThreadCommand.Subcommand.CHANGE_CATEGORY.getCommandName()) .flatMap(command -> { MessageCreateData threadMessage = new MessageCreateBuilder() - .setContent(""" - %s has a question about '**%s**' and will send the details now. + .setContent(""" + %s has a question about '**%s**' and will send the details now. - Please use %s to greatly increase the visibility of the question.""" - .formatted(author, title, command)).setEmbeds(embed).build(); + Please use %s to greatly increase the visibility of the question.""" + .formatted(author, title, command)) + .setEmbeds(embed) + .build(); return threadChannel.sendMessage(threadMessage); }); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java index 3ee8161e4c..c1ca02728f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java @@ -64,19 +64,21 @@ public static String escapeMarkdown(String text) { * in Discord. * * @param guild the {@link Guild} that contains the command - * @param command the command's name + * @param commandInput the command's name * @return Formatted string for the mentioned slash command * @throws IllegalArgumentException when the command name doesn't match with the guild's * commands */ - public static RestAction mentionSlashCommand(Guild guild, String command) { - return guild.retrieveCommands().map(commands -> { - Command foundCommand = commands.stream() - .filter(c -> c.getName().equalsIgnoreCase(command)) - .findFirst() - .orElseThrow(() -> new IllegalArgumentException( - "Command %s does not exist in guild %s".formatted(command, guild.getId()))); - return String.format("", command, foundCommand.getIdLong()); + public static RestAction mentionSlashCommand(Guild guild, String... commandInput) { + String commandWithOptions = String.join(" ", commandInput); + return guild.retrieveCommands().map(guildCommands -> { + Command guildCommand = guildCommands.stream() + .filter(c -> c.getName().equalsIgnoreCase(commandInput[0])) + .findAny() + .orElseThrow( + () -> new IllegalArgumentException("Command %s does not exist in guild %s" + .formatted(commandWithOptions, guild.getId()))); + return String.format("", commandWithOptions, guildCommand.getIdLong()); }); } From 31c8b1df1956a429dac4a0bfa3135bb9047430c2 Mon Sep 17 00:00:00 2001 From: Gyoo Date: Wed, 5 Oct 2022 14:13:15 +0200 Subject: [PATCH 05/14] Changed signature of MessageUtils.mentionSlashCommand --- .../tjbot/commands/utils/MessageUtils.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java index c1ca02728f..9409113b20 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java @@ -8,6 +8,7 @@ import net.dv8tion.jda.api.requests.RestAction; import net.dv8tion.jda.api.utils.MarkdownSanitizer; +import java.util.ArrayList; import java.util.List; /** @@ -64,21 +65,25 @@ public static String escapeMarkdown(String text) { * in Discord. * * @param guild the {@link Guild} that contains the command - * @param commandInput the command's name + * @param baseCommandName the command's name + * @param subcommands optional subcommands, depending on the base command used * @return Formatted string for the mentioned slash command * @throws IllegalArgumentException when the command name doesn't match with the guild's * commands */ - public static RestAction mentionSlashCommand(Guild guild, String... commandInput) { - String commandWithOptions = String.join(" ", commandInput); + public static RestAction mentionSlashCommand(Guild guild, String baseCommandName, + String... subcommands) { + List fullCommand = new ArrayList<>(List.of(baseCommandName)); + fullCommand.addAll(List.of(subcommands)); return guild.retrieveCommands().map(guildCommands -> { Command guildCommand = guildCommands.stream() - .filter(c -> c.getName().equalsIgnoreCase(commandInput[0])) + .filter(c -> c.getName().equalsIgnoreCase(baseCommandName)) .findAny() .orElseThrow( - () -> new IllegalArgumentException("Command %s does not exist in guild %s" - .formatted(commandWithOptions, guild.getId()))); - return String.format("", commandWithOptions, guildCommand.getIdLong()); + () -> new IllegalArgumentException("Command '%s' does not exist in guild %s" + .formatted(String.join(" ", fullCommand), guild.getId()))); + return String.format("", String.join(" ", fullCommand), + guildCommand.getIdLong()); }); } From aa631a590d925cc30a38abbee06feba9e8c38795 Mon Sep 17 00:00:00 2001 From: Gyoo Date: Tue, 11 Oct 2022 08:20:41 +0200 Subject: [PATCH 06/14] Use full command as single parameter in MessageUtils.mentionSlashCommand --- .../tjbot/commands/help/HelpSystemHelper.java | 13 ++++++------ .../commands/help/ImplicitAskListener.java | 6 +++--- .../tjbot/commands/utils/MessageUtils.java | 20 ++++++++----------- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java index 1f60fcbe73..8f38c30ca1 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java @@ -17,6 +17,7 @@ import net.dv8tion.jda.internal.requests.CompletedRestAction; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import org.togetherjava.tjbot.commands.utils.MessageUtils; import org.togetherjava.tjbot.config.Config; import org.togetherjava.tjbot.config.HelpSystemConfig; @@ -95,8 +96,9 @@ public HelpSystemHelper(JDA jda, Config config, Database database) { RestAction sendExplanationMessage(GuildMessageChannel threadChannel) { return MessageUtils - .mentionSlashCommand(threadChannel.getGuild(), HelpThreadCommand.COMMAND_NAME, - HelpThreadCommand.Subcommand.CLOSE.getCommandName()) + .mentionSlashCommand(threadChannel.getGuild(), + String.join(" ", HelpThreadCommand.COMMAND_NAME, + HelpThreadCommand.Subcommand.CLOSE.getCommandName())) .flatMap(closeCommandMention -> sendExplanationMessage(threadChannel, closeCommandMention)); } @@ -311,10 +313,9 @@ private void executeUncategorizedAdviceCheck(long threadChannelId, long authorId } // Still no category, send advice - return MessageUtils - .mentionSlashCommand(threadChannel.getGuild(), HelpThreadCommand.COMMAND_NAME, - HelpThreadCommand.CHANGE_SUBCOMMAND, - HelpThreadCommand.Subcommand.CHANGE_CATEGORY.getCommandName()) + return MessageUtils.mentionSlashCommand(threadChannel.getGuild(), + String.join(HelpThreadCommand.COMMAND_NAME, HelpThreadCommand.CHANGE_SUBCOMMAND, + HelpThreadCommand.Subcommand.CHANGE_CATEGORY.getCommandName())) .flatMap(command -> { MessageEmbed embed = HelpSystemHelper.embedWith( """ diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java index 8b965ea316..91c6a39a44 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java @@ -185,9 +185,9 @@ private static RestAction sendInitialMessage(ThreadChannel threadChanne .build(); return MessageUtils - .mentionSlashCommand(originalMessage.getGuild(), HelpThreadCommand.COMMAND_NAME, - HelpThreadCommand.CHANGE_SUBCOMMAND, - HelpThreadCommand.Subcommand.CHANGE_CATEGORY.getCommandName()) + .mentionSlashCommand(originalMessage.getGuild(), + String.join(HelpThreadCommand.COMMAND_NAME, HelpThreadCommand.CHANGE_SUBCOMMAND, + HelpThreadCommand.Subcommand.CHANGE_CATEGORY.getCommandName())) .flatMap(command -> { MessageCreateData threadMessage = new MessageCreateBuilder() .setContent(""" diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java index 9409113b20..37cbdb0929 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java @@ -7,8 +7,8 @@ import net.dv8tion.jda.api.interactions.components.buttons.Button; import net.dv8tion.jda.api.requests.RestAction; import net.dv8tion.jda.api.utils.MarkdownSanitizer; +import org.jetbrains.annotations.NotNull; -import java.util.ArrayList; import java.util.List; /** @@ -65,24 +65,20 @@ public static String escapeMarkdown(String text) { * in Discord. * * @param guild the {@link Guild} that contains the command - * @param baseCommandName the command's name - * @param subcommands optional subcommands, depending on the base command used + * @param fullCommand the command's name with its optional subcommand * @return Formatted string for the mentioned slash command - * @throws IllegalArgumentException when the command name doesn't match with the guild's - * commands + * @throws IllegalArgumentException when the command isn't found in the guild */ - public static RestAction mentionSlashCommand(Guild guild, String baseCommandName, - String... subcommands) { - List fullCommand = new ArrayList<>(List.of(baseCommandName)); - fullCommand.addAll(List.of(subcommands)); + public static RestAction mentionSlashCommand(Guild guild, @NotNull String fullCommand) { + List fullCommandSplit = List.of(fullCommand.split(" ")); return guild.retrieveCommands().map(guildCommands -> { Command guildCommand = guildCommands.stream() - .filter(c -> c.getName().equalsIgnoreCase(baseCommandName)) + .filter(c -> c.getName().equalsIgnoreCase(fullCommandSplit.get(0))) .findAny() .orElseThrow( () -> new IllegalArgumentException("Command '%s' does not exist in guild %s" - .formatted(String.join(" ", fullCommand), guild.getId()))); - return String.format("", String.join(" ", fullCommand), + .formatted(fullCommandSplit.get(0), guild.getId()))); + return String.format("", String.join(" ", fullCommandSplit), guildCommand.getIdLong()); }); } From 768208e10b9fc7a35bbfea89a8bd6c5b7c4681da Mon Sep 17 00:00:00 2001 From: Gyoo Date: Tue, 11 Oct 2022 09:34:43 +0200 Subject: [PATCH 07/14] MessageUtils.mentionSlashCommand : use only first element of commandPath split --- .../tjbot/commands/utils/MessageUtils.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java index 37cbdb0929..0fa11b39f4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java @@ -7,7 +7,6 @@ import net.dv8tion.jda.api.interactions.components.buttons.Button; import net.dv8tion.jda.api.requests.RestAction; import net.dv8tion.jda.api.utils.MarkdownSanitizer; -import org.jetbrains.annotations.NotNull; import java.util.List; @@ -65,21 +64,20 @@ public static String escapeMarkdown(String text) { * in Discord. * * @param guild the {@link Guild} that contains the command - * @param fullCommand the command's name with its optional subcommand + * @param commandPath the command's name with its optional subcommand * @return Formatted string for the mentioned slash command * @throws IllegalArgumentException when the command isn't found in the guild */ - public static RestAction mentionSlashCommand(Guild guild, @NotNull String fullCommand) { - List fullCommandSplit = List.of(fullCommand.split(" ")); + public static RestAction mentionSlashCommand(Guild guild, String commandPath) { + String commandName = commandPath.split(" ", 1)[0]; return guild.retrieveCommands().map(guildCommands -> { Command guildCommand = guildCommands.stream() - .filter(c -> c.getName().equalsIgnoreCase(fullCommandSplit.get(0))) + .filter(c -> c.getName().equalsIgnoreCase(commandName)) .findAny() .orElseThrow( () -> new IllegalArgumentException("Command '%s' does not exist in guild %s" - .formatted(fullCommandSplit.get(0), guild.getId()))); - return String.format("", String.join(" ", fullCommandSplit), - guildCommand.getIdLong()); + .formatted(commandName, guild.getId()))); + return String.format("", commandPath, guildCommand.getId()); }); } From e8ae029b9e08680df87d45d0d6da690a920666fd Mon Sep 17 00:00:00 2001 From: Gyoo Date: Tue, 11 Oct 2022 10:25:37 +0200 Subject: [PATCH 08/14] MessageUtils.mentionSlashCommand : split limit should be 2 instead of 1 --- .../org/togetherjava/tjbot/commands/utils/MessageUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java index 0fa11b39f4..c8c616a877 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java @@ -69,7 +69,7 @@ public static String escapeMarkdown(String text) { * @throws IllegalArgumentException when the command isn't found in the guild */ public static RestAction mentionSlashCommand(Guild guild, String commandPath) { - String commandName = commandPath.split(" ", 1)[0]; + String commandName = commandPath.split(" ", 2)[0]; return guild.retrieveCommands().map(guildCommands -> { Command guildCommand = guildCommands.stream() .filter(c -> c.getName().equalsIgnoreCase(commandName)) From 88726c55b94c9c81d4ba63ff22d283990726be46 Mon Sep 17 00:00:00 2001 From: Gyoo Date: Tue, 11 Oct 2022 10:36:28 +0200 Subject: [PATCH 09/14] Update application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java Co-authored-by: Tanish Azad <73871477+Taz03@users.noreply.github.com> --- .../org/togetherjava/tjbot/commands/utils/MessageUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java index c8c616a877..106b1f5709 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java @@ -64,7 +64,7 @@ public static String escapeMarkdown(String text) { * in Discord. * * @param guild the {@link Guild} that contains the command - * @param commandPath the command's name with its optional subcommand + * @param commandPath full command path with subcommand groups and subcommands * @return Formatted string for the mentioned slash command * @throws IllegalArgumentException when the command isn't found in the guild */ From 1f95b935185ce4bba4aa5f573ccd2284dec4cc0d Mon Sep 17 00:00:00 2001 From: Gyoo Date: Tue, 11 Oct 2022 10:55:25 +0200 Subject: [PATCH 10/14] Missing delimiters for String.join --- .../org/togetherjava/tjbot/commands/help/HelpSystemHelper.java | 2 +- .../togetherjava/tjbot/commands/help/ImplicitAskListener.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java index 8f38c30ca1..d999f707b3 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java @@ -314,7 +314,7 @@ private void executeUncategorizedAdviceCheck(long threadChannelId, long authorId // Still no category, send advice return MessageUtils.mentionSlashCommand(threadChannel.getGuild(), - String.join(HelpThreadCommand.COMMAND_NAME, HelpThreadCommand.CHANGE_SUBCOMMAND, + String.join(" ", HelpThreadCommand.COMMAND_NAME, HelpThreadCommand.CHANGE_SUBCOMMAND, HelpThreadCommand.Subcommand.CHANGE_CATEGORY.getCommandName())) .flatMap(command -> { MessageEmbed embed = HelpSystemHelper.embedWith( diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java index 91c6a39a44..be94e7e161 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java @@ -186,7 +186,7 @@ private static RestAction sendInitialMessage(ThreadChannel threadChanne return MessageUtils .mentionSlashCommand(originalMessage.getGuild(), - String.join(HelpThreadCommand.COMMAND_NAME, HelpThreadCommand.CHANGE_SUBCOMMAND, + String.join(" ", HelpThreadCommand.COMMAND_NAME, HelpThreadCommand.CHANGE_SUBCOMMAND, HelpThreadCommand.Subcommand.CHANGE_CATEGORY.getCommandName())) .flatMap(command -> { MessageCreateData threadMessage = new MessageCreateBuilder() From 1992fd01241d8d7d0fcf87e4bac30f9e64fb48bb Mon Sep 17 00:00:00 2001 From: Gyoo Date: Tue, 11 Oct 2022 11:15:10 +0200 Subject: [PATCH 11/14] Switch back to varargs for mentionSlashCommand --- .../tjbot/commands/help/HelpSystemHelper.java | 8 ++++---- .../tjbot/commands/help/HelpThreadCommand.java | 4 ++-- .../tjbot/commands/help/ImplicitAskListener.java | 4 ++-- .../togetherjava/tjbot/commands/utils/MessageUtils.java | 6 +++--- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java index d999f707b3..169429c122 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java @@ -97,8 +97,8 @@ public HelpSystemHelper(JDA jda, Config config, Database database) { RestAction sendExplanationMessage(GuildMessageChannel threadChannel) { return MessageUtils .mentionSlashCommand(threadChannel.getGuild(), - String.join(" ", HelpThreadCommand.COMMAND_NAME, - HelpThreadCommand.Subcommand.CLOSE.getCommandName())) + HelpThreadCommand.COMMAND_NAME, + HelpThreadCommand.Subcommand.CLOSE.getCommandName()) .flatMap(closeCommandMention -> sendExplanationMessage(threadChannel, closeCommandMention)); } @@ -314,8 +314,8 @@ private void executeUncategorizedAdviceCheck(long threadChannelId, long authorId // Still no category, send advice return MessageUtils.mentionSlashCommand(threadChannel.getGuild(), - String.join(" ", HelpThreadCommand.COMMAND_NAME, HelpThreadCommand.CHANGE_SUBCOMMAND, - HelpThreadCommand.Subcommand.CHANGE_CATEGORY.getCommandName())) + HelpThreadCommand.COMMAND_NAME, HelpThreadCommand.CHANGE_SUBCOMMAND_GROUP, + HelpThreadCommand.Subcommand.CHANGE_CATEGORY.getCommandName()) .flatMap(command -> { MessageEmbed embed = HelpSystemHelper.embedWith( """ diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadCommand.java index 36bd4dfd74..be4bc0a7c3 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadCommand.java @@ -45,7 +45,7 @@ public final class HelpThreadCommand extends SlashCommandAdapter { private static final String CHANGE_CATEGORY_OPTION = "category"; private static final String CHANGE_TITLE_OPTION = "title"; private static final String CHANGE_TITLE_SUBCOMMAND = "title"; - public static final String CHANGE_SUBCOMMAND = "change"; + public static final String CHANGE_SUBCOMMAND_GROUP = "change"; public static final String COMMAND_NAME = "help-thread"; private final HelpSystemHelper helper; @@ -75,7 +75,7 @@ public HelpThreadCommand(Config config, HelpSystemHelper helper) { .addOption(OptionType.STRING, CHANGE_TITLE_OPTION, "new title", true); SubcommandGroupData changeCommands = - new SubcommandGroupData(CHANGE_SUBCOMMAND, "Change the details of this help thread") + new SubcommandGroupData(CHANGE_SUBCOMMAND_GROUP, "Change the details of this help thread") .addSubcommands(changeCategory, changeTitle); getData().addSubcommandGroups(changeCommands); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java index be94e7e161..27293a8f72 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java @@ -186,8 +186,8 @@ private static RestAction sendInitialMessage(ThreadChannel threadChanne return MessageUtils .mentionSlashCommand(originalMessage.getGuild(), - String.join(" ", HelpThreadCommand.COMMAND_NAME, HelpThreadCommand.CHANGE_SUBCOMMAND, - HelpThreadCommand.Subcommand.CHANGE_CATEGORY.getCommandName())) + HelpThreadCommand.COMMAND_NAME, HelpThreadCommand.CHANGE_SUBCOMMAND_GROUP, + HelpThreadCommand.Subcommand.CHANGE_CATEGORY.getCommandName()) .flatMap(command -> { MessageCreateData threadMessage = new MessageCreateBuilder() .setContent(""" diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java index 106b1f5709..cda6abc94f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java @@ -68,8 +68,8 @@ public static String escapeMarkdown(String text) { * @return Formatted string for the mentioned slash command * @throws IllegalArgumentException when the command isn't found in the guild */ - public static RestAction mentionSlashCommand(Guild guild, String commandPath) { - String commandName = commandPath.split(" ", 2)[0]; + public static RestAction mentionSlashCommand(Guild guild, String... commandPath) { + String commandName = commandPath[0]; return guild.retrieveCommands().map(guildCommands -> { Command guildCommand = guildCommands.stream() .filter(c -> c.getName().equalsIgnoreCase(commandName)) @@ -77,7 +77,7 @@ public static RestAction mentionSlashCommand(Guild guild, String command .orElseThrow( () -> new IllegalArgumentException("Command '%s' does not exist in guild %s" .formatted(commandName, guild.getId()))); - return String.format("", commandPath, guildCommand.getId()); + return String.format("", String.join(" ", commandPath), guildCommand.getId()); }); } From 9ac1a6ad2c195ad11165a339ef3aa1a186fe4af2 Mon Sep 17 00:00:00 2001 From: Gyoo Date: Tue, 11 Oct 2022 11:33:19 +0200 Subject: [PATCH 12/14] MessageUtils.mentionSlashCommand: varargs for subcommands only --- .../tjbot/commands/help/HelpSystemHelper.java | 12 ++++++------ .../tjbot/commands/help/HelpThreadCommand.java | 6 +++--- .../tjbot/commands/help/ImplicitAskListener.java | 6 +++--- .../tjbot/commands/utils/MessageUtils.java | 11 ++++++++--- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java index 169429c122..4dce9512e0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java @@ -96,9 +96,8 @@ public HelpSystemHelper(JDA jda, Config config, Database database) { RestAction sendExplanationMessage(GuildMessageChannel threadChannel) { return MessageUtils - .mentionSlashCommand(threadChannel.getGuild(), - HelpThreadCommand.COMMAND_NAME, - HelpThreadCommand.Subcommand.CLOSE.getCommandName()) + .mentionSlashCommand(threadChannel.getGuild(), HelpThreadCommand.COMMAND_NAME, + HelpThreadCommand.Subcommand.CLOSE.getCommandName()) .flatMap(closeCommandMention -> sendExplanationMessage(threadChannel, closeCommandMention)); } @@ -313,9 +312,10 @@ private void executeUncategorizedAdviceCheck(long threadChannelId, long authorId } // Still no category, send advice - return MessageUtils.mentionSlashCommand(threadChannel.getGuild(), - HelpThreadCommand.COMMAND_NAME, HelpThreadCommand.CHANGE_SUBCOMMAND_GROUP, - HelpThreadCommand.Subcommand.CHANGE_CATEGORY.getCommandName()) + return MessageUtils + .mentionSlashCommand(threadChannel.getGuild(), HelpThreadCommand.COMMAND_NAME, + HelpThreadCommand.CHANGE_SUBCOMMAND_GROUP, + HelpThreadCommand.Subcommand.CHANGE_CATEGORY.getCommandName()) .flatMap(command -> { MessageEmbed embed = HelpSystemHelper.embedWith( """ diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadCommand.java index be4bc0a7c3..b4e52339f6 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/HelpThreadCommand.java @@ -74,9 +74,9 @@ public HelpThreadCommand(Config config, HelpSystemHelper helper) { SubcommandData changeTitle = Subcommand.CHANGE_TITLE.toSubcommandData() .addOption(OptionType.STRING, CHANGE_TITLE_OPTION, "new title", true); - SubcommandGroupData changeCommands = - new SubcommandGroupData(CHANGE_SUBCOMMAND_GROUP, "Change the details of this help thread") - .addSubcommands(changeCategory, changeTitle); + SubcommandGroupData changeCommands = new SubcommandGroupData(CHANGE_SUBCOMMAND_GROUP, + "Change the details of this help thread").addSubcommands(changeCategory, + changeTitle); getData().addSubcommandGroups(changeCommands); getData().addSubcommands(Subcommand.CLOSE.toSubcommandData()); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java b/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java index 27293a8f72..3b550cb0b0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java @@ -185,9 +185,9 @@ private static RestAction sendInitialMessage(ThreadChannel threadChanne .build(); return MessageUtils - .mentionSlashCommand(originalMessage.getGuild(), - HelpThreadCommand.COMMAND_NAME, HelpThreadCommand.CHANGE_SUBCOMMAND_GROUP, - HelpThreadCommand.Subcommand.CHANGE_CATEGORY.getCommandName()) + .mentionSlashCommand(originalMessage.getGuild(), HelpThreadCommand.COMMAND_NAME, + HelpThreadCommand.CHANGE_SUBCOMMAND_GROUP, + HelpThreadCommand.Subcommand.CHANGE_CATEGORY.getCommandName()) .flatMap(command -> { MessageCreateData threadMessage = new MessageCreateBuilder() .setContent(""" diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java index cda6abc94f..90bd33a31c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java @@ -64,12 +64,13 @@ public static String escapeMarkdown(String text) { * in Discord. * * @param guild the {@link Guild} that contains the command - * @param commandPath full command path with subcommand groups and subcommands + * @param commandName the command's name + * @param subCommands optional subcommands, depending on the base command used * @return Formatted string for the mentioned slash command * @throws IllegalArgumentException when the command isn't found in the guild */ - public static RestAction mentionSlashCommand(Guild guild, String... commandPath) { - String commandName = commandPath[0]; + public static RestAction mentionSlashCommand(Guild guild, String commandName, + String... subCommands) { return guild.retrieveCommands().map(guildCommands -> { Command guildCommand = guildCommands.stream() .filter(c -> c.getName().equalsIgnoreCase(commandName)) @@ -77,6 +78,10 @@ public static RestAction mentionSlashCommand(Guild guild, String... comm .orElseThrow( () -> new IllegalArgumentException("Command '%s' does not exist in guild %s" .formatted(commandName, guild.getId()))); + String commandPath = commandName; + if (subCommands.length > 0) { + commandPath = commandPath + " " + String.join(" ", subCommands); + } return String.format("", String.join(" ", commandPath), guildCommand.getId()); }); } From b24ddaa633bdbc825a4d8abc8a1d12a173891300 Mon Sep 17 00:00:00 2001 From: Gyoo Date: Tue, 11 Oct 2022 12:13:13 +0200 Subject: [PATCH 13/14] MessageUtils.mentionSlashCommand: Use += for string concat --- .../org/togetherjava/tjbot/commands/utils/MessageUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java index 90bd33a31c..7d0b8ed678 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java @@ -80,7 +80,7 @@ public static RestAction mentionSlashCommand(Guild guild, String command .formatted(commandName, guild.getId()))); String commandPath = commandName; if (subCommands.length > 0) { - commandPath = commandPath + " " + String.join(" ", subCommands); + commandPath += " " + String.join(" ", subCommands); } return String.format("", String.join(" ", commandPath), guildCommand.getId()); }); From 13154db84d15347cbf75741ac3259cd7e8b4c236 Mon Sep 17 00:00:00 2001 From: Gyoo Date: Tue, 11 Oct 2022 13:12:42 +0200 Subject: [PATCH 14/14] Update application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java Co-authored-by: Tais993 <49957334+Tais993@users.noreply.github.com> --- .../org/togetherjava/tjbot/commands/utils/MessageUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java index 7d0b8ed678..978d8843c4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/utils/MessageUtils.java @@ -65,7 +65,7 @@ public static String escapeMarkdown(String text) { * * @param guild the {@link Guild} that contains the command * @param commandName the command's name - * @param subCommands optional subcommands, depending on the base command used + * @param subCommands optional subcommand group & subcommand, depending on the base command used * @return Formatted string for the mentioned slash command * @throws IllegalArgumentException when the command isn't found in the guild */