Make dice roll support slash commands too
This commit is contained in:
parent
bc0463dd38
commit
91261f04e5
@ -119,6 +119,7 @@ public class HidekoBot
|
|||||||
slashCommandListener.registerCommand(new BotInfoCommand());
|
slashCommandListener.registerCommand(new BotInfoCommand());
|
||||||
slashCommandListener.registerCommand(new ClearCommand());
|
slashCommandListener.registerCommand(new ClearCommand());
|
||||||
slashCommandListener.registerCommand(new CoinFlipCommand());
|
slashCommandListener.registerCommand(new CoinFlipCommand());
|
||||||
|
slashCommandListener.registerCommand(new DiceRollCommand());
|
||||||
slashCommandListener.registerCommand(new DieCommand());
|
slashCommandListener.registerCommand(new DieCommand());
|
||||||
slashCommandListener.registerCommand(new HelpCommand());
|
slashCommandListener.registerCommand(new HelpCommand());
|
||||||
slashCommandListener.registerCommand(new InviteCommand());
|
slashCommandListener.registerCommand(new InviteCommand());
|
||||||
|
@ -1,2 +1,157 @@
|
|||||||
package wtf.beatrice.hidekobot.commands.base;public class DiceRoll {
|
package wtf.beatrice.hidekobot.commands.base;
|
||||||
|
|
||||||
|
import net.dv8tion.jda.api.EmbedBuilder;
|
||||||
|
import net.dv8tion.jda.api.entities.User;
|
||||||
|
import wtf.beatrice.hidekobot.Cache;
|
||||||
|
import wtf.beatrice.hidekobot.objects.Dice;
|
||||||
|
import wtf.beatrice.hidekobot.objects.MessageResponse;
|
||||||
|
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class DiceRoll
|
||||||
|
{
|
||||||
|
public static MessageResponse buildResponse(User author, String[] args)
|
||||||
|
{
|
||||||
|
LinkedHashMap<Dice, Integer> 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)
|
||||||
|
{
|
||||||
|
return new MessageResponse("Too many total rolls!", null);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(arg.matches(amountRegex))
|
||||||
|
{
|
||||||
|
currentAmount = Integer.parseInt(arg);
|
||||||
|
|
||||||
|
if(currentDice == null)
|
||||||
|
{
|
||||||
|
currentDice = new Dice(6);
|
||||||
|
} else {
|
||||||
|
currentDice = new Dice(currentDice);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(currentAmount > 100)
|
||||||
|
{
|
||||||
|
return new MessageResponse("Too many rolls (`" + currentAmount + "`)!", null);
|
||||||
|
}
|
||||||
|
|
||||||
|
lastPushedDice = currentDice.getUUID();
|
||||||
|
dicesToRoll.put(currentDice, currentAmount);
|
||||||
|
totalRolls += currentAmount;
|
||||||
|
}
|
||||||
|
else if(arg.matches(diceRegex))
|
||||||
|
{
|
||||||
|
int sides = Integer.parseInt(arg.substring(1));
|
||||||
|
|
||||||
|
if(sides > 10000)
|
||||||
|
{
|
||||||
|
return new MessageResponse("Too many sides (`" + sides + "`)!", null);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(args.length == 1)
|
||||||
|
{
|
||||||
|
dicesToRoll.put(new Dice(sides), 1);
|
||||||
|
totalRolls++;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
if(currentDice != null)
|
||||||
|
{
|
||||||
|
if(lastPushedDice == null || !lastPushedDice.equals(currentDice.getUUID()))
|
||||||
|
{
|
||||||
|
dicesToRoll.put(currentDice, 1);
|
||||||
|
lastPushedDice = currentDice.getUUID();
|
||||||
|
totalRolls++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
currentDice = new Dice(sides);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(lastPushedDice == null)
|
||||||
|
{
|
||||||
|
if(currentDice != null)
|
||||||
|
{
|
||||||
|
dicesToRoll.put(currentDice, 1);
|
||||||
|
totalRolls++;
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
if(!lastPushedDice.equals(currentDice.getUUID()))
|
||||||
|
{
|
||||||
|
dicesToRoll.put(new Dice(currentDice), 1);
|
||||||
|
totalRolls++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LinkedList<Dice> rolledDices = new LinkedList<>();
|
||||||
|
|
||||||
|
// in case no dice was specified (or invalid), roll a standard 6-sided dice.
|
||||||
|
if(dicesToRoll.isEmpty())
|
||||||
|
{
|
||||||
|
Dice standardDice = new Dice(6);
|
||||||
|
dicesToRoll.put(standardDice, 1);
|
||||||
|
totalRolls = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(Dice dice : dicesToRoll.keySet())
|
||||||
|
{
|
||||||
|
for(int roll = 0; roll < dicesToRoll.get(dice); roll++)
|
||||||
|
{
|
||||||
|
dice.roll();
|
||||||
|
rolledDices.add(new Dice(dice));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
EmbedBuilder embedBuilder = new EmbedBuilder();
|
||||||
|
|
||||||
|
embedBuilder.setColor(Cache.getBotColor());
|
||||||
|
embedBuilder.setAuthor(author.getAsTag(), null, author.getAvatarUrl());
|
||||||
|
embedBuilder.setTitle("Dice Roll");
|
||||||
|
|
||||||
|
StringBuilder message = new StringBuilder();
|
||||||
|
int total = 0;
|
||||||
|
|
||||||
|
int previousDiceSides = 0;
|
||||||
|
for (Dice dice : rolledDices) {
|
||||||
|
int diceSize = dice.getSides();
|
||||||
|
|
||||||
|
if (previousDiceSides != diceSize) {
|
||||||
|
message.append("\nd").append(diceSize).append(": ");
|
||||||
|
previousDiceSides = diceSize;
|
||||||
|
} else if (previousDiceSides != 0) {
|
||||||
|
message.append(", ");
|
||||||
|
}
|
||||||
|
|
||||||
|
message.append("`").append(dice.getValue()).append("`");
|
||||||
|
|
||||||
|
total += dice.getValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
// discord doesn't allow embed fields to be longer than 1024 and errors out
|
||||||
|
if(message.length() > 1024)
|
||||||
|
{
|
||||||
|
return new MessageResponse("Too many rolls!", null);
|
||||||
|
}
|
||||||
|
|
||||||
|
embedBuilder.addField("\uD83C\uDFB2 Rolls", message.toString(), false);
|
||||||
|
|
||||||
|
String rolls = totalRolls == 1 ? "roll" : "rolls";
|
||||||
|
|
||||||
|
embedBuilder.addField("✨ Total", totalRolls + " " + rolls + ": " + total, false);
|
||||||
|
|
||||||
|
return new MessageResponse(null, embedBuilder.build());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,9 @@ import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import wtf.beatrice.hidekobot.Cache;
|
import wtf.beatrice.hidekobot.Cache;
|
||||||
|
import wtf.beatrice.hidekobot.commands.base.DiceRoll;
|
||||||
import wtf.beatrice.hidekobot.objects.Dice;
|
import wtf.beatrice.hidekobot.objects.Dice;
|
||||||
|
import wtf.beatrice.hidekobot.objects.MessageResponse;
|
||||||
import wtf.beatrice.hidekobot.objects.commands.CommandCategory;
|
import wtf.beatrice.hidekobot.objects.commands.CommandCategory;
|
||||||
import wtf.beatrice.hidekobot.objects.commands.MessageCommand;
|
import wtf.beatrice.hidekobot.objects.commands.MessageCommand;
|
||||||
|
|
||||||
@ -56,150 +58,17 @@ 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<>();
|
|
||||||
String diceRegex = "d[0-9]+";
|
|
||||||
String amountRegex = "[0-9]+";
|
|
||||||
|
|
||||||
Dice currentDice = null;
|
MessageResponse response = DiceRoll.buildResponse(event.getAuthor(), args);
|
||||||
int currentAmount;
|
|
||||||
UUID lastPushedDice = null;
|
|
||||||
int totalRolls = 0;
|
|
||||||
|
|
||||||
for(String arg : args)
|
if(response.getContent() != null)
|
||||||
{
|
{
|
||||||
if(totalRolls > 200)
|
event.getMessage().reply(response.getContent()).queue();
|
||||||
{
|
} else if(response.getEmbed() != null)
|
||||||
event.getMessage().reply("Too many total rolls!").queue();
|
{
|
||||||
return;
|
event.getMessage().replyEmbeds(response.getEmbed()).queue();
|
||||||
}
|
|
||||||
|
|
||||||
if(arg.matches(amountRegex))
|
|
||||||
{
|
|
||||||
currentAmount = Integer.parseInt(arg);
|
|
||||||
|
|
||||||
if(currentDice == null)
|
|
||||||
{
|
|
||||||
currentDice = new Dice(6);
|
|
||||||
} else {
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
if(lastPushedDice == null || !lastPushedDice.equals(currentDice.getUUID()))
|
|
||||||
{
|
|
||||||
dicesToRoll.put(currentDice, 1);
|
|
||||||
lastPushedDice = currentDice.getUUID();
|
|
||||||
totalRolls++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
currentDice = new Dice(sides);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(lastPushedDice == null)
|
|
||||||
{
|
|
||||||
if(currentDice != null)
|
|
||||||
{
|
|
||||||
dicesToRoll.put(currentDice, 1);
|
|
||||||
totalRolls++;
|
|
||||||
}
|
|
||||||
} else
|
|
||||||
{
|
|
||||||
if(!lastPushedDice.equals(currentDice.getUUID()))
|
|
||||||
{
|
|
||||||
dicesToRoll.put(new Dice(currentDice), 1);
|
|
||||||
totalRolls++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LinkedList<Dice> rolledDices = new LinkedList<>();
|
|
||||||
|
|
||||||
// in case no dice was specified (or invalid), roll a standard 6-sided dice.
|
|
||||||
if(dicesToRoll.isEmpty())
|
|
||||||
{
|
|
||||||
Dice standardDice = new Dice(6);
|
|
||||||
dicesToRoll.put(standardDice, 1);
|
|
||||||
totalRolls = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(Dice dice : dicesToRoll.keySet())
|
|
||||||
{
|
|
||||||
for(int roll = 0; roll < dicesToRoll.get(dice); roll++)
|
|
||||||
{
|
|
||||||
dice.roll();
|
|
||||||
rolledDices.add(new Dice(dice));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
int previousDiceSides = 0;
|
|
||||||
for (Dice dice : rolledDices) {
|
|
||||||
int diceSize = dice.getSides();
|
|
||||||
|
|
||||||
if (previousDiceSides != diceSize) {
|
|
||||||
message.append("\nd").append(diceSize).append(": ");
|
|
||||||
previousDiceSides = diceSize;
|
|
||||||
} else if (previousDiceSides != 0) {
|
|
||||||
message.append(", ");
|
|
||||||
}
|
|
||||||
|
|
||||||
message.append("`").append(dice.getValue()).append("`");
|
|
||||||
|
|
||||||
total += dice.getValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
embedBuilder.addField("\uD83C\uDFB2 Rolls", message.toString(), false);
|
|
||||||
|
|
||||||
String rolls = totalRolls == 1 ? "roll" : "rolls";
|
|
||||||
|
|
||||||
embedBuilder.addField("✨ Total", totalRolls + " " + rolls + ": " + total, false);
|
|
||||||
|
|
||||||
|
|
||||||
event.getMessage().replyEmbeds(embedBuilder.build()).queue();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,47 +8,46 @@ import net.dv8tion.jda.api.interactions.commands.OptionType;
|
|||||||
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
||||||
import net.dv8tion.jda.api.interactions.commands.build.Commands;
|
import net.dv8tion.jda.api.interactions.commands.build.Commands;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import wtf.beatrice.hidekobot.commands.base.DiceRoll;
|
||||||
import wtf.beatrice.hidekobot.commands.base.Say;
|
import wtf.beatrice.hidekobot.commands.base.Say;
|
||||||
|
import wtf.beatrice.hidekobot.objects.MessageResponse;
|
||||||
import wtf.beatrice.hidekobot.objects.commands.SlashCommandImpl;
|
import wtf.beatrice.hidekobot.objects.commands.SlashCommandImpl;
|
||||||
|
|
||||||
public class SayCommand extends SlashCommandImpl
|
public class DiceRollCommand extends SlashCommandImpl
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public CommandData getSlashCommandData()
|
public CommandData getSlashCommandData()
|
||||||
{
|
{
|
||||||
|
|
||||||
return Commands.slash("say", "Make the bot say something.")
|
return Commands.slash("diceroll", "Roll dice. You can roll multiple dice at the same time.")
|
||||||
.addOption(OptionType.STRING, "text",
|
.addOption(OptionType.STRING, "query",
|
||||||
"The message to send.",
|
"The dice to roll.",
|
||||||
true,
|
false,
|
||||||
false)
|
false);
|
||||||
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Say.getPermission()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void runSlashCommand(@NotNull SlashCommandInteractionEvent event)
|
public void runSlashCommand(@NotNull SlashCommandInteractionEvent event)
|
||||||
{
|
{
|
||||||
MessageChannel channel = event.getChannel();
|
event.deferReply().queue();
|
||||||
|
|
||||||
// get the text to send
|
OptionMapping textOption = event.getOption("query");
|
||||||
OptionMapping textOption = event.getOption("text");
|
|
||||||
String messageContent = "";
|
String messageContent = "";
|
||||||
if(textOption != null)
|
if(textOption != null)
|
||||||
{
|
{
|
||||||
messageContent = textOption.getAsString();
|
messageContent = textOption.getAsString();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(textOption == null || messageContent.isEmpty())
|
String[] args = messageContent.split("\\s");
|
||||||
{
|
|
||||||
event.reply("\uD83D\uDE20 Hey, you have to tell me what to say!")
|
|
||||||
.setEphemeral(true)
|
|
||||||
.queue();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
channel.sendMessage(messageContent).queue();
|
MessageResponse response = DiceRoll.buildResponse(event.getUser(), args);
|
||||||
event.reply("Message sent! ✨")
|
|
||||||
.setEphemeral(true)
|
if(response.getContent() != null)
|
||||||
.queue();
|
{
|
||||||
|
event.getHook().editOriginal(response.getContent()).queue();
|
||||||
|
} else if(response.getEmbed() != null)
|
||||||
|
{
|
||||||
|
event.getHook().editOriginalEmbeds(response.getEmbed()).queue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user