Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

context command to pin messages in respective thread #1188 #1194

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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 @@ -32,6 +32,7 @@
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.PinAnswerCommand;
import org.togetherjava.tjbot.features.help.PinnedNotificationRemover;
import org.togetherjava.tjbot.features.javamail.RSSHandlerRoutine;
import org.togetherjava.tjbot.features.jshell.JShellCommand;
Expand Down Expand Up @@ -160,6 +161,7 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con

// Message context commands
features.add(new TransferQuestionCommand(config, chatGptService));
features.add(new PinAnswerCommand(config));

// User context commands

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
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.ForumChannel;
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.config.Config;
import org.togetherjava.tjbot.config.HelpSystemConfig;
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 final String helpForumPattern;

public PinAnswerCommand(Config config) {
super(Commands.message(COMMAND_NAME), CommandVisibility.GUILD);
HelpSystemConfig helpConfig = config.getHelpSystem();
helpForumPattern = helpConfig.getHelpForumPattern();
}

@Override
public void onMessageContext(MessageContextInteractionEvent event) {
Message originalMessage = event.getTarget();
User commandInvoker = event.getUser();

if (!(originalMessage.getChannel() instanceof ThreadChannel threadChannel)) {
replyNotInThread(event);
return;
}

if (!(threadChannel.getParentChannel() instanceof ForumChannel forumChannel)
|| !forumChannel.getName().equalsIgnoreCase(helpForumPattern)) {
andrew-09 marked this conversation as resolved.
Show resolved Hide resolved
andrew-09 marked this conversation as resolved.
Show resolved Hide resolved
replyNotInThread(event);
return;
}

if (!threadOwner(commandInvoker, threadChannel)) {
replyNotThreadOwner(event);
return;
}

threadChannel.retrievePinnedMessages().queue(pinnedMessages -> {
if (pinnedMessages.size() >= MAX_PINNED_ANSWERS) {
replyMaxPinsReached(event);
} else {
pinMessage(event, originalMessage);
}
});
}

private boolean threadOwner(User user, ThreadChannel thread) {
return user.getIdLong() == thread.getOwnerIdLong();
}

private void pinMessage(MessageContextInteractionEvent event, Message message) {
message.pin()
.queue(success -> event.reply("Answer pinned successfully!").setEphemeral(true).queue(),
failure -> event.reply("Failed to pin the answer.").setEphemeral(true).queue());
andrew-09 marked this conversation as resolved.
Show resolved Hide resolved
}

private void replyNotInThread(MessageContextInteractionEvent event) {
event.reply("This command can only be used in threads").setEphemeral(true).queue();
}

private void replyNotThreadOwner(MessageContextInteractionEvent event) {
event.reply("Only thread owners can use this command").setEphemeral(true).queue();
}

private void replyMaxPinsReached(MessageContextInteractionEvent event) {
event.reply("You've reached a maximum pinned limit (" + MAX_PINNED_ANSWERS
+ ") for threads, if you wish to pin more messages please remove a few existing pinned messages")
.setEphemeral(true)
.queue();
}
}
Loading