Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding useful random number generator commands #17

Merged
merged 16 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,54 @@ event:
name: ✅
#id:
#animated: true
repeat:
name: 🔁
#id:
#animated: true

# Emoji for entering the advent calendar giveaway
adventcalendar: vote.check
random.coin.heads:
name: 👤
#id:
#animated: true
random.coin.tails:
name: 🪙
#id:
#animated: true
random.coin.flip: repeat
random.coin.reflip: repeat
random.dice.1:
#name: 1️⃣
id: 1322967431106527322
#animated: true
random.dice.2:
#name: 2️⃣
id: 1322967432024817748
#animated: true
random.dice.3:
#name: 3️⃣
id: 1322967433786691724
#animated: true
random.dice.4:
#name: 4️⃣
id: 1322967435170807968
#animated: true
random.dice.5:
#name: 5️⃣
id: 1322967436307206145
#animated: true
random.dice.6:
#name: 6️⃣
id: 1322967437800378411
#animated: true
random.dice.rolling:
#name: 🎲
id: 1322968516311126057
animated: true
random.dice.reroll: repeat
random.teams.resplit_size: repeat
random.teams.resplit_amount: repeat
secretsanta: vote.yes
secretsanta.invite.show_match:
name: 🎁
Expand Down
27 changes: 27 additions & 0 deletions data/lang/de.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ discord.command:
no: Nein
msg.self_hidden: Warum ist das unsichtbar?
msg.self_hidden.desc: Da du deinen Geburtstag als nicht sichtbar eingetragen hast, kannst diese Nachricht nur du sehen. Du kannst diese Nachricht nun schließen.
msg.page: Seite %d/%d

birthday:
base: geburtstag
Expand Down Expand Up @@ -123,6 +124,32 @@ 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
display: Zufallsgenerator

option.coin: münze
option.coin.description: Wirf eine Münze
option.dice: würfel
option.dice.description: Würfle ein Würfel
option.dice.option.range: bereich
option.dice.option.range.description: "Wie viele Zahlen soll der Würfel haben? (Standard: 6)"
option.teams: gruppen
option.teams.description: Teile Gruppen zu
option.teams.option.members: mitglieder
option.teams.option.members.description: Welche Mitglieder sollen in Gruppen geteilt werden?
option.teams.option.team_size: gruppengröße
option.teams.option.team_size.description: Wie viele Mitglieder sollen in einer Gruppe sein?
option.teams.option.team_amount: gruppenanzahl
option.teams.option.team_amount.description: Wie viele Gruppen sollen erstellt werden?

msg.dice.roll: You rolled a %d
msg.teams.missing_option: "Fehler: Du musst entweder die Gruppengröße oder die Gruppenanzahl angeben!"
msg.teams.multiple_options: "Fehler: Du kannst nicht gleichzeitig die Gruppengröße und die Gruppenanzahl angeben!"
msg.teams.title: Gruppen
msg.teams.team: Gruppe %d

secretsanta:
base: Wichteln
display: Wichteln
Expand Down
27 changes: 27 additions & 0 deletions data/lang/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ discord.command:
no: No
msg.self_hidden: Why is this invisible?
msg.self_hidden.desc: Since you've set your birthday to not be visible, this message is also only visible to you. You can close this message now.
msg.page: Page %d/%d

birthday:
base: birthday
Expand Down Expand Up @@ -123,6 +124,32 @@ 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
display: Random Generator

option.coin: coin
option.coin.description: Flip a coin
option.dice: dice
option.dice.description: Roll a dice
option.dice.option.range: range
option.dice.option.range.description: "How many sides does the dice have? (Default: 6)"
option.teams: teams
option.teams.description: Generate teams
option.teams.option.members: members
option.teams.option.members.description: Which members should be splitted into teams?
option.teams.option.team_size: size
option.teams.option.team_size.description: How many members should be in each team?
option.teams.option.team_amount: amount
option.teams.option.team_amount.description: How many teams should be created?

msg.dice.roll: You rolled a %d
msg.teams.missing_option: "Error: You need to specify either the team size or the team amount!"
msg.teams.multiple_options: "Error: You can't specify both the team size and the team amount at the same time!"
msg.teams.title: Teams
msg.teams.team: Team %d

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
2 changes: 2 additions & 0 deletions event/component/componentBase.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package component
import (
"cake4everybot/logger"
"cake4everybot/modules/adventcalendar"
"cake4everybot/modules/random"
"cake4everybot/modules/secretsanta"

"github.com/bwmarrin/discordgo"
Expand Down Expand Up @@ -34,6 +35,7 @@ func Register() {
var componentList []Component

componentList = append(componentList, adventcalendar.Component{})
componentList = append(componentList, random.Component{})
componentList = append(componentList, secretsanta.Component{})

if len(componentList) == 0 {
Expand Down
19 changes: 3 additions & 16 deletions modules/adventcalendar/midnight.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,7 @@ func splitEntriesToEmbeds(s *discordgo.Session, entries []database.GiveawayEntry
for _, e := range entries {
totalTickets += e.Weight
}
numEmbeds := len(entries)/25 + 1
embeds := make([]*discordgo.MessageEmbed, 0, numEmbeds)
for i, e := range entries {
if i%25 == 0 {
new := &discordgo.MessageEmbed{}
if numEmbeds > 1 {
new.Description = fmt.Sprintf("Page %d/%d", i/25+1, numEmbeds)
}
util.SetEmbedFooter(s, "module.adventcalendar.embed_footer", new)
embeds = append(embeds, new)
}

embeds[len(embeds)-1].Fields = append(embeds[len(embeds)-1].Fields, e.ToEmbedField(s, totalTickets))
}

return embeds
return util.SplitToEmbedFields(s, entries, 0, "module.adventcalendar.embed_footer", func(e database.GiveawayEntry, _ int) *discordgo.MessageEmbedField {
return e.ToEmbedField(s, totalTickets)
})
}
80 changes: 80 additions & 0 deletions modules/random/chatCommand.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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."
)

// The Chat (slash) command of the random package. Has a few sub commands and options to use all
// features through a single chat command.
type Chat struct {
randomBase
ID string
}

type subcommand interface {
appCmd() *discordgo.ApplicationCommandOption
handle()
}

// AppCmd (ApplicationCommand) returns the definition of the chat command
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,
}
}

// Handle handles the functionality of a command
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
}
51 changes: 51 additions & 0 deletions modules/random/component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package random

import (
"cake4everybot/util"
"strings"

"github.com/bwmarrin/discordgo"
)

// The Component of the random package.
type Component struct {
randomBase
data discordgo.MessageComponentInteractionData
}

// Handle handles the functionality of a component.
func (c Component) Handle(s *discordgo.Session, i *discordgo.InteractionCreate) {
c.InteractionUtil = util.InteractionUtil{Session: s, Interaction: i}
c.member = i.Member
c.user = i.User
if i.Member != nil {
c.user = i.Member.User
} else if i.User != nil {
c.member = &discordgo.Member{User: i.User}
}
c.data = i.MessageComponentData()

ids := strings.Split(c.data.CustomID, ".")
// pop the first level identifier
util.ShiftL(ids)

switch util.ShiftL(ids) {
case "coin":
c.subcommandCoin().handleComponent(ids)
return
case "dice":
c.subcommandDice().handleComponent(ids)
return
case "teams":
c.subcommandTeams().handleComponent(ids)
return
default:
log.Printf("Unknown component interaction ID: %s", c.data.CustomID)
}

}

// ID returns the custom ID of the modal to identify the module
func (c Component) ID() string {
return "random"
}
Loading
Loading