diff --git a/src/main/java/org/geysermc/discordbot/commands/moderation/PurgeCommand.java b/src/main/java/org/geysermc/discordbot/commands/moderation/PurgeCommand.java index afd39250..a51c5384 100644 --- a/src/main/java/org/geysermc/discordbot/commands/moderation/PurgeCommand.java +++ b/src/main/java/org/geysermc/discordbot/commands/moderation/PurgeCommand.java @@ -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 { @@ -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; } @@ -84,21 +84,14 @@ protected void execute(SlashCommandEvent event) { } } - List delList = handle(user, moderator, event.getGuild(), history, count, true); + List 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 @@ -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; } @@ -137,16 +130,21 @@ protected void execute(CommandEvent event) { MessageHistory history = event.getChannel().getHistory(); - List delList = handle(user, event.getMember(), event.getGuild(), history, count, false); + List 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 handle(User user, Member mod, Guild guild, MessageHistory history, int count, boolean isSlash) { - int totalMessages = 0; - while (totalMessages < count) { + private List handle(User user, Member mod, Guild guild, MessageHistory history, int count) { + OffsetDateTime twoWeeksAgo = OffsetDateTime.now().minusWeeks(2); + + boolean hitTwoWeeks = false; + List messagesToDeleteIds = new ArrayList<>(); + while (messagesToDeleteIds.size() < count && !hitTwoWeeks) { List messagesToDelete = new ArrayList<>(); // Pull the last 100 messages @@ -155,54 +153,44 @@ private List 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 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; } }