Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -92,7 +94,16 @@ public HelpSystemHelper(JDA jda, Config config, Database database) {
categoryRoleSuffix = helpConfig.getCategoryRoleSuffix();
}

RestAction<Message> sendExplanationMessage(MessageChannel threadChannel) {
RestAction<Message> sendExplanationMessage(GuildMessageChannel threadChannel) {
return MessageUtils
.mentionSlashCommand(threadChannel.getGuild(), HelpThreadCommand.COMMAND_NAME,
HelpThreadCommand.Subcommand.CLOSE.getCommandName())
.flatMap(closeCommandMention -> sendExplanationMessage(threadChannel,
closeCommandMention));
}

private RestAction<Message> sendExplanationMessage(GuildMessageChannel threadChannel,
String closeCommandMention) {
boolean useCodeSyntaxExampleImage = true;
InputStream codeSyntaxExampleData =
AskCommand.class.getResourceAsStream("/" + CODE_SYNTAX_EXAMPLE_PATH);
Expand All @@ -117,7 +128,8 @@ RestAction<Message> 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) {
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Subcommand> nameToSubcommand;
Expand All @@ -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);
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -171,7 +173,7 @@ private RestAction<?> handleEvent(ThreadChannel threadChannel, Message message,
author.getIdLong()));
}

private static MessageCreateAction sendInitialMessage(ThreadChannel threadChannel,
private static RestAction<Message> sendInitialMessage(ThreadChannel threadChannel,
Message originalMessage, String title) {
String content = originalMessage.getContentRaw();
Member author = originalMessage.getMember();
Expand All @@ -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<Message> 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<String> 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("</%s:%s>", String.join(" ", commandPath), guildCommand.getId());
});
}

}