-
-
Notifications
You must be signed in to change notification settings - Fork 91
context command to pin messages in respective thread #1188 #1194
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
Closed
andrew-09
wants to merge
6
commits into
Together-Java:develop
from
andrew-09:feature/pin_answer_context_command
+82
−0
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cee6189
Pin answer context command working
6c01ced
Clearer replies for users. Better way of getting the # of pinned mess…
5b40a1f
Remove unnecessary method and use replyNotInThread instead
66d28ab
Use matches intead of equals, merge if statements and remove unnecess…
15500a4
newline undo
8759346
More intuitive function name . Change replynotinthread to replynotinq…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
application/src/main/java/org/togetherjava/tjbot/features/help/PinAnswerCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
andrew-09 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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(); | ||
} | ||
andrew-09 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private void replyNotThreadCreator(MessageContextInteractionEvent event) { | ||
event.reply("You are not the thread creator and cannot pin answers here.") | ||
.setEphemeral(true) | ||
andrew-09 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.queue(); | ||
} | ||
|
||
private void replyMaxPinsReached(MessageContextInteractionEvent event) { | ||
event.reply("Maximum pinned answers (" + MAX_PINNED_ANSWERS + ") reached.") | ||
.setEphemeral(true) | ||
andrew-09 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.queue(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.