33import net .dv8tion .jda .api .entities .*;
44import net .dv8tion .jda .api .events .interaction .command .SlashCommandInteractionEvent ;
55import net .dv8tion .jda .api .interactions .commands .OptionMapping ;
6+ import net .dv8tion .jda .api .requests .restaction .MessageAction ;
67import net .dv8tion .jda .api .requests .restaction .interactions .ReplyCallbackAction ;
78import net .javadiscord .javabot .Bot ;
8- import net .javadiscord .javabot .command .ResponseException ;
99import net .javadiscord .javabot .command .Responses ;
1010import net .javadiscord .javabot .command .moderation .ModerateCommand ;
1111import net .javadiscord .javabot .data .config .guild .ModerationConfig ;
2222import 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 */
2727public 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