cleanup and reformat
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-09-05 00:06:35 +02:00
parent 14b54501fd
commit fd2970fa59
81 changed files with 1245 additions and 766 deletions

View File

@@ -14,34 +14,42 @@ import java.util.List;
public class ClearChat
{
private ClearChat() {
private ClearChat()
{
throw new IllegalStateException("Utility class");
}
public static String getLabel() {
public static String getLabel()
{
return "clear";
}
public static String getDescription() {
public static String getDescription()
{
return "Clear the current channel's chat.";
}
public static Permission getPermission() {
public static Permission getPermission()
{
return Permission.MESSAGE_MANAGE;
}
public static String checkDMs(Channel channel)
{
if(!(channel instanceof TextChannel))
{ return "\uD83D\uDE22 Sorry! I can't delete messages here."; }
if (!(channel instanceof TextChannel))
{
return "\uD83D\uDE22 Sorry! I can't delete messages here.";
}
return null;
}
public static String checkDeleteAmount(int toDeleteAmount)
{
if(toDeleteAmount <= 0)
{ return "\uD83D\uDE22 Sorry, I can't delete that amount of messages!"; }
if (toDeleteAmount <= 0)
{
return "\uD83D\uDE22 Sorry, I can't delete that amount of messages!";
}
return null;
}
@@ -65,7 +73,7 @@ public class ClearChat
//if there are some messages left, but less than <limit>, we need one more iterations.
int remainder = toDeleteAmount % limit;
if(remainder != 0) iterations++;
if (remainder != 0) iterations++;
// set the starting point.
long messageId = startingMessageId;
@@ -74,59 +82,62 @@ public class ClearChat
boolean outOfBounds = false;
// do iterate.
for(int iteration = 0; iteration < iterations; iteration++)
for (int iteration = 0; iteration < iterations; iteration++)
{
if(outOfBounds) break;
if (outOfBounds) break;
// 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... check if we have <limit> or fewer messages to delete
if(iteration+1 == iterations && remainder != 0) {
if (iteration + 1 == iterations && remainder != 0)
{
iterationSize = remainder;
}
if(iterationSize == 1)
if (iterationSize == 1)
{
// grab the message
Message toDelete = channel.retrieveMessageById(messageId).complete();
//only delete one message
if(toDelete != null) toDelete.delete().queue();
if (toDelete != null) toDelete.delete().queue();
else outOfBounds = true;
// increase deleted counter by 1
deleted++;
} else {
} else
{
// 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(channel.retrieveMessageById(messageId).complete());
if (iteration != 0) messages.add(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)
if (messages.size() <= 1)
{
outOfBounds = true;
} else {
} else
{
// before deleting, we need to grab the <previous to the oldest> message's id for next iteration.
action = channel.getHistoryBefore(messages.getLast().getIdLong(), 1);
List<Message> previousMessage = action.complete().getRetrievedHistory();
// if that message exists (we are not out of bounds)... store it
if(!previousMessage.isEmpty()) messageId = previousMessage.getFirst().getIdLong();
if (!previousMessage.isEmpty()) messageId = previousMessage.getFirst().getIdLong();
else outOfBounds = true;
}
// queue messages for deletion
if(messages.size() == 1)
if (messages.size() == 1)
{
messages.getFirst().delete().queue();
}
else if(!messages.isEmpty())
} else if (!messages.isEmpty())
{
try {
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
@@ -156,18 +167,22 @@ public class ClearChat
public static String parseAmount(int deleted)
{
if(deleted < 1)
if (deleted < 1)
{
return "\uD83D\uDE22 Couldn't clear any message!";
} else if(deleted == 1)
} else if (deleted == 1)
{
return "✂ Cleared 1 message!";
} else {
} else
{
return "✂ Cleared " + deleted + " messages!";
}
}
// cap the amount to avoid abuse.
public static int getMaxAmount() { return 1000; }
public static int getMaxAmount()
{
return 1000;
}
}