Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@
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;

public class PurgeCommand extends Command {

@Override public String getName() { return "purge"; }
Expand Down Expand Up @@ -51,6 +59,46 @@ public void run(CommandEvent event) {
} else {
TextChannel channel = event.getChannel();
channel.getHistory().retrievePast(messagesToRemove).queue((messages) -> {
// Adds the messages to the messageBuilder object
StringBuilder stringBuilder = new StringBuilder();

// Iterates through the message history and appends the values to the MessageBuilder.
for (Message m : messages) {
stringBuilder.insert(0,
String.format("[%s] (%s): %s",
m.getTimeCreated().format(DateTimeFormatter.RFC_1123_DATE_TIME),
m.getAuthor().getName(),
m.getContentRaw())
);
if (!m.getAttachments().isEmpty()) {
for (Message.Attachment a : m.getAttachments()) {
stringBuilder.insert(0,
String.format(" [ATTACHMENT: %s ]\n",
a.getProxyUrl())
);
}
} else {
stringBuilder.insert(0,
"\n"
);
}
}

stringBuilder.insert(0, "Here are the messages you purged;\n");

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.
channel.deleteMessages(messages).queue();
});
}
Expand Down