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..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; 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..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 @@ -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; @@ -17,6 +18,7 @@ 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 +94,16 @@ public HelpSystemHelper(JDA jda, Config config, Database database) { categoryRoleSuffix = helpConfig.getCategoryRoleSuffix(); } - RestAction sendExplanationMessage(MessageChannel threadChannel) { + RestAction sendExplanationMessage(GuildMessageChannel threadChannel) { + return MessageUtils + .mentionSlashCommand(threadChannel.getGuild(), HelpThreadCommand.COMMAND_NAME, + HelpThreadCommand.Subcommand.CLOSE.getCommandName()) + .flatMap(closeCommandMention -> sendExplanationMessage(threadChannel, + closeCommandMention)); + } + + private RestAction sendExplanationMessage(GuildMessageChannel threadChannel, + String closeCommandMention) { boolean useCodeSyntaxExampleImage = true; InputStream codeSyntaxExampleData = AskCommand.class.getResourceAsStream("/" + CODE_SYNTAX_EXAMPLE_PATH); @@ -117,7 +128,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 when your question has been answered, thanks." + .formatted(closeCommandMention))); MessageCreateAction action = threadChannel.sendMessage(message); if (useCodeSyntaxExampleImage) { @@ -300,16 +312,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_GROUP, + 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..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 @@ -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_GROUP = "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); @@ -72,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", "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 17a49dbf64..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 @@ -10,13 +10,13 @@ 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; 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; @@ -118,13 +118,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 `/ask` to ask a new questions, thanks.""" - .formatted(author.getAsMention(), threadDescription)) - .flatMap(any -> message.delete()) + MessageUtils.mentionSlashCommand(message.getGuild(), AskCommand.COMMAND_NAME) + .flatMap(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; } @@ -171,7 +173,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(); @@ -182,27 +184,34 @@ private static MessageCreateAction sendInitialMessage(ThreadChannel threadChanne .setColor(HelpSystemHelper.AMBIENT_COLOR) .build(); - MessageCreateData threadMessage = new MessageCreateBuilder() - .setContent( - """ + return MessageUtils + .mentionSlashCommand(originalMessage.getGuild(), HelpThreadCommand.COMMAND_NAME, + HelpThreadCommand.CHANGE_SUBCOMMAND_GROUP, + HelpThreadCommand.Subcommand.CHANGE_CATEGORY.getCommandName()) + .flatMap(command -> { + MessageCreateData threadMessage = new MessageCreateBuilder() + .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 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 `/ask` to ask questions. Don't worry though, I created %s for you. \ - Please continue there, thanks.""" - .formatted(message.getAuthor().getAsMention(), - 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) { 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..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 @@ -1,8 +1,11 @@ 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.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 java.util.List; @@ -56,4 +59,31 @@ 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 the {@link Guild} that contains the command + * @param commandName the command's name + * @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 + */ + 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)) + .findAny() + .orElseThrow( + () -> new IllegalArgumentException("Command '%s' does not exist in guild %s" + .formatted(commandName, guild.getId()))); + String commandPath = commandName; + if (subCommands.length > 0) { + commandPath += " " + String.join(" ", subCommands); + } + return String.format("", String.join(" ", commandPath), guildCommand.getId()); + }); + } + }