2022-11-20 18:56:57 +01:00
|
|
|
package wtf.beatrice.hidekobot.commands.slash;
|
2022-11-20 06:04:00 +01:00
|
|
|
|
|
|
|
import net.dv8tion.jda.api.entities.Message;
|
|
|
|
import net.dv8tion.jda.api.entities.MessageHistory;
|
2022-11-20 06:13:52 +01:00
|
|
|
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
|
|
|
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
|
2022-11-20 22:23:14 +01:00
|
|
|
import net.dv8tion.jda.api.entities.emoji.Emoji;
|
2022-11-20 06:04:00 +01:00
|
|
|
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
2022-11-20 22:23:14 +01:00
|
|
|
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
|
2022-11-20 06:04:00 +01:00
|
|
|
import net.dv8tion.jda.api.interactions.InteractionHook;
|
|
|
|
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
|
2022-11-20 22:23:14 +01:00
|
|
|
import net.dv8tion.jda.api.interactions.components.buttons.Button;
|
2022-11-21 00:14:13 +01:00
|
|
|
import net.dv8tion.jda.api.requests.restaction.WebhookMessageEditAction;
|
2022-11-20 06:04:00 +01:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2022-11-21 20:20:11 +01:00
|
|
|
import wtf.beatrice.hidekobot.Cache;
|
2022-11-22 14:32:22 +01:00
|
|
|
import wtf.beatrice.hidekobot.objects.SlashCommand;
|
2022-11-20 06:04:00 +01:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2022-11-22 14:32:22 +01:00
|
|
|
public class ClearCommand implements SlashCommand
|
2022-11-20 06:04:00 +01:00
|
|
|
{
|
|
|
|
|
2022-11-22 14:32:22 +01:00
|
|
|
@Override
|
|
|
|
public String getCommandName() {
|
|
|
|
return "clear";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
2022-11-20 22:09:58 +01:00
|
|
|
public void runSlashCommand(@NotNull SlashCommandInteractionEvent event)
|
2022-11-20 06:04:00 +01:00
|
|
|
{
|
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
// run in a new thread, so we don't block the main one
|
|
|
|
new Thread(() ->
|
2022-11-20 06:04:00 +01:00
|
|
|
{
|
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
MessageChannel channel = event.getChannel();
|
2022-11-20 06:04:00 +01:00
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
if(!(channel instanceof TextChannel))
|
|
|
|
{
|
2022-11-21 15:07:46 +01:00
|
|
|
event.reply("\uD83D\uDE22 Sorry! I can't delete messages here.").queue();
|
2022-11-21 00:14:13 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-11-20 06:04:00 +01:00
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
/* get the amount from the command args.
|
|
|
|
NULL should not be possible because we specified them as mandatory,
|
|
|
|
but apparently the mobile app doesn't care and still sends the command if you omit the args. */
|
2022-11-21 15:37:12 +01:00
|
|
|
OptionMapping amountOption = event.getOption("amount");
|
|
|
|
int toDeleteAmount = amountOption == null ? 1 : amountOption.getAsInt();
|
2022-11-20 06:04:00 +01:00
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
if(toDeleteAmount <= 0)
|
|
|
|
{
|
2022-11-21 15:07:46 +01:00
|
|
|
event.reply("\uD83D\uDE22 Sorry, I can't delete that amount of messages!").queue();
|
2022-11-21 00:14:13 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// answer by saying that the operation has begun.
|
|
|
|
InteractionHook replyInteraction = event.reply("\uD83D\uDEA7 Clearing...").complete();
|
2022-11-20 06:04:00 +01:00
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
// int to keep track of how many messages we actually deleted.
|
|
|
|
int deleted = 0;
|
2022-11-20 06:04:00 +01:00
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
int limit = 95; //discord limits this method to range 2-100. we set it to 95 to be safe.
|
2022-11-20 06:04:00 +01:00
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
// increase the count by 1, because we technically aren't clearing the first ID ever
|
|
|
|
// which is actually the slash command's ID and not a message.
|
|
|
|
toDeleteAmount++;
|
2022-11-20 06:04:00 +01:00
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
// count how many times we have to iterate this to delete the full <toDeleteAmount> messages.
|
|
|
|
int iterations = toDeleteAmount / limit;
|
2022-11-20 06:04:00 +01:00
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
//if there are some messages left, but less than <limit>, we need one more iterations.
|
|
|
|
int remainder = toDeleteAmount % limit;
|
|
|
|
if(remainder != 0) iterations++;
|
2022-11-20 06:04:00 +01:00
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
// set the starting point.
|
|
|
|
long messageId = event.getInteraction().getIdLong();
|
2022-11-20 06:04:00 +01:00
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
// boolean to see if we're trying to delete more messages than possible.
|
|
|
|
boolean outOfBounds = false;
|
2022-11-20 06:04:00 +01:00
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
// do iterate.
|
|
|
|
for(int iteration = 0; iteration < iterations; iteration++)
|
2022-11-20 06:04:00 +01:00
|
|
|
{
|
2022-11-21 00:14:13 +01:00
|
|
|
if(outOfBounds) break;
|
2022-11-20 06:04:00 +01:00
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
// set how many messages to delete for this iteration (usually <limit> unless there's a remainder)
|
|
|
|
int iterationSize = limit;
|
|
|
|
|
|
|
|
// if we are at the last iteration...
|
|
|
|
if(iteration+1 == iterations)
|
2022-11-20 06:04:00 +01:00
|
|
|
{
|
2022-11-21 00:14:13 +01:00
|
|
|
// check if we have <limit> or fewer messages to delete
|
|
|
|
if(remainder != 0) iterationSize = remainder;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(iterationSize == 1)
|
|
|
|
{
|
|
|
|
// grab the message
|
|
|
|
Message toDelete = ((TextChannel)channel).retrieveMessageById(messageId).complete();
|
|
|
|
//only delete one message
|
|
|
|
if(toDelete != null) toDelete.delete().queue();
|
|
|
|
else outOfBounds = true;
|
|
|
|
// increase deleted counter by 1
|
|
|
|
deleted++;
|
2022-11-20 06:04:00 +01:00
|
|
|
} else {
|
2022-11-21 00:14:13 +01:00
|
|
|
// get the last <iterationSize - 1> messages.
|
|
|
|
MessageHistory.MessageRetrieveAction action = channel.getHistoryBefore(messageId, iterationSize - 1);
|
|
|
|
// note: first one is the most recent, last one is the oldest message.
|
|
|
|
List<Message> messages = new ArrayList<>();
|
|
|
|
// (we are skipping first iteration since it would return an error, given that the id is the slash command and not a message)
|
|
|
|
if(iteration!=0) messages.add(((TextChannel)channel).retrieveMessageById(messageId).complete());
|
|
|
|
messages.addAll(action.complete().getRetrievedHistory());
|
|
|
|
|
|
|
|
// check if we only have one or zero messages left (trying to delete more than possible)
|
|
|
|
if(messages.size() <= 1)
|
|
|
|
{
|
|
|
|
outOfBounds = true;
|
|
|
|
} else {
|
|
|
|
// before deleting, we need to grab the <previous to the oldest> message's id for next iteration.
|
|
|
|
action = channel.getHistoryBefore(messages.get(messages.size() - 1).getIdLong(), 1);
|
2022-11-20 06:04:00 +01:00
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
List<Message> previousMessage = action.complete().getRetrievedHistory();
|
2022-11-20 06:04:00 +01:00
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
// if that message exists (we are not out of bounds)... store it
|
|
|
|
if(!previousMessage.isEmpty()) messageId = previousMessage.get(0).getIdLong();
|
|
|
|
else outOfBounds = true;
|
|
|
|
}
|
2022-11-20 06:04:00 +01:00
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
// queue messages for deletion
|
|
|
|
if(messages.size() == 1)
|
|
|
|
{
|
|
|
|
messages.get(0).delete().queue();
|
|
|
|
}
|
|
|
|
else if(!messages.isEmpty())
|
2022-11-20 06:04:00 +01:00
|
|
|
{
|
2022-11-21 00:14:13 +01:00
|
|
|
try {
|
|
|
|
((TextChannel) channel).deleteMessages(messages).complete();
|
|
|
|
/* alternatively, we could use purgeMessages, which is smarter...
|
|
|
|
however, it also tries to delete messages older than 2 weeks
|
|
|
|
which are restricted by discord, and thus has to use
|
|
|
|
a less efficient way that triggers rate-limiting very quickly. */
|
|
|
|
} catch (Exception e)
|
|
|
|
{
|
2022-11-21 15:07:46 +01:00
|
|
|
replyInteraction.editOriginal("\uD83D\uDE22 Sorry, I ran into an error! " + e.getMessage()).queue();
|
2022-11-21 00:14:13 +01:00
|
|
|
return; // warning: this quits everything.
|
|
|
|
}
|
2022-11-20 06:04:00 +01:00
|
|
|
}
|
2022-11-21 00:14:13 +01:00
|
|
|
|
|
|
|
// increase deleted counter by <list size>
|
|
|
|
deleted += messages.size();
|
2022-11-20 06:04:00 +01:00
|
|
|
}
|
2022-11-21 00:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-21 15:59:27 +01:00
|
|
|
Button dismissButton = Button.primary("clear_dismiss", "Dismiss")
|
|
|
|
.withEmoji(Emoji.fromUnicode("❌"));
|
2022-11-20 06:04:00 +01:00
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
WebhookMessageEditAction<Message> webhookMessageEditAction;
|
|
|
|
|
|
|
|
// log having deleted the messages (or not).
|
|
|
|
if(deleted < 1)
|
|
|
|
{
|
|
|
|
webhookMessageEditAction = replyInteraction.editOriginal("\uD83D\uDE22 Couldn't clear any message!");
|
|
|
|
} else if(deleted == 1)
|
|
|
|
{
|
|
|
|
webhookMessageEditAction = replyInteraction.editOriginal("✂ Cleared 1 message!");
|
|
|
|
} else {
|
|
|
|
webhookMessageEditAction = replyInteraction.editOriginal("✂ Cleared " + deleted + " messages!");
|
2022-11-20 06:04:00 +01:00
|
|
|
}
|
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
Message message = webhookMessageEditAction
|
2022-11-21 15:04:12 +01:00
|
|
|
.setActionRow(dismissButton)
|
2022-11-21 00:14:13 +01:00
|
|
|
.complete();
|
2022-11-20 22:23:14 +01:00
|
|
|
|
2022-11-22 00:28:33 +01:00
|
|
|
Cache.getDatabaseSource().queueDisabling(message);
|
|
|
|
Cache.getDatabaseSource().trackRanCommandReply(message, event.getUser());
|
2022-11-20 22:23:14 +01:00
|
|
|
|
2022-11-20 06:04:00 +01:00
|
|
|
}
|
2022-11-21 00:14:13 +01:00
|
|
|
}).start();
|
|
|
|
|
2022-11-20 22:23:14 +01:00
|
|
|
}
|
2022-11-20 06:04:00 +01:00
|
|
|
|
2022-11-21 15:04:12 +01:00
|
|
|
public void dismissMessage(ButtonInteractionEvent event)
|
2022-11-20 22:23:14 +01:00
|
|
|
{
|
2022-11-21 00:27:57 +01:00
|
|
|
|
2022-11-22 00:28:33 +01:00
|
|
|
if(!(Cache.getDatabaseSource().isUserTrackedFor(event.getUser().getId(), event.getMessageId())))
|
2022-11-21 00:27:57 +01:00
|
|
|
{
|
|
|
|
event.reply("❌ You did not run this command!").setEphemeral(true).queue();
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
event.getInteraction().getMessage().delete().queue();
|
|
|
|
}
|
2022-11-20 06:04:00 +01:00
|
|
|
}
|
|
|
|
}
|