Skip to content

Commit 57cb269

Browse files
let the bot send the archive file to the log channel
1 parent df0e45b commit 57cb269

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/main/java/net/javadiscord/javabot/systems/moderation/PurgeCommand.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import net.dv8tion.jda.api.entities.*;
44
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
55
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
6+
import net.dv8tion.jda.api.requests.restaction.MessageAction;
67
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
78
import net.javadiscord.javabot.Bot;
8-
import net.javadiscord.javabot.command.ResponseException;
99
import net.javadiscord.javabot.command.Responses;
1010
import net.javadiscord.javabot.command.moderation.ModerateCommand;
1111
import net.javadiscord.javabot.data.config.guild.ModerationConfig;
@@ -22,9 +22,12 @@
2222
import java.util.List;
2323

2424
/**
25-
* This command deletes messages from a channel.
25+
* Moderation command that deletes multiple messages from a single channel.
2626
*/
2727
public class PurgeCommand extends ModerateCommand {
28+
29+
private final Path ARCHIVE_DIR = Path.of("purgeArchives");
30+
2831
@Override
2932
protected ReplyCallbackAction handleModerationCommand(SlashCommandInteractionEvent event, Member commandUser) {
3033
OptionMapping amountOption = event.getOption("amount");
@@ -61,7 +64,9 @@ protected ReplyCallbackAction handleModerationCommand(SlashCommandInteractionEve
6164
*/
6265
private void purge(@Nullable Long amount, @Nullable User user, boolean archive, TextChannel channel, TextChannel logChannel) {
6366
MessageHistory history = channel.getHistory();
64-
PrintWriter archiveWriter = archive ? createArchiveWriter(channel, logChannel) : null;
67+
String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss"));
68+
String file = String.format("purge_%s_%s.txt", channel.getName(), timestamp);
69+
PrintWriter archiveWriter = archive ? createArchiveWriter(channel, logChannel, file) : null;
6570
List<Message> messages;
6671
OffsetDateTime startTime = OffsetDateTime.now();
6772
long count = 0;
@@ -82,12 +87,14 @@ private void purge(@Nullable Long amount, @Nullable User user, boolean archive,
8287
if (archiveWriter != null) {
8388
archiveWriter.close();
8489
}
85-
logChannel.sendMessage(String.format(
90+
MessageAction action = logChannel.sendMessage(String.format(
8691
"Purge of channel %s has completed. %d messages have been removed, and the purge took %s.",
8792
channel.getAsMention(),
8893
count,
8994
new TimeUtils().formatDurationToNow(startTime)
90-
)).queue();
95+
));
96+
if (archive) action.addFile(ARCHIVE_DIR.resolve(file).toFile());
97+
action.queue();
9198
}
9299

93100
/**
@@ -120,14 +127,13 @@ private int removeMessages(List<Message> messages, @Nullable User user, @Nullabl
120127
*
121128
* @param channel The channel to create the writer for.
122129
* @param logChannel The log channel, where log messages can be sent.
130+
* @param file The archive's filename.
123131
* @return The print writer to use.
124132
*/
125-
private PrintWriter createArchiveWriter(TextChannel channel, TextChannel logChannel) {
133+
private PrintWriter createArchiveWriter(TextChannel channel, TextChannel logChannel, String file) {
126134
try {
127-
Path purgeArchivesDir = Path.of("purgeArchives");
128-
if (Files.notExists(purgeArchivesDir)) Files.createDirectory(purgeArchivesDir);
129-
String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss"));
130-
Path archiveFile = purgeArchivesDir.resolve("purge_" + channel.getName() + "_" + timestamp + ".txt");
135+
if (Files.notExists(ARCHIVE_DIR)) Files.createDirectory(ARCHIVE_DIR);
136+
Path archiveFile = ARCHIVE_DIR.resolve(file);
131137
var archiveWriter = new PrintWriter(Files.newBufferedWriter(archiveFile), true);
132138
logChannel.sendMessage("Created archive of purge of channel " + channel.getAsMention() + " at " + archiveFile).queue();
133139
archiveWriter.println("Purge of channel " + channel.getName());

0 commit comments

Comments
 (0)