Add demo clear-chat command
continuous-integration/drone/push Build is passing Details

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.
This commit is contained in:
Bea 2022-08-26 01:15:10 +02:00
parent a875053435
commit 59c5e09f14
2 changed files with 35 additions and 1 deletions

View File

@ -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

View File

@ -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<Message> messagesUnmodifiable = action.complete().getRetrievedHistory();
List<Message> 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;
}
}
}
}