From 1348b6df8bc922bd86334aa3d309f6d8c5aecaff Mon Sep 17 00:00:00 2001 From: Andrew1031 Date: Thu, 24 Oct 2024 13:13:17 -0700 Subject: [PATCH 1/5] PinAnswerCommand file for pinning messages --- .../togetherjava/tjbot/features/Features.java | 12 ++----- .../tjbot/features/help/PinAnswerCommand.java | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 application/src/main/java/org/togetherjava/tjbot/features/help/PinAnswerCommand.java 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..dd8fcccfb0 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/PinAnswerCommand.java @@ -0,0 +1,36 @@ +package org.togetherjava.tjbot.features.help; + +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent; +import net.dv8tion.jda.api.interactions.commands.build.Commands; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +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 Logger logger = LoggerFactory.getLogger(PinAnswerCommand.class); + + /** + * Creates a new instance. + * + */ + public PinAnswerCommand() { + super(Commands.message(COMMAND_NAME), CommandVisibility.GUILD); + + + } + + @Override + public void onMessageContext(MessageContextInteractionEvent event) { + Message originalMessage = event.getTarget(); + originalMessage.pin() + .queue(success -> logger.debug("Message pinned successfully!"), + failure -> logger.debug(failure.getMessage())); + + } +} From 8f02c0081b0ab47848311526c49549ba7d222286 Mon Sep 17 00:00:00 2001 From: Andrew1031 Date: Sat, 26 Oct 2024 01:24:31 -0700 Subject: [PATCH 2/5] Got pin context working with no errors --- .../tjbot/features/help/PinAnswerCommand.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) 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 index dd8fcccfb0..de5a01befc 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/PinAnswerCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/PinAnswerCommand.java @@ -3,8 +3,6 @@ import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent; import net.dv8tion.jda.api.interactions.commands.build.Commands; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.features.BotCommandAdapter; import org.togetherjava.tjbot.features.CommandVisibility; @@ -13,7 +11,6 @@ public final class PinAnswerCommand extends BotCommandAdapter implements MessageContextCommand { private static final String COMMAND_NAME = "pin-answer"; - private static final Logger logger = LoggerFactory.getLogger(PinAnswerCommand.class); /** * Creates a new instance. @@ -29,8 +26,10 @@ public PinAnswerCommand() { public void onMessageContext(MessageContextInteractionEvent event) { Message originalMessage = event.getTarget(); originalMessage.pin() - .queue(success -> logger.debug("Message pinned successfully!"), - failure -> logger.debug(failure.getMessage())); - + .queue(success -> event.reply("Answer pinned successfully!").setEphemeral(true).queue(), + failure -> event.reply("Failed to pin the message.") + .setEphemeral(true) + .queue()); } + } From 717931c947530638a95b58ace1cbcf054b25f22c Mon Sep 17 00:00:00 2001 From: Andrew1031 Date: Sun, 3 Nov 2024 12:42:26 -0800 Subject: [PATCH 3/5] Working solution for all the requirements --- .../tjbot/features/help/PinAnswerCommand.java | 34 +++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) 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 index de5a01befc..8633978c07 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/PinAnswerCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/PinAnswerCommand.java @@ -1,6 +1,8 @@ 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; @@ -11,6 +13,8 @@ public final class PinAnswerCommand extends BotCommandAdapter implements MessageContextCommand { private static final String COMMAND_NAME = "pin-answer"; + private static final int MAX_PINNED_ANSWERS = 3; + private int count = 0; /** * Creates a new instance. @@ -18,18 +22,36 @@ public final class PinAnswerCommand extends BotCommandAdapter implements Message */ public PinAnswerCommand() { super(Commands.message(COMMAND_NAME), CommandVisibility.GUILD); - - } @Override public void onMessageContext(MessageContextInteractionEvent event) { Message originalMessage = event.getTarget(); - originalMessage.pin() - .queue(success -> event.reply("Answer pinned successfully!").setEphemeral(true).queue(), - failure -> event.reply("Failed to pin the message.") + User commandInvoker = event.getUser(); + + if (originalMessage.getChannel() instanceof ThreadChannel threadChannel) { + if (commandInvoker.getIdLong() == threadChannel.getOwnerIdLong()) { + if (count < MAX_PINNED_ANSWERS) { + originalMessage.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()); + } else { + event.reply("Maximum pinned answers (" + count + ") reached") + .setEphemeral(true) + .queue(); + } + } else { + event.reply("You are not the thread creator and cannot pin answers here.") + .setEphemeral(true) + .queue(); + } + } else { + event.reply("This message is not in a thread.").setEphemeral(true).queue(); + } } - } From 2499044897965e010025339acc51fcab8c85c5c1 Mon Sep 17 00:00:00 2001 From: Andrew1031 Date: Sun, 3 Nov 2024 12:47:08 -0800 Subject: [PATCH 4/5] Cleaned up code --- .../tjbot/features/help/PinAnswerCommand.java | 72 +++++++++++-------- 1 file changed, 44 insertions(+), 28 deletions(-) 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 index 8633978c07..e7c1708c73 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/PinAnswerCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/PinAnswerCommand.java @@ -10,16 +10,11 @@ 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 = 3; private int count = 0; - /** - * Creates a new instance. - * - */ public PinAnswerCommand() { super(Commands.message(COMMAND_NAME), CommandVisibility.GUILD); } @@ -29,29 +24,50 @@ public void onMessageContext(MessageContextInteractionEvent event) { Message originalMessage = event.getTarget(); User commandInvoker = event.getUser(); - if (originalMessage.getChannel() instanceof ThreadChannel threadChannel) { - if (commandInvoker.getIdLong() == threadChannel.getOwnerIdLong()) { - if (count < MAX_PINNED_ANSWERS) { - originalMessage.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()); - } else { - event.reply("Maximum pinned answers (" + count + ") reached") - .setEphemeral(true) - .queue(); - } - } else { - event.reply("You are not the thread creator and cannot pin answers here.") - .setEphemeral(true) - .queue(); - } - } else { - event.reply("This message is not in a thread.").setEphemeral(true).queue(); + 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(); } } From 5b1f2c934c061f6286b45f6b4f668656567503f1 Mon Sep 17 00:00:00 2001 From: Andrew1031 Date: Sun, 3 Nov 2024 12:49:45 -0800 Subject: [PATCH 5/5] Change max pinned answers from 3 to 10 --- .../org/togetherjava/tjbot/features/help/PinAnswerCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index e7c1708c73..af4d836414 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/PinAnswerCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/PinAnswerCommand.java @@ -12,7 +12,7 @@ public final class PinAnswerCommand extends BotCommandAdapter implements MessageContextCommand { private static final String COMMAND_NAME = "pin-answer"; - private static final int MAX_PINNED_ANSWERS = 3; + private static final int MAX_PINNED_ANSWERS = 10; private int count = 0; public PinAnswerCommand() {