Improve diceroll looks, implement limits to avoid abuse

This commit is contained in:
Bea 2022-12-19 17:30:47 +01:00
parent 982902fc6d
commit b033763704
4 changed files with 57 additions and 5 deletions

View File

@ -54,7 +54,8 @@ public class ClearChat
// int to keep track of how many messages we actually deleted. // int to keep track of how many messages we actually deleted.
int deleted = 0; int deleted = 0;
int limit = 95; //discord limits this method to range 2-100. we set it to 95 to be safe. int limit = 95; //discord limits this method to only 2<x<100 deletions per run.
// we set this slightly lower to be safe, and iterate as needed.
// increase the count by 1, because we technically aren't clearing the first ID ever // 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. // which is actually the slash command's ID and not a message.
@ -193,4 +194,7 @@ public class ClearChat
} }
} }
// cap the amount to avoid abuse.
public static int getMaxAmount() { return 1000; }
} }

View File

@ -53,6 +53,9 @@ public class ClearCommand implements MessageCommand
} }
} }
// cap the amount to avoid abuse.
if(toDeleteAmount > ClearChat.getMaxAmount()) toDeleteAmount = 0;
error = ClearChat.checkDeleteAmount(toDeleteAmount); error = ClearChat.checkDeleteAmount(toDeleteAmount);
if (error != null) { if (error != null) {
event.getMessage().reply(error).queue(); event.getMessage().reply(error).queue();

View File

@ -1,8 +1,10 @@
package wtf.beatrice.hidekobot.commands.message; package wtf.beatrice.hidekobot.commands.message;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import wtf.beatrice.hidekobot.Cache;
import wtf.beatrice.hidekobot.objects.Dice; import wtf.beatrice.hidekobot.objects.Dice;
import wtf.beatrice.hidekobot.objects.commands.MessageCommand; import wtf.beatrice.hidekobot.objects.commands.MessageCommand;
@ -29,16 +31,23 @@ public class DiceRollCommand implements MessageCommand
@Override @Override
public void runCommand(MessageReceivedEvent event, String label, String[] args) public void runCommand(MessageReceivedEvent event, String label, String[] args)
{ {
LinkedHashMap<Dice, Integer> dicesToRoll = new LinkedHashMap<Dice, Integer>(); LinkedHashMap<Dice, Integer> dicesToRoll = new LinkedHashMap<>();
String diceRegex = "d[0-9]+"; String diceRegex = "d[0-9]+";
String amountRegex = "[0-9]+"; String amountRegex = "[0-9]+";
Dice currentDice = null; Dice currentDice = null;
int currentAmount; int currentAmount;
UUID lastPushedDice = null; UUID lastPushedDice = null;
int totalRolls = 0;
for(String arg : args) for(String arg : args)
{ {
if(totalRolls > 200)
{
event.getMessage().reply("Too many total rolls!").queue();
return;
}
if(arg.matches(amountRegex)) if(arg.matches(amountRegex))
{ {
currentAmount = Integer.parseInt(arg); currentAmount = Integer.parseInt(arg);
@ -50,16 +59,30 @@ public class DiceRollCommand implements MessageCommand
currentDice = new Dice(currentDice); currentDice = new Dice(currentDice);
} }
if(currentAmount > 100)
{
event.getMessage().reply("Too many rolls (`" + currentAmount + "`)!").queue();
return;
}
lastPushedDice = currentDice.getUUID(); lastPushedDice = currentDice.getUUID();
dicesToRoll.put(currentDice, currentAmount); dicesToRoll.put(currentDice, currentAmount);
totalRolls += currentAmount;
} }
else if(arg.matches(diceRegex)) else if(arg.matches(diceRegex))
{ {
int sides = Integer.parseInt(arg.substring(1)); int sides = Integer.parseInt(arg.substring(1));
if(sides > 10000)
{
event.getMessage().reply("Too many sides (`" + sides + "`)!").queue();
return;
}
if(args.length == 1) if(args.length == 1)
{ {
dicesToRoll.put(new Dice(sides), 1); dicesToRoll.put(new Dice(sides), 1);
totalRolls++;
} else } else
{ {
if(currentDice != null) if(currentDice != null)
@ -68,6 +91,7 @@ public class DiceRollCommand implements MessageCommand
{ {
dicesToRoll.put(currentDice, 1); dicesToRoll.put(currentDice, 1);
lastPushedDice = currentDice.getUUID(); lastPushedDice = currentDice.getUUID();
totalRolls++;
} }
} }
@ -81,12 +105,14 @@ public class DiceRollCommand implements MessageCommand
if(currentDice != null) if(currentDice != null)
{ {
dicesToRoll.put(currentDice, 1); dicesToRoll.put(currentDice, 1);
totalRolls++;
} }
} else } else
{ {
if(!lastPushedDice.equals(currentDice.getUUID())) if(!lastPushedDice.equals(currentDice.getUUID()))
{ {
dicesToRoll.put(new Dice(currentDice), 1); dicesToRoll.put(new Dice(currentDice), 1);
totalRolls++;
} }
} }
@ -101,6 +127,12 @@ public class DiceRollCommand implements MessageCommand
} }
} }
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setColor(Cache.getBotColor());
embedBuilder.setAuthor(event.getAuthor().getAsTag(), null, event.getAuthor().getAvatarUrl());
embedBuilder.setTitle("Dice Roll");
StringBuilder message = new StringBuilder(); StringBuilder message = new StringBuilder();
int total = 0; int total = 0;
@ -115,14 +147,24 @@ public class DiceRollCommand implements MessageCommand
message.append(", "); message.append(", ");
} }
message.append(dice.getValue()); message.append("`").append(dice.getValue()).append("`");
total += dice.getValue(); total += dice.getValue();
} }
message.append("\n\n").append("**Total**: ").append(total); // discord doesn't allow embed fields to be longer than 1024 and errors out
if(message.length() > 1024)
{
event.getMessage().reply("Too many rolls!").queue();
return;
}
event.getMessage().reply(message.toString()).queue(); embedBuilder.addField("\uD83C\uDFB2 Rolls", message.toString(), false);
embedBuilder.addField("✨ Total", totalRolls + " rolls: " + total, false);
event.getMessage().replyEmbeds(embedBuilder.build()).queue();
} }
} }

View File

@ -44,6 +44,9 @@ public class ClearCommand extends SlashCommandImpl
OptionMapping amountOption = event.getOption("amount"); OptionMapping amountOption = event.getOption("amount");
int toDeleteAmount = amountOption == null ? 1 : amountOption.getAsInt(); int toDeleteAmount = amountOption == null ? 1 : amountOption.getAsInt();
// cap the amount to avoid abuse.
if(toDeleteAmount > ClearChat.getMaxAmount()) toDeleteAmount = 0;
error = ClearChat.checkDeleteAmount(toDeleteAmount); error = ClearChat.checkDeleteAmount(toDeleteAmount);
if(error != null) if(error != null)
{ {