Repository navigation
Create new Purge command. #238
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
RealYusufIsmail
wants to merge
6
commits into
Together-Java:develop
from
BotCraftHub:feature/pruge_and_clear_command
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
03a6f80
Getting started
RealYusufIsmail 878a8ca
added purge command
RealYusufIsmail b7f4ae7
Update PurgeCommand.java
RealYusufIsmail c4f5e54
Update PurgeCommand.java
RealYusufIsmail 4374979
Merge branch 'develop' into feature/pruge_and_clear_command
RealYusufIsmail af6a6ad
spotlessApply
RealYusufIsmail 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
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
108 changes: 108 additions & 0 deletions
108
application/src/main/java/org/togetherjava/tjbot/commands/moderation/PurgeCommand.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,108 @@ | ||||||||||
| package org.togetherjava.tjbot.commands.moderation; | ||||||||||
|
|
||||||||||
| import net.dv8tion.jda.api.Permission; | ||||||||||
| import net.dv8tion.jda.api.entities.ISnowflake; | ||||||||||
| import net.dv8tion.jda.api.entities.Member; | ||||||||||
| import net.dv8tion.jda.api.entities.MessageChannel; | ||||||||||
| import net.dv8tion.jda.api.entities.TextChannel; | ||||||||||
| import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; | ||||||||||
| import net.dv8tion.jda.api.interactions.commands.OptionType; | ||||||||||
| import org.jetbrains.annotations.NotNull; | ||||||||||
| import org.slf4j.Logger; | ||||||||||
| import org.slf4j.LoggerFactory; | ||||||||||
| import org.togetherjava.tjbot.commands.SlashCommandAdapter; | ||||||||||
| import org.togetherjava.tjbot.commands.SlashCommandVisibility; | ||||||||||
|
|
||||||||||
| import java.util.ArrayList; | ||||||||||
| import java.util.List; | ||||||||||
| import java.util.Objects; | ||||||||||
| import java.util.concurrent.locks.ReentrantLock; | ||||||||||
| import java.util.function.Consumer; | ||||||||||
| import java.util.stream.Collectors; | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * This command can remove specific messages using their ids. The command requires the user to input | ||||||||||
| * the id of message they want to delete from and the id of the messages they want to delete to. | ||||||||||
| * <p> | ||||||||||
| * The command fails if one of the message id that was given is invalid or the user lacks the | ||||||||||
| * permission to delete messages. | ||||||||||
| */ | ||||||||||
| public class PurgeCommand extends SlashCommandAdapter { | ||||||||||
| // TODO Test the command | ||||||||||
| private static final Logger logger = LoggerFactory.getLogger(PurgeCommand.class); | ||||||||||
| private static final String FIRST_MESSAGE_ID = "first_message_id"; | ||||||||||
| private static final String LAST_MESSAGE_ID = "last_message_id"; | ||||||||||
|
|
||||||||||
|
|
||||||||||
| public PurgeCommand() { | ||||||||||
| super("purge", "Delete specific messages using there ids.", SlashCommandVisibility.GUILD); | ||||||||||
|
|
||||||||||
| getData() | ||||||||||
| .addOption(OptionType.NUMBER, FIRST_MESSAGE_ID, | ||||||||||
| "The id of the message you want to delete from.", true) | ||||||||||
| .addOption(OptionType.NUMBER, LAST_MESSAGE_ID, | ||||||||||
| "The id of the message you want to delete to.", true); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Override | ||||||||||
| public void onSlashCommand(@NotNull SlashCommandEvent event) { | ||||||||||
| final Member author = event.getMember(); | ||||||||||
|
|
||||||||||
| if (!author.hasPermission(Permission.MESSAGE_MANAGE)) { | ||||||||||
| event.reply("You are missing MESSAGE_MANAGE permission to delete these the message") | ||||||||||
| .setEphemeral(true) | ||||||||||
| .queue(); | ||||||||||
| return; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| final Member bot = Objects.requireNonNull(event.getGuild()).getSelfMember(); | ||||||||||
| if (!bot.hasPermission(Permission.MESSAGE_MANAGE)) { | ||||||||||
| event.reply( | ||||||||||
| "I am missing MESSAGE_MANAGE permission which means I am unable to delete messages in this server.") | ||||||||||
| .setEphemeral(true) | ||||||||||
| .queue(); | ||||||||||
|
|
||||||||||
| logger.error("The bot does not have MESSAGE_MANAGE permission on the server '{}' ", | ||||||||||
| event.getGuild().getId()); | ||||||||||
| return; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| long firstMessageId = Objects.requireNonNull(event.getOption(FIRST_MESSAGE_ID)).getAsLong(); | ||||||||||
| long lastMessageId = Objects.requireNonNull(event.getOption(LAST_MESSAGE_ID)).getAsLong(); | ||||||||||
|
|
||||||||||
| deleteMessagesById(firstMessageId, lastMessageId, event.getMessageChannel(), strings -> { | ||||||||||
|
|
||||||||||
| }); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private static void deleteMessagesById(long firstMessageId, long lastMessageId, | ||||||||||
| MessageChannel mc, Consumer<List<String>> cb) { | ||||||||||
| getBetween0(firstMessageId, lastMessageId, mc, new ArrayList<>(), new ReentrantLock(), cb); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private static void getBetween0(long firstMessageId, long lastMessageId, MessageChannel mc, | ||||||||||
| List<String> acc, ReentrantLock lock, Consumer<List<String>> cb) { | ||||||||||
|
Comment on lines
+83
to
+84
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
mc, Minecraft? This is about Minecraft? I don't like the abbreviations myself and I'd prefer longer, more clear names. I currently can't comment on the behaviour since I'm not sure what "cb" and "acc" their purpose is. |
||||||||||
| mc.getHistoryBefore(firstMessageId, 100).queue(his -> { | ||||||||||
| try { | ||||||||||
| lock.lock(); | ||||||||||
| List<String> collected = his.getRetrievedHistory() | ||||||||||
| .stream() | ||||||||||
| .map(ISnowflake::getId) | ||||||||||
| .collect(Collectors.toList()); | ||||||||||
|
|
||||||||||
| int foundIdx = collected.indexOf(String.valueOf(lastMessageId)); | ||||||||||
|
|
||||||||||
| if (foundIdx != -1) { | ||||||||||
| acc.addAll(collected.subList(0, foundIdx)); | ||||||||||
| cb.accept(acc); | ||||||||||
| } else { | ||||||||||
| acc.addAll(collected); | ||||||||||
| getBetween0(Long.parseLong(collected.get(collected.size() - 1)), lastMessageId, | ||||||||||
| mc, acc, lock, cb); | ||||||||||
| } | ||||||||||
| } finally { | ||||||||||
| lock.unlock(); | ||||||||||
| } | ||||||||||
| }); | ||||||||||
| } | ||||||||||
| } | ||||||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some places you use
final, some you don't (on local variables)I'd recommend keeping consistency, and either use it everywhere or nowhere.