Skip to content

Commit 9f51321

Browse files
authored
Merge pull request #471 from danthe1st/clear-help-notifs
automatically clear old help notifications
2 parents 25a04d4 + 6d6abbc commit 9f51321

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

src/main/java/net/discordjug/javabot/systems/help/commands/HelpCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.discordjug.javabot.systems.help.commands;
22

3+
import net.discordjug.javabot.systems.help.commands.notify.HelpPingSubcommand;
34
import net.dv8tion.jda.api.interactions.commands.build.Commands;
45
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand;
56

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package net.discordjug.javabot.systems.help.commands.notify;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.time.OffsetDateTime;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
import org.springframework.scheduling.annotation.Scheduled;
10+
import org.springframework.stereotype.Service;
11+
12+
import lombok.RequiredArgsConstructor;
13+
import net.discordjug.javabot.data.config.BotConfig;
14+
import net.discordjug.javabot.util.ExceptionLogger;
15+
import net.dv8tion.jda.api.JDA;
16+
import net.dv8tion.jda.api.entities.Guild;
17+
import net.dv8tion.jda.api.entities.Message;
18+
import net.dv8tion.jda.api.entities.MessageHistory;
19+
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
20+
import net.dv8tion.jda.api.utils.FileUpload;
21+
22+
/**
23+
* Automatically delete old help notifications.
24+
* Once a day, this class deletes old messages of the bot in the help notification channel.
25+
*/
26+
@Service
27+
@RequiredArgsConstructor
28+
public class ClearOldHelpNotificationJob {
29+
private final BotConfig botConfig;
30+
private final JDA jda;
31+
32+
/**
33+
* Runs the message deletion.
34+
*/
35+
@Scheduled(cron="0 0 0 * * *")//00:00 UTC
36+
public void execute() {
37+
for (Guild guild : jda.getGuilds()) {
38+
TextChannel helpNotificationChannel = botConfig.get(guild).getHelpConfig().getHelpNotificationChannel();
39+
if(helpNotificationChannel != null) {
40+
MessageHistory history = helpNotificationChannel.getHistory();
41+
deleteOldMessagesInChannel(helpNotificationChannel, history, new ArrayList<>());
42+
}
43+
}
44+
}
45+
46+
private void deleteOldMessagesInChannel(TextChannel helpNotificationChannel, MessageHistory history, List<Message> foundSoFar) {
47+
history.retrievePast(50).queue(msgs -> {
48+
List<Message> toDelete = msgs
49+
.stream()
50+
.filter(msg -> msg.getAuthor().getIdLong() == msg.getJDA().getSelfUser().getIdLong())
51+
.filter(msg -> msg.getTimeCreated().isBefore(OffsetDateTime.now().minusDays(3)))
52+
.filter(msg -> msg
53+
.getButtons()
54+
.stream()
55+
.anyMatch(button -> "Mark as unacknowledged".equals(button.getLabel())))
56+
.toList();
57+
helpNotificationChannel.purgeMessages(toDelete);
58+
foundSoFar.addAll(toDelete);
59+
if (!msgs.isEmpty()) {
60+
deleteOldMessagesInChannel(helpNotificationChannel, history, foundSoFar);
61+
}else {
62+
if (foundSoFar.size() > 50) {
63+
String messageInfo = foundSoFar
64+
.stream()
65+
.map(msg -> convertMessageToString(msg))
66+
.collect(Collectors.joining("\n\n===============\n\n"));
67+
botConfig.get(helpNotificationChannel.getGuild()).getModerationConfig().getLogChannel()
68+
.sendMessageFormat("Warning: deleting %d messages in help notification channel, this might be due to help notification spam", foundSoFar.size())
69+
.addFiles(FileUpload.fromData(messageInfo.getBytes(StandardCharsets.UTF_8), "messages.txt"))
70+
.queue();
71+
}
72+
}
73+
}, e -> {
74+
ExceptionLogger.capture(e, getClass().getName());
75+
helpNotificationChannel.purgeMessages(foundSoFar);
76+
});
77+
}
78+
79+
private String convertMessageToString(Message msg) {
80+
return msg.getContentRaw()+"\n"+
81+
msg.getEmbeds().stream().map(e->e.toData().toString()).collect(Collectors.joining("\n"));
82+
}
83+
}

src/main/java/net/discordjug/javabot/systems/help/commands/HelpPingSubcommand.java renamed to src/main/java/net/discordjug/javabot/systems/help/commands/notify/HelpPingSubcommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.discordjug.javabot.systems.help.commands;
1+
package net.discordjug.javabot.systems.help.commands.notify;
22

33
import net.discordjug.javabot.annotations.AutoDetectableComponentHandler;
44
import net.discordjug.javabot.data.config.BotConfig;

0 commit comments

Comments
 (0)