Skip to content

Commit bef8fc4

Browse files
committed
send warning when deleting many messages
1 parent b116792 commit bef8fc4

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/main/java/net/discordjug/javabot/systems/help/ClearOldHelpNotificationJob.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package net.discordjug.javabot.systems.help;
22

3+
import java.nio.charset.StandardCharsets;
34
import java.time.OffsetDateTime;
45
import java.util.ArrayList;
56
import java.util.List;
7+
import java.util.stream.Collectors;
68

79
import org.springframework.scheduling.annotation.Scheduled;
810
import org.springframework.stereotype.Service;
@@ -15,6 +17,7 @@
1517
import net.dv8tion.jda.api.entities.Message;
1618
import net.dv8tion.jda.api.entities.MessageHistory;
1719
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
20+
import net.dv8tion.jda.api.utils.FileUpload;
1821

1922
/**
2023
* Automatically delete old help notifications.
@@ -46,6 +49,16 @@ private void deleteOldMessagesInChannel(TextChannel helpNotificationChannel, Mes
4649
if (deleteMore) {
4750
deleteOldMessagesInChannel(helpNotificationChannel, history, foundSoFar);
4851
}else {
52+
if (foundSoFar.size() > 50) {
53+
String messageInfo = foundSoFar
54+
.stream()
55+
.map(msg -> convertMessageToString(msg))
56+
.collect(Collectors.joining("\n\n===============\n\n"));
57+
botConfig.get(helpNotificationChannel.getGuild()).getModerationConfig().getLogChannel()
58+
.sendMessageFormat("Warning: deleting %d messages in help notification channel, this might be due to help notification spam", foundSoFar.size())
59+
.addFiles(FileUpload.fromData(messageInfo.getBytes(StandardCharsets.UTF_8), "messages.txt"))
60+
.queue();
61+
}
4962
helpNotificationChannel.purgeMessages(foundSoFar);
5063
}
5164
}, e -> {
@@ -54,6 +67,11 @@ private void deleteOldMessagesInChannel(TextChannel helpNotificationChannel, Mes
5467
});
5568
}
5669

70+
private String convertMessageToString(Message msg) {
71+
return msg.getContentRaw()+"\n"+
72+
msg.getEmbeds().stream().map(e->e.toData().toString()).collect(Collectors.joining("\n"));
73+
}
74+
5775
private boolean addMessagesToDelete(List<Message> toDelete, List<Message> msgs) {
5876
for (Message message : msgs) {
5977
if (message.getAuthor().getIdLong() == message.getJDA().getSelfUser().getIdLong() &&

0 commit comments

Comments
 (0)