Skip to content

Commit 2499044

Browse files
author
Andrew1031
committed
Cleaned up code
1 parent 717931c commit 2499044

File tree

1 file changed

+44
-28
lines changed

1 file changed

+44
-28
lines changed

application/src/main/java/org/togetherjava/tjbot/features/help/PinAnswerCommand.java

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,11 @@
1010
import org.togetherjava.tjbot.features.CommandVisibility;
1111
import org.togetherjava.tjbot.features.MessageContextCommand;
1212

13-
1413
public final class PinAnswerCommand extends BotCommandAdapter implements MessageContextCommand {
1514
private static final String COMMAND_NAME = "pin-answer";
1615
private static final int MAX_PINNED_ANSWERS = 3;
1716
private int count = 0;
1817

19-
/**
20-
* Creates a new instance.
21-
*
22-
*/
2318
public PinAnswerCommand() {
2419
super(Commands.message(COMMAND_NAME), CommandVisibility.GUILD);
2520
}
@@ -29,29 +24,50 @@ public void onMessageContext(MessageContextInteractionEvent event) {
2924
Message originalMessage = event.getTarget();
3025
User commandInvoker = event.getUser();
3126

32-
if (originalMessage.getChannel() instanceof ThreadChannel threadChannel) {
33-
if (commandInvoker.getIdLong() == threadChannel.getOwnerIdLong()) {
34-
if (count < MAX_PINNED_ANSWERS) {
35-
originalMessage.pin().queue(success -> {
36-
count++;
37-
event.reply("Answer pinned successfully! Pinned answers: " + count)
38-
.setEphemeral(true)
39-
.queue();
40-
}, failure -> event.reply("Failed to pin the answer.")
41-
.setEphemeral(true)
42-
.queue());
43-
} else {
44-
event.reply("Maximum pinned answers (" + count + ") reached")
45-
.setEphemeral(true)
46-
.queue();
47-
}
48-
} else {
49-
event.reply("You are not the thread creator and cannot pin answers here.")
50-
.setEphemeral(true)
51-
.queue();
52-
}
53-
} else {
54-
event.reply("This message is not in a thread.").setEphemeral(true).queue();
27+
if (!(originalMessage.getChannel() instanceof ThreadChannel threadChannel)) {
28+
replyNotInThread(event);
29+
return;
30+
}
31+
32+
if (!isThreadCreator(commandInvoker, threadChannel)) {
33+
replyNotThreadCreator(event);
34+
return;
35+
}
36+
37+
if (count >= MAX_PINNED_ANSWERS) {
38+
replyMaxPinsReached(event);
39+
return;
5540
}
41+
42+
pinMessage(event, originalMessage);
43+
}
44+
45+
private boolean isThreadCreator(User user, ThreadChannel thread) {
46+
return user.getIdLong() == thread.getOwnerIdLong();
47+
}
48+
49+
private void pinMessage(MessageContextInteractionEvent event, Message message) {
50+
message.pin().queue(success -> {
51+
count++;
52+
event.reply("Answer pinned successfully! Pinned answers: " + count)
53+
.setEphemeral(true)
54+
.queue();
55+
}, failure -> event.reply("Failed to pin the answer.").setEphemeral(true).queue());
56+
}
57+
58+
private void replyNotInThread(MessageContextInteractionEvent event) {
59+
event.reply("This message is not in a thread.").setEphemeral(true).queue();
60+
}
61+
62+
private void replyNotThreadCreator(MessageContextInteractionEvent event) {
63+
event.reply("You are not the thread creator and cannot pin answers here.")
64+
.setEphemeral(true)
65+
.queue();
66+
}
67+
68+
private void replyMaxPinsReached(MessageContextInteractionEvent event) {
69+
event.reply("Maximum pinned answers (" + MAX_PINNED_ANSWERS + ") reached.")
70+
.setEphemeral(true)
71+
.queue();
5672
}
5773
}

0 commit comments

Comments
 (0)