Skip to content

Commit

Permalink
Fix purge command
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Jan 12, 2024
1 parent ab6333a commit 97736c1
Showing 1 changed file with 52 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
import org.geysermc.discordbot.util.MessageHelper;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class PurgeCommand extends SlashCommand {

Expand All @@ -69,9 +69,9 @@ protected void execute(SlashCommandEvent event) {
Member moderator = event.getMember();
int count = event.getOption("count").getAsInt();

// Make sure we don't have a invalid number
if (count <= 0) {
event.reply("Invalid count, Please specify a positive integer for the number of messages to delete!").setEphemeral(true).queue();
// Make sure we don't have an invalid number
if (count <= 0 || count > 100) {
event.reply("Invalid count, Please specify a positive integer (<100) for the number of messages to delete!").setEphemeral(true).queue();
return;
}

Expand All @@ -84,21 +84,14 @@ protected void execute(SlashCommandEvent event) {
}
}

List<String> delList = handle(user, moderator, event.getGuild(), history, count, true);
List<String> delList = handle(user, moderator, event.getGuild(), history, count);

if (delList == null) {
// Should only return null when it's a single entry
event.reply("Purged 1/1 messages!").setEphemeral(true).queue();
return;
if (delList.size() > 1) {
event.getTextChannel().deleteMessagesByIds(delList).queue();
} else if (delList.size() == 1) {
event.getTextChannel().deleteMessageById(delList.get(0)).queue();
}

// Remove the extra purge line at the end, so it doesn't interfere
String purged = delList.get(delList.size()-1);
delList.remove(delList.size()-1);

event.getTextChannel().deleteMessagesByIds(delList).queue();
event.reply("Purged " + purged + " messages!").setEphemeral(true).queue();

event.reply("Purged " + delList.size() + "/" + count + " messages!").setEphemeral(true).queue();
}

@Override
Expand All @@ -114,9 +107,9 @@ protected void execute(CommandEvent event) {
return;
}

// Make sure we dont have a invalid number
if (count <= 0) {
MessageHelper.errorResponse(event, "Invalid count", "Please specify a positive integer for the number of messages to delete!");
// Make sure we don't have an invalid number
if (count <= 0 || count > 100) {
MessageHelper.errorResponse(event, "Invalid count", "Please specify a positive integer (<100) for the number of messages to delete!");
return;
}

Expand All @@ -137,16 +130,21 @@ protected void execute(CommandEvent event) {

MessageHistory history = event.getChannel().getHistory();

List<String> delList = handle(user, event.getMember(), event.getGuild(), history, count, false);
List<String> delList = handle(user, event.getMember(), event.getGuild(), history, count);

if (delList != null) {
if (delList.size() > 1) {
event.getTextChannel().deleteMessagesByIds(delList).queue();
} else if (delList.size() == 1) {
event.getTextChannel().deleteMessageById(delList.get(0)).queue();
}
}

private List<String> handle(User user, Member mod, Guild guild, MessageHistory history, int count, boolean isSlash) {
int totalMessages = 0;
while (totalMessages < count) {
private List<String> handle(User user, Member mod, Guild guild, MessageHistory history, int count) {
OffsetDateTime twoWeeksAgo = OffsetDateTime.now().minusWeeks(2);

boolean hitTwoWeeks = false;
List<String> messagesToDeleteIds = new ArrayList<>();
while (messagesToDeleteIds.size() < count && !hitTwoWeeks) {
List<Message> messagesToDelete = new ArrayList<>();

// Pull the last 100 messages
Expand All @@ -155,54 +153,44 @@ private List<String> handle(User user, Member mod, Guild guild, MessageHistory h
continue;
}

/*
if (message.getIdLong() == event.getMessage().getIdLong()) {
continue;
// Check if we hit the two-week mark
// Discord API doesn't let us remove messages older than two weeks
if (message.getTimeCreated().isBefore(twoWeeksAgo)) {
hitTwoWeeks = true;
break;
}
*/

messagesToDelete.add(message);
totalMessages++;

if (totalMessages >= count) {
if ((messagesToDeleteIds.size() + messagesToDelete.size()) >= count) {
break;
}
}

// Remove the message(s)
List<String> messagesToDeleteIds = messagesToDelete.stream().map(ISnowflake::getId).collect(Collectors.toList());

if (messagesToDelete.size() > 1) {
// Tell the log handler to ignore the messages
LogHandler.PURGED_MESSAGES.addAll(messagesToDeleteIds);

// TODO: Store the contents of removed messages and upload along side the log
// Log the change
MessageEmbed bannedEmbed = new EmbedBuilder()
.setTitle("Purged channel")
.addField("Channel", history.getChannel().getAsMention(), false)
.addField("Target", user != null ? user.getAsMention() : "None", false)
.addField("Staff member", mod.getAsMention(), false)
.addField("Count", totalMessages + "/" + count, false)
.setTimestamp(Instant.now())
.setColor(BotColors.FAILURE.getColor())
.build();

// Send the embed as a reply and to the log
ServerSettings.getLogChannel(guild).sendMessageEmbeds(bannedEmbed).queue();

if (isSlash) {
// If slash command, add purge message count
messagesToDeleteIds.add(totalMessages+"/"+count);
}
// Add messages to delete
messagesToDeleteIds.addAll(messagesToDelete.stream().map(ISnowflake::getId).toList());
}

// Return to the main event
return messagesToDeleteIds;
} else if (messagesToDelete.size() == 1) {
messagesToDelete.get(0).delete().queue();
break;
}
if (!messagesToDeleteIds.isEmpty()) {
// Tell the log handler to ignore the messages
LogHandler.PURGED_MESSAGES.addAll(messagesToDeleteIds);

// TODO: Store the contents of removed messages and upload along side the log
// Log the change
MessageEmbed purgedEmbed = new EmbedBuilder()
.setTitle("Purged channel")
.addField("Channel", history.getChannel().getAsMention(), false)
.addField("Target", user != null ? user.getAsMention() : "None", false)
.addField("Staff member", mod.getAsMention(), false)
.addField("Count", messagesToDeleteIds.size() + "/" + count, false)
.setTimestamp(Instant.now())
.setColor(BotColors.FAILURE.getColor())
.build();

// Send the embed as a reply and to the log
ServerSettings.getLogChannel(guild).sendMessageEmbeds(purgedEmbed).queue();
}
return null;

return messagesToDeleteIds;
}
}

0 comments on commit 97736c1

Please sign in to comment.