From 0022d63128b2db19baac23887e4310a76435de2e Mon Sep 17 00:00:00 2001 From: Kesuaheli Date: Fri, 27 Dec 2024 23:27:28 +0100 Subject: [PATCH] feat(Random): added random command setup --- data/lang/de.yaml | 11 ++++ data/lang/en.yaml | 11 ++++ event/command/commandBase.go | 2 + modules/random/chatCommand.go | 76 +++++++++++++++++++++++++ modules/random/handleSubcommandCoin.go | 40 +++++++++++++ modules/random/handleSubcommandDice.go | 58 +++++++++++++++++++ modules/random/handleSubcommandTeams.go | 39 +++++++++++++ modules/random/randombase.go | 16 ++++++ 8 files changed, 253 insertions(+) create mode 100644 modules/random/chatCommand.go create mode 100644 modules/random/handleSubcommandCoin.go create mode 100644 modules/random/handleSubcommandDice.go create mode 100644 modules/random/handleSubcommandTeams.go create mode 100644 modules/random/randombase.go diff --git a/data/lang/de.yaml b/data/lang/de.yaml index aeb4d95..c9f1492 100644 --- a/data/lang/de.yaml +++ b/data/lang/de.yaml @@ -123,6 +123,17 @@ discord.command: msg.winner.details: "__Gewinner: %s__\nTickets: %d/24\nGewinnchance: %.2f%%" msg.winner.congratulation: "Herzlichen Glückwunsch, %s! :heart:\nFrohe Weihnachten an alle!" + random: + base: zufall + base.description: Einige nützliche Zufallsgenerator Befehle + + option.coin: münze + option.coin.description: Wirf eine Münze + option.dice: würfel + option.dice.description: Würfle ein Würfel + option.teams: gruppen + option.teams.description: Teile Gruppen zu + secretsanta: base: Wichteln display: Wichteln diff --git a/data/lang/en.yaml b/data/lang/en.yaml index 28194dd..6b145e2 100644 --- a/data/lang/en.yaml +++ b/data/lang/en.yaml @@ -123,6 +123,17 @@ discord.command: msg.winner.details: "__Winner: %s__\nTickets: %d/24\nProbability of winning: %.2f%%" msg.winner.congratulation: "Congratulations, %s! :heart:\nMerry XMas everyone!" + random: + base: random + base.description: Some useful Random Number Generator commands + + option.coin: coin + option.coin.description: Flip a coin + option.dice: dice + option.dice.description: Roll a dice + option.teams: teams + option.teams.description: Generate teams + secretsanta: base: Secret Santa display: Secret Santa diff --git a/event/command/commandBase.go b/event/command/commandBase.go index ec6ea74..f1df7c1 100644 --- a/event/command/commandBase.go +++ b/event/command/commandBase.go @@ -19,6 +19,7 @@ import ( "cake4everybot/modules/adventcalendar" "cake4everybot/modules/birthday" "cake4everybot/modules/info" + "cake4everybot/modules/random" "cake4everybot/modules/secretsanta" "cake4everybot/util" "fmt" @@ -72,6 +73,7 @@ func Register(s *discordgo.Session, guildID string) error { commandsList = append(commandsList, &birthday.Chat{}) commandsList = append(commandsList, &info.Chat{}) commandsList = append(commandsList, &adventcalendar.Chat{}) + commandsList = append(commandsList, &random.Chat{}) commandsList = append(commandsList, &secretsanta.Chat{}) commandsList = append(commandsList, &secretsanta.MsgCmd{}) // messsage commands diff --git a/modules/random/chatCommand.go b/modules/random/chatCommand.go new file mode 100644 index 0000000..ea7352f --- /dev/null +++ b/modules/random/chatCommand.go @@ -0,0 +1,76 @@ +package random + +import ( + "cake4everybot/data/lang" + "cake4everybot/util" + + "github.com/bwmarrin/discordgo" +) + +const ( + // Prefix for translation key, i.e.: + // key := tp+"base" // => random + tp = "discord.command.random." +) + +type Chat struct { + randomBase + ID string +} + +type subcommand interface { + appCmd() *discordgo.ApplicationCommandOption + handle() +} + +func (cmd Chat) AppCmd() *discordgo.ApplicationCommand { + options := []*discordgo.ApplicationCommandOption{ + cmd.subcommandCoin().appCmd(), + cmd.subcommandDice().appCmd(), + cmd.subcommandTeams().appCmd(), + } + + return &discordgo.ApplicationCommand{ + Name: lang.GetDefault(tp + "base"), + NameLocalizations: util.TranslateLocalization(tp + "base"), + Description: lang.GetDefault(tp + "base.description"), + DescriptionLocalizations: util.TranslateLocalization(tp + "base.description"), + Options: options, + } +} + +func (cmd Chat) Handle(s *discordgo.Session, i *discordgo.InteractionCreate) { + cmd.InteractionUtil = util.InteractionUtil{Session: s, Interaction: i} + cmd.member = i.Member + cmd.user = i.User + if i.Member != nil { + cmd.user = i.Member.User + } else if i.User != nil { + cmd.member = &discordgo.Member{User: i.User} + } + + subcommandName := i.ApplicationCommandData().Options[0].Name + var sub subcommand + switch subcommandName { + case lang.GetDefault(tp + "option.dice"): + sub = cmd.subcommandDice() + case lang.GetDefault(tp + "option.coin"): + sub = cmd.subcommandCoin() + case lang.GetDefault(tp + "option.teams"): + sub = cmd.subcommandTeams() + default: + return + } + + sub.handle() +} + +// SetID sets the registered command ID for internal uses after uploading to discord +func (cmd *Chat) SetID(id string) { + cmd.ID = id +} + +// GetID gets the registered command ID +func (cmd Chat) GetID() string { + return cmd.ID +} diff --git a/modules/random/handleSubcommandCoin.go b/modules/random/handleSubcommandCoin.go new file mode 100644 index 0000000..83fb451 --- /dev/null +++ b/modules/random/handleSubcommandCoin.go @@ -0,0 +1,40 @@ +package random + +import ( + "cake4everybot/data/lang" + "cake4everybot/util" + "math/rand/v2" + + "github.com/bwmarrin/discordgo" +) + +// The set subcommand. Used when executing the slash-command "/random coin". +type subcommandCoin struct { + Chat + *discordgo.ApplicationCommandInteractionDataOption +} + +// Constructor for subcommandCoin, the struct for the slash-command "/random coin". +func (cmd Chat) subcommandCoin() subcommandCoin { + var subcommand *discordgo.ApplicationCommandInteractionDataOption + if cmd.Interaction != nil { + subcommand = cmd.Interaction.ApplicationCommandData().Options[0] + } + return subcommandCoin{ + Chat: cmd, + ApplicationCommandInteractionDataOption: subcommand, + } +} + +func (cmd subcommandCoin) appCmd() *discordgo.ApplicationCommandOption { + return &discordgo.ApplicationCommandOption{ + Type: discordgo.ApplicationCommandOptionSubCommand, + Name: lang.GetDefault(tp + "option.coin"), + NameLocalizations: *util.TranslateLocalization(tp + "option.coin"), + Description: lang.GetDefault(tp + "option.coin.description"), + DescriptionLocalizations: *util.TranslateLocalization(tp + "option.coin.description"), + } +} + +func (cmd subcommandCoin) handle() { +} diff --git a/modules/random/handleSubcommandDice.go b/modules/random/handleSubcommandDice.go new file mode 100644 index 0000000..c862028 --- /dev/null +++ b/modules/random/handleSubcommandDice.go @@ -0,0 +1,58 @@ +package random + +import ( + "cake4everybot/data/lang" + "cake4everybot/util" + "math/rand/v2" + + "github.com/bwmarrin/discordgo" +) + +// The set subcommand. Used when executing the slash-command "/random dice". +type subcommandDice struct { + Chat + *discordgo.ApplicationCommandInteractionDataOption +} + +// Constructor for subcommandDice, the struct for the slash-command "/random dice". +func (cmd Chat) subcommandDice() subcommandDice { + var subcommand *discordgo.ApplicationCommandInteractionDataOption + if cmd.Interaction != nil { + subcommand = cmd.Interaction.ApplicationCommandData().Options[0] + } + return subcommandDice{ + Chat: cmd, + ApplicationCommandInteractionDataOption: subcommand, + } +} + +func (cmd subcommandDice) appCmd() *discordgo.ApplicationCommandOption { + options := []*discordgo.ApplicationCommandOption{ + cmd.optionRange(), + } + + return &discordgo.ApplicationCommandOption{ + Type: discordgo.ApplicationCommandOptionSubCommand, + Name: lang.GetDefault(tp + "option.dice"), + NameLocalizations: *util.TranslateLocalization(tp + "option.dice"), + Description: lang.GetDefault(tp + "option.dice.description"), + DescriptionLocalizations: *util.TranslateLocalization(tp + "option.dice.description"), + Options: options, + } +} + +func (cmd subcommandDice) optionRange() *discordgo.ApplicationCommandOption { + minValueTwo := float64(2) + return &discordgo.ApplicationCommandOption{ + Type: discordgo.ApplicationCommandOptionInteger, + Name: lang.GetDefault(tp + "option.dice.option.range"), + NameLocalizations: *util.TranslateLocalization(tp + "option.dice.option.range"), + Description: lang.GetDefault(tp + "option.dice.option.range.description"), + DescriptionLocalizations: *util.TranslateLocalization(tp + "option.dice.option.range.description"), + Required: false, + MinValue: &minValueTwo, + } +} + +func (cmd subcommandDice) handle() { +} diff --git a/modules/random/handleSubcommandTeams.go b/modules/random/handleSubcommandTeams.go new file mode 100644 index 0000000..48c0127 --- /dev/null +++ b/modules/random/handleSubcommandTeams.go @@ -0,0 +1,39 @@ +package random + +import ( + "cake4everybot/data/lang" + "cake4everybot/util" + + "github.com/bwmarrin/discordgo" +) + +// The set subcommand. Used when executing the slash-command "/random teams". +type subcommandTeams struct { + Chat + *discordgo.ApplicationCommandInteractionDataOption +} + +// Constructor for subcommandTeams, the struct for the slash-command "/random teams". +func (cmd Chat) subcommandTeams() subcommandTeams { + var subcommand *discordgo.ApplicationCommandInteractionDataOption + if cmd.Interaction != nil { + subcommand = cmd.Interaction.ApplicationCommandData().Options[0] + } + return subcommandTeams{ + Chat: cmd, + ApplicationCommandInteractionDataOption: subcommand, + } +} + +func (cmd subcommandTeams) appCmd() *discordgo.ApplicationCommandOption { + return &discordgo.ApplicationCommandOption{ + Type: discordgo.ApplicationCommandOptionSubCommand, + Name: lang.GetDefault(tp + "option.teams"), + NameLocalizations: *util.TranslateLocalization(tp + "option.teams"), + Description: lang.GetDefault(tp + "option.teams.description"), + DescriptionLocalizations: *util.TranslateLocalization(tp + "option.teams.description"), + } +} + +func (cmd subcommandTeams) handle() { +} diff --git a/modules/random/randombase.go b/modules/random/randombase.go new file mode 100644 index 0000000..b01db2f --- /dev/null +++ b/modules/random/randombase.go @@ -0,0 +1,16 @@ +package random + +import ( + "cake4everybot/logger" + "cake4everybot/util" + + "github.com/bwmarrin/discordgo" +) + +var log = logger.New("Random") + +type randomBase struct { + util.InteractionUtil + member *discordgo.Member + user *discordgo.User +}