From b0337637048df25ccc2e6ffa4594526f9a9a66ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Mon, 19 Dec 2022 17:30:47 +0100 Subject: [PATCH] Improve diceroll looks, implement limits to avoid abuse --- .../hidekobot/commands/base/ClearChat.java | 6 ++- .../commands/message/ClearCommand.java | 3 ++ .../commands/message/DiceRollCommand.java | 50 +++++++++++++++++-- .../commands/slash/ClearCommand.java | 3 ++ 4 files changed, 57 insertions(+), 5 deletions(-) diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/base/ClearChat.java b/src/main/java/wtf/beatrice/hidekobot/commands/base/ClearChat.java index b64546c..828787b 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/base/ClearChat.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/base/ClearChat.java @@ -54,7 +54,8 @@ public class ClearChat // int to keep track of how many messages we actually deleted. 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 ClearChat.getMaxAmount()) toDeleteAmount = 0; + error = ClearChat.checkDeleteAmount(toDeleteAmount); if (error != null) { event.getMessage().reply(error).queue(); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/DiceRollCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/DiceRollCommand.java index 8bb86b0..d5ded36 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/DiceRollCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/DiceRollCommand.java @@ -1,8 +1,10 @@ package wtf.beatrice.hidekobot.commands.message; +import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import org.jetbrains.annotations.Nullable; +import wtf.beatrice.hidekobot.Cache; import wtf.beatrice.hidekobot.objects.Dice; import wtf.beatrice.hidekobot.objects.commands.MessageCommand; @@ -29,16 +31,23 @@ public class DiceRollCommand implements MessageCommand @Override public void runCommand(MessageReceivedEvent event, String label, String[] args) { - LinkedHashMap dicesToRoll = new LinkedHashMap(); + LinkedHashMap dicesToRoll = new LinkedHashMap<>(); String diceRegex = "d[0-9]+"; String amountRegex = "[0-9]+"; Dice currentDice = null; int currentAmount; UUID lastPushedDice = null; + int totalRolls = 0; for(String arg : args) { + if(totalRolls > 200) + { + event.getMessage().reply("Too many total rolls!").queue(); + return; + } + if(arg.matches(amountRegex)) { currentAmount = Integer.parseInt(arg); @@ -50,16 +59,30 @@ public class DiceRollCommand implements MessageCommand currentDice = new Dice(currentDice); } + if(currentAmount > 100) + { + event.getMessage().reply("Too many rolls (`" + currentAmount + "`)!").queue(); + return; + } + lastPushedDice = currentDice.getUUID(); dicesToRoll.put(currentDice, currentAmount); + totalRolls += currentAmount; } else if(arg.matches(diceRegex)) { int sides = Integer.parseInt(arg.substring(1)); + if(sides > 10000) + { + event.getMessage().reply("Too many sides (`" + sides + "`)!").queue(); + return; + } + if(args.length == 1) { dicesToRoll.put(new Dice(sides), 1); + totalRolls++; } else { if(currentDice != null) @@ -68,6 +91,7 @@ public class DiceRollCommand implements MessageCommand { dicesToRoll.put(currentDice, 1); lastPushedDice = currentDice.getUUID(); + totalRolls++; } } @@ -81,12 +105,14 @@ public class DiceRollCommand implements MessageCommand if(currentDice != null) { dicesToRoll.put(currentDice, 1); + totalRolls++; } } else { if(!lastPushedDice.equals(currentDice.getUUID())) { 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(); int total = 0; @@ -115,14 +147,24 @@ public class DiceRollCommand implements MessageCommand message.append(", "); } - message.append(dice.getValue()); + message.append("`").append(dice.getValue()).append("`"); 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(); } } diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearCommand.java index 680772d..73a6013 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearCommand.java @@ -44,6 +44,9 @@ public class ClearCommand extends SlashCommandImpl OptionMapping amountOption = event.getOption("amount"); int toDeleteAmount = amountOption == null ? 1 : amountOption.getAsInt(); + // cap the amount to avoid abuse. + if(toDeleteAmount > ClearChat.getMaxAmount()) toDeleteAmount = 0; + error = ClearChat.checkDeleteAmount(toDeleteAmount); if(error != null) {