diff --git a/application/src/main/java/org/togetherjava/tjbot/features/Features.java b/application/src/main/java/org/togetherjava/tjbot/features/Features.java index 893adbc00f..12f8f1b9bf 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/Features.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/Features.java @@ -23,16 +23,7 @@ import org.togetherjava.tjbot.features.filesharing.FileSharingMessageListener; import org.togetherjava.tjbot.features.github.GitHubCommand; import org.togetherjava.tjbot.features.github.GitHubReference; -import org.togetherjava.tjbot.features.help.GuildLeaveCloseThreadListener; -import org.togetherjava.tjbot.features.help.HelpSystemHelper; -import org.togetherjava.tjbot.features.help.HelpThreadActivityUpdater; -import org.togetherjava.tjbot.features.help.HelpThreadAutoArchiver; -import org.togetherjava.tjbot.features.help.HelpThreadCommand; -import org.togetherjava.tjbot.features.help.HelpThreadCreatedListener; -import org.togetherjava.tjbot.features.help.HelpThreadLifecycleListener; -import org.togetherjava.tjbot.features.help.HelpThreadMetadataPurger; -import org.togetherjava.tjbot.features.help.MarkHelpThreadCloseInDBRoutine; -import org.togetherjava.tjbot.features.help.PinnedNotificationRemover; +import org.togetherjava.tjbot.features.help.*; import org.togetherjava.tjbot.features.javamail.RSSHandlerRoutine; import org.togetherjava.tjbot.features.jshell.JShellCommand; import org.togetherjava.tjbot.features.jshell.JShellEval; @@ -160,6 +151,7 @@ public static Collection createFeatures(JDA jda, Database database, Con // Message context commands features.add(new TransferQuestionCommand(config, chatGptService)); + features.add(new PinAnswerCommand()); // User context commands diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/PinAnswerCommand.java b/application/src/main/java/org/togetherjava/tjbot/features/help/PinAnswerCommand.java new file mode 100644 index 0000000000..af4d836414 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/PinAnswerCommand.java @@ -0,0 +1,73 @@ +package org.togetherjava.tjbot.features.help; + +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; +import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent; +import net.dv8tion.jda.api.interactions.commands.build.Commands; + +import org.togetherjava.tjbot.features.BotCommandAdapter; +import org.togetherjava.tjbot.features.CommandVisibility; +import org.togetherjava.tjbot.features.MessageContextCommand; + +public final class PinAnswerCommand extends BotCommandAdapter implements MessageContextCommand { + private static final String COMMAND_NAME = "pin-answer"; + private static final int MAX_PINNED_ANSWERS = 10; + private int count = 0; + + public PinAnswerCommand() { + super(Commands.message(COMMAND_NAME), CommandVisibility.GUILD); + } + + @Override + public void onMessageContext(MessageContextInteractionEvent event) { + Message originalMessage = event.getTarget(); + User commandInvoker = event.getUser(); + + if (!(originalMessage.getChannel() instanceof ThreadChannel threadChannel)) { + replyNotInThread(event); + return; + } + + if (!isThreadCreator(commandInvoker, threadChannel)) { + replyNotThreadCreator(event); + return; + } + + if (count >= MAX_PINNED_ANSWERS) { + replyMaxPinsReached(event); + return; + } + + pinMessage(event, originalMessage); + } + + private boolean isThreadCreator(User user, ThreadChannel thread) { + return user.getIdLong() == thread.getOwnerIdLong(); + } + + private void pinMessage(MessageContextInteractionEvent event, Message message) { + message.pin().queue(success -> { + count++; + event.reply("Answer pinned successfully! Pinned answers: " + count) + .setEphemeral(true) + .queue(); + }, failure -> event.reply("Failed to pin the answer.").setEphemeral(true).queue()); + } + + private void replyNotInThread(MessageContextInteractionEvent event) { + event.reply("This message is not in a thread.").setEphemeral(true).queue(); + } + + private void replyNotThreadCreator(MessageContextInteractionEvent event) { + event.reply("You are not the thread creator and cannot pin answers here.") + .setEphemeral(true) + .queue(); + } + + private void replyMaxPinsReached(MessageContextInteractionEvent event) { + event.reply("Maximum pinned answers (" + MAX_PINNED_ANSWERS + ") reached.") + .setEphemeral(true) + .queue(); + } +}