From 59c5e09f14fb8d8ac333825df6db2ae9b6c085c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Fri, 26 Aug 2022 01:15:10 +0200 Subject: [PATCH] Add demo clear-chat command This command is very unoptimized (spawning 12 threads) and sometimes hits 429 errors, but it works until things get more serious. Also it's hardcoded to only delete 10 messages. --- .../wtf/beatrice/hidekobot/HidekoBot.java | 3 +- .../hidekobot/listeners/MessageListener.java | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index c645f78..ba95186 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -16,7 +16,8 @@ import java.util.List; public class HidekoBot { private static String botToken; - private static String standardInviteLink = "https://discord.com/oauth2/authorize?client_id=%userid%&scope=bot&permissions=8"; + private static String standardInviteLink = + "https://discord.com/oauth2/authorize?client_id=%userid%&scope=bot&permissions=8"; private static String botUserId; private static final String version = "0.0.1"; // we should probably find a way to make this consistent with Maven diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java index 32887a3..8be6a8d 100644 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java @@ -3,11 +3,17 @@ package wtf.beatrice.hidekobot.listeners; import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; +import net.dv8tion.jda.api.requests.RestAction; import org.jetbrains.annotations.NotNull; import wtf.beatrice.hidekobot.Configuration; import wtf.beatrice.hidekobot.utils.Logger; import wtf.beatrice.hidekobot.utils.RandomUtil; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + public class MessageListener extends ListenerAdapter { @@ -39,5 +45,32 @@ public class MessageListener extends ListenerAdapter channel.sendMessage(msg).queue(); } + + + if(event.getMessage().getContentDisplay().equalsIgnoreCase("clear the chat")) + { + MessageChannel channel = event.getChannel(); + int count = 10; + int delay = 300; + + Message warn = channel.sendMessage("Clearing...").complete(); + + MessageHistory.MessageRetrieveAction action = channel.getHistoryBefore(event.getMessage().getIdLong(), count); + List messagesUnmodifiable = action.complete().getRetrievedHistory(); + List messages = new ArrayList<>(messagesUnmodifiable); + messages.add(warn); + messages.add(event.getMessage()); + for(Message msg : messages) + { + //... after waiting X seconds. + Executors.newSingleThreadScheduledExecutor().schedule(() -> + { + logger.log(msg.getContentDisplay()); + msg.delete().complete(); + }, delay, TimeUnit.MILLISECONDS); + + delay += 500; + } + } } }