roll Implemntation

This commit is contained in:
Ocame
2025-07-17 23:02:05 +02:00
parent e2b9694e09
commit cb221e6bb2
4 changed files with 226 additions and 0 deletions

View File

@ -0,0 +1,101 @@
package de.ocame.wuerfelbot.Simpleroll;
import de.ocame.wuerfelbot.commands.ICommand;
import de.ocame.wuerfelbot.output.DiceOutputFormatter;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
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.Commands;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import java.util.Arrays;
import java.util.List;
public class SimplerollCommand implements ICommand {
private final SimplerollLogic rollLogic;
private final DiceOutputFormatter outputFormatter;
public SimplerollCommand() {
this.rollLogic = new SimplerollLogic();
this.outputFormatter = new DiceOutputFormatter();
}
@Override
public String getName() {
// Der primäre Name für den Text-Befehl
return "roll";
}
@Override
public String getDescription() {
return "Würfelt Würfel nach verschiedenen Notationen (z.B. 9, 9d4, 3w8, 9 +4).";
}
// --- Slash Command Handling (Wird leer gelassen oder eine Fehlermeldung gesendet) ---
@Override
public void executeSlash(SlashCommandInteractionEvent event) {
// Dieser Befehl ist nicht für Slash-Commands vorgesehen.
event.reply(outputFormatter.formatError("Dieser Befehl ist nur als Text-Befehl (!roll oder !r) verfügbar.")).setEphemeral(true).queue();
}
// --- Text Command Handling (Bestehend, mit Erweiterungen) ---
@Override
public void executeText(MessageReceivedEvent event, String[] args) {
if (args.length == 0) {
event.getChannel().sendMessage(outputFormatter.formatError("Verwendung: `!roll <notation> [+<modifikator>]` oder `!r <notation> [+<modifikator>]`")).queue();
return;
}
String notation = args[0];
int modifier = 0;
// Versuche, einen optionalen Modifikator zu parsen, wenn mehr Argumente vorhanden sind
if (args.length > 1) {
// Kombiniere die restlichen Argumente zu einem String und versuche zu parsen
String modifierPart = String.join("", Arrays.copyOfRange(args, 1, args.length));
try {
if (modifierPart.startsWith("+") || modifierPart.startsWith("-")) {
modifier = Integer.parseInt(modifierPart); // Parser handhabt +/-
} else {
// Wenn es keine + oder - gibt, aber weitere Argumente, behandle es als Fehler
event.getChannel().sendMessage(outputFormatter.formatError("Ungültiger Modifikator-Format. Verwende `+4` oder `-2`.")).queue();
return;
}
} catch (NumberFormatException e) {
event.getChannel().sendMessage(outputFormatter.formatError("Ungültiger Modifikator: '" + modifierPart + "'. Bitte gib eine Zahl ein.")).queue();
return;
}
}
try {
SimplerollLogic.RollResult result = rollLogic.performRoll(notation, modifier);
// EmbedBuilder response = outputFormatter.DCDefaultOutput(getEffectiveName(event) + result.getTitle(), result.getDescription(), result.isGlitch());
EmbedBuilder response = outputFormatter.DCDefaultOutput(getEffectiveName(event) + result.getTitle(), result.getDescription(), false);
event.getChannel().sendMessageEmbeds(response.build()).queue();
} catch (IllegalArgumentException e) {
event.getChannel().sendMessage(outputFormatter.formatError(e.getMessage())).queue();
} catch (Exception e) {
System.err.println("Fehler beim Ausführen von Text-Befehl roll/r: " + e.getMessage());
e.printStackTrace();
event.getChannel().sendMessage(outputFormatter.formatError("Ein unerwarteter Fehler ist beim Würfeln aufgetreten.")).queue();
}
}
@Override
public CommandData getSlashCommandData() {
return null;
}
private String getEffectiveName(MessageReceivedEvent event) {
if (event.getMember() == null) {
return "";
}
return event.getMember().getEffectiveName() + " ";
}
}