Skip to content

Commit

Permalink
feat(Random): added random command setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Kesuaheli committed Dec 27, 2024
1 parent c76d5d6 commit 0022d63
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 0 deletions.
11 changes: 11 additions & 0 deletions data/lang/de.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions data/lang/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions event/command/commandBase.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"cake4everybot/modules/adventcalendar"
"cake4everybot/modules/birthday"
"cake4everybot/modules/info"
"cake4everybot/modules/random"
"cake4everybot/modules/secretsanta"
"cake4everybot/util"
"fmt"
Expand Down Expand Up @@ -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
Expand Down
76 changes: 76 additions & 0 deletions modules/random/chatCommand.go
Original file line number Diff line number Diff line change
@@ -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
}
40 changes: 40 additions & 0 deletions modules/random/handleSubcommandCoin.go
Original file line number Diff line number Diff line change
@@ -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() {
}
58 changes: 58 additions & 0 deletions modules/random/handleSubcommandDice.go
Original file line number Diff line number Diff line change
@@ -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() {
}
39 changes: 39 additions & 0 deletions modules/random/handleSubcommandTeams.go
Original file line number Diff line number Diff line change
@@ -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() {
}
16 changes: 16 additions & 0 deletions modules/random/randombase.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 0022d63

Please sign in to comment.