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 #1193

Closed
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 @@ -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;
Expand Down Expand Up @@ -160,6 +151,7 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con

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

// User context commands

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading