From 4348776678f9a28769318a895cfd4309ef540f8f Mon Sep 17 00:00:00 2001 From: Bence R Date: Sun, 15 Aug 2021 13:29:54 +0100 Subject: [PATCH 1/6] Purge now sends deleted messages to the user. --- .../command/impl/other/mod/PurgeCommand.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java index 5519aecb..df7bb274 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java @@ -8,8 +8,11 @@ import com.diamondfire.helpbot.bot.command.reply.PresetBuilder; import com.diamondfire.helpbot.bot.command.reply.feature.informative.*; import com.diamondfire.helpbot.bot.events.CommandEvent; +import net.dv8tion.jda.api.MessageBuilder; import net.dv8tion.jda.api.entities.*; +import java.time.format.DateTimeFormatter; + public class PurgeCommand extends Command { @Override public String getName() { return "purge"; } @@ -51,6 +54,27 @@ public void run(CommandEvent event) { } else { TextChannel channel = event.getChannel(); channel.getHistory().retrievePast(messagesToRemove).queue((messages) -> { + // Adds the messages to the messageBuilder object + MessageBuilder messageBuilder = new MessageBuilder(); + messageBuilder.append("Here are the messages you purged;\n"); + + for (Message m : messages) { + messageBuilder + .append("[") + .append(m.getTimeCreated().format(DateTimeFormatter.RFC_1123_DATE_TIME)) + .append("] (").append(m.getAuthor().getAsMention()).append("): ") + .append(m.getContentRaw()) + .append("\n"); + } + + // Builds the MessageBuilder object and iterates through it. + // messageBuilder.buildAll() returns a queue of messages, split if the content exceeds 2000 characters. + for (Message message : messageBuilder.buildAll()) { + event.getAuthor().openPrivateChannel().flatMap(userChannel -> + userChannel.sendMessage(message)).queue(); + } + + // Removes the messages. channel.deleteMessages(messages).queue(); }); } From a4fa31d5922c4f22ee5c3ae15e9264aa89a9527c Mon Sep 17 00:00:00 2001 From: Bence R Date: Sun, 15 Aug 2021 14:01:47 +0100 Subject: [PATCH 2/6] Removed unnecessary import --- .../helpbot/bot/command/impl/other/mod/PurgeCommand.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java index df7bb274..e740c9b9 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java @@ -12,6 +12,7 @@ import net.dv8tion.jda.api.entities.*; import java.time.format.DateTimeFormatter; +import java.util.Collections; public class PurgeCommand extends Command { @@ -58,6 +59,7 @@ public void run(CommandEvent event) { MessageBuilder messageBuilder = new MessageBuilder(); messageBuilder.append("Here are the messages you purged;\n"); + // Iterates through the message history and appends the values to the MessageBuilder. for (Message m : messages) { messageBuilder .append("[") From c68110d42419e8096408a5f6671caa8c9d50c547 Mon Sep 17 00:00:00 2001 From: Bence R Date: Sun, 15 Aug 2021 22:39:17 +0100 Subject: [PATCH 3/6] Now supports media --- .../command/impl/other/mod/PurgeCommand.java | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java index e740c9b9..4aaeecfc 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java @@ -12,7 +12,8 @@ import net.dv8tion.jda.api.entities.*; import java.time.format.DateTimeFormatter; -import java.util.Collections; +import java.util.*; +import java.util.concurrent.ExecutionException; public class PurgeCommand extends Command { @@ -58,6 +59,7 @@ public void run(CommandEvent event) { // Adds the messages to the messageBuilder object MessageBuilder messageBuilder = new MessageBuilder(); messageBuilder.append("Here are the messages you purged;\n"); + HashMap attachments = new HashMap<>(); // Iterates through the message history and appends the values to the MessageBuilder. for (Message m : messages) { @@ -67,6 +69,11 @@ public void run(CommandEvent event) { .append("] (").append(m.getAuthor().getAsMention()).append("): ") .append(m.getContentRaw()) .append("\n"); + if (!m.getAttachments().isEmpty()) { + for (Message.Attachment a : m.getAttachments()) { + attachments.put(a, m); + } + } } // Builds the MessageBuilder object and iterates through it. @@ -76,6 +83,29 @@ public void run(CommandEvent event) { userChannel.sendMessage(message)).queue(); } + // Sends media + for (Message.Attachment attachment : attachments.keySet()) { + event.getAuthor().openPrivateChannel().flatMap(userChannel -> + { + try { + MessageBuilder aBuilder = new MessageBuilder(); + Message mObject = attachments.get(attachment); + aBuilder.append("[") + .append(mObject + .getTimeCreated().format(DateTimeFormatter.RFC_1123_DATE_TIME)) + .append("] ") + .append(mObject + .getAuthor().getAsMention()); + return userChannel + .sendMessage(aBuilder.build()) + .addFile(attachment.downloadToFile().get()); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + return null; + }).queue(); + } + // Removes the messages. channel.deleteMessages(messages).queue(); }); From 5d8e13010fc61cc81c70317b9d57fc027570a31c Mon Sep 17 00:00:00 2001 From: Bence R Date: Sun, 15 Aug 2021 22:50:46 +0100 Subject: [PATCH 4/6] Rewrote a whole lotta stuff from Ryan's advice --- .../command/impl/other/mod/PurgeCommand.java | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java index 4aaeecfc..e98bf40b 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java @@ -64,11 +64,12 @@ public void run(CommandEvent event) { // Iterates through the message history and appends the values to the MessageBuilder. for (Message m : messages) { messageBuilder - .append("[") - .append(m.getTimeCreated().format(DateTimeFormatter.RFC_1123_DATE_TIME)) - .append("] (").append(m.getAuthor().getAsMention()).append("): ") - .append(m.getContentRaw()) - .append("\n"); + .append( + String.format("[%s] (%s): %s", + m.getTimeCreated().format(DateTimeFormatter.RFC_1123_DATE_TIME), + m.getAuthor().getAsMention(), + m.getContentRaw()) + ); if (!m.getAttachments().isEmpty()) { for (Message.Attachment a : m.getAttachments()) { attachments.put(a, m); @@ -87,22 +88,15 @@ public void run(CommandEvent event) { for (Message.Attachment attachment : attachments.keySet()) { event.getAuthor().openPrivateChannel().flatMap(userChannel -> { - try { - MessageBuilder aBuilder = new MessageBuilder(); - Message mObject = attachments.get(attachment); - aBuilder.append("[") - .append(mObject - .getTimeCreated().format(DateTimeFormatter.RFC_1123_DATE_TIME)) - .append("] ") - .append(mObject - .getAuthor().getAsMention()); - return userChannel - .sendMessage(aBuilder.build()) - .addFile(attachment.downloadToFile().get()); - } catch (InterruptedException | ExecutionException e) { - e.printStackTrace(); - } - return null; + MessageBuilder aBuilder = new MessageBuilder(); + Message mObject = attachments.get(attachment); + aBuilder.append(String.format( + "[%s] %s sent this attachment: %s", + mObject.getTimeCreated().format(DateTimeFormatter.RFC_1123_DATE_TIME), + mObject.getAuthor().getAsMention(), + attachment.getProxyUrl())); + return userChannel + .sendMessage(aBuilder.build()); }).queue(); } From 828d08982f9a140f6976da0b0b83ca01c2e4ebd6 Mon Sep 17 00:00:00 2001 From: Bence R Date: Mon, 16 Aug 2021 13:47:06 +0100 Subject: [PATCH 5/6] Purge now dumps messages + attachments into a .txt file. --- .../command/impl/other/mod/PurgeCommand.java | 52 +++++++++---------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java index e98bf40b..fedee133 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java @@ -8,9 +8,12 @@ import com.diamondfire.helpbot.bot.command.reply.PresetBuilder; import com.diamondfire.helpbot.bot.command.reply.feature.informative.*; import com.diamondfire.helpbot.bot.events.CommandEvent; +import com.diamondfire.helpbot.sys.externalfile.ExternalFileUtil; import net.dv8tion.jda.api.MessageBuilder; import net.dv8tion.jda.api.entities.*; +import java.io.File; +import java.nio.file.*; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.concurrent.ExecutionException; @@ -57,47 +60,42 @@ public void run(CommandEvent event) { TextChannel channel = event.getChannel(); channel.getHistory().retrievePast(messagesToRemove).queue((messages) -> { // Adds the messages to the messageBuilder object - MessageBuilder messageBuilder = new MessageBuilder(); - messageBuilder.append("Here are the messages you purged;\n"); - HashMap attachments = new HashMap<>(); + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("Here are the messages you purged;\n"); // Iterates through the message history and appends the values to the MessageBuilder. for (Message m : messages) { - messageBuilder + stringBuilder .append( String.format("[%s] (%s): %s", m.getTimeCreated().format(DateTimeFormatter.RFC_1123_DATE_TIME), - m.getAuthor().getAsMention(), + m.getAuthor().getName(), m.getContentRaw()) ); if (!m.getAttachments().isEmpty()) { for (Message.Attachment a : m.getAttachments()) { - attachments.put(a, m); + stringBuilder.append( + String.format(" [ATTACHMENT: %s ]\n", + a.getProxyUrl()) + ); } + } else { + stringBuilder.append( + "\n" + ); } } - // Builds the MessageBuilder object and iterates through it. - // messageBuilder.buildAll() returns a queue of messages, split if the content exceeds 2000 characters. - for (Message message : messageBuilder.buildAll()) { - event.getAuthor().openPrivateChannel().flatMap(userChannel -> - userChannel.sendMessage(message)).queue(); - } - - // Sends media - for (Message.Attachment attachment : attachments.keySet()) { - event.getAuthor().openPrivateChannel().flatMap(userChannel -> - { - MessageBuilder aBuilder = new MessageBuilder(); - Message mObject = attachments.get(attachment); - aBuilder.append(String.format( - "[%s] %s sent this attachment: %s", - mObject.getTimeCreated().format(DateTimeFormatter.RFC_1123_DATE_TIME), - mObject.getAuthor().getAsMention(), - attachment.getProxyUrl())); - return userChannel - .sendMessage(aBuilder.build()); - }).queue(); + try { + File file = ExternalFileUtil.generateFile("purge_log.txt"); + Files.writeString(file.toPath(), stringBuilder.toString(), StandardOpenOption.WRITE); + + event.getAuthor().openPrivateChannel().flatMap( + userChannel -> userChannel.sendFile(file) + ).queue(); + + } catch (Exception e) { + throw new IllegalStateException(); } // Removes the messages. From ac3e11316e07ee765695a943cb555e1cbf94495f Mon Sep 17 00:00:00 2001 From: Bence R Date: Mon, 16 Aug 2021 13:53:16 +0100 Subject: [PATCH 6/6] Messages are now inserted in the correct order --- .../bot/command/impl/other/mod/PurgeCommand.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java index fedee133..de68a769 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/mod/PurgeCommand.java @@ -61,12 +61,10 @@ public void run(CommandEvent event) { channel.getHistory().retrievePast(messagesToRemove).queue((messages) -> { // Adds the messages to the messageBuilder object StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append("Here are the messages you purged;\n"); // Iterates through the message history and appends the values to the MessageBuilder. for (Message m : messages) { - stringBuilder - .append( + stringBuilder.insert(0, String.format("[%s] (%s): %s", m.getTimeCreated().format(DateTimeFormatter.RFC_1123_DATE_TIME), m.getAuthor().getName(), @@ -74,17 +72,19 @@ public void run(CommandEvent event) { ); if (!m.getAttachments().isEmpty()) { for (Message.Attachment a : m.getAttachments()) { - stringBuilder.append( + stringBuilder.insert(0, String.format(" [ATTACHMENT: %s ]\n", a.getProxyUrl()) ); } } else { - stringBuilder.append( + stringBuilder.insert(0, "\n" ); } } + + stringBuilder.insert(0, "Here are the messages you purged;\n"); try { File file = ExternalFileUtil.generateFile("purge_log.txt");