From dc7fae2531760e95a8a3acd98fc971a01ef41c83 Mon Sep 17 00:00:00 2001 From: Kesuaheli Date: Sat, 16 Nov 2024 01:36:25 +0100 Subject: [PATCH] feat: added secret santa message command --- data/lang/de.yaml | 9 ++++ data/lang/en.yaml | 9 ++++ event/command/commandBase.go | 2 + modules/secretsanta/handlerMessageSetup.go | 46 ++++++++++++++++++++ modules/secretsanta/messageCommandSetup.go | 49 ++++++++++++++++++++++ modules/secretsanta/secretsantabase.go | 22 ++++++++++ 6 files changed, 137 insertions(+) create mode 100644 modules/secretsanta/handlerMessageSetup.go create mode 100644 modules/secretsanta/messageCommandSetup.go create mode 100644 modules/secretsanta/secretsantabase.go diff --git a/data/lang/de.yaml b/data/lang/de.yaml index 3b268b5..b88edd4 100644 --- a/data/lang/de.yaml +++ b/data/lang/de.yaml @@ -123,6 +123,15 @@ discord.command: msg.winner.details: "__Gewinner: %s__\nTickets: %d/24\nGewinnchance: %.2f%%" msg.winner.congratulation: "Herzlichen Glückwunsch, %s! :heart:\nFrohe Weihnachten an alle!" + secretsanta: + base: wichteln + display: Wichteln + + title: Wichteln + + msg.setup.no_reactions: Diese Nachricht hat keine Reaktionen. Nur Leute, die mit %s reagiert haben, werden eingeschlossen. + msg.setup.not_enough_reactions: Nicht genug Reaktionen um zu starten. Es werden mindestens %d Reaktionen benötigt. + module: adventcalendar: post.message: Noch %d Mal schlafen bis Heilig Abend! Heute öffnet sich das **Türchen %d**. diff --git a/data/lang/en.yaml b/data/lang/en.yaml index 03b5734..d6e5ea9 100644 --- a/data/lang/en.yaml +++ b/data/lang/en.yaml @@ -123,6 +123,15 @@ discord.command: msg.winner.details: "__Winner: %s__\nTickets: %d/24\nProbability of winning: %.2f%%" msg.winner.congratulation: "Congratulations, %s! :heart:\nMerry XMas everyone!" + secretsanta: + setup: Secret Santa + display: Secret Santa + + title: Secret Santa + + msg.setup.no_reactions: This message doesn't have any vote reactions. Only members who reated with %s are included. + msg.setup.not_enough_reactions: Not enough votes to start a game. At least %d votes are required. + module: adventcalendar: post.message: Just sleep %d more times! Its time for **door %d**. diff --git a/event/command/commandBase.go b/event/command/commandBase.go index e89f770..ee709e0 100644 --- a/event/command/commandBase.go +++ b/event/command/commandBase.go @@ -18,6 +18,7 @@ import ( "cake4everybot/modules/adventcalendar" "cake4everybot/modules/birthday" "cake4everybot/modules/info" + "cake4everybot/modules/secretsanta" "cake4everybot/util" "fmt" "log" @@ -69,6 +70,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, &secretsanta.MsgCmd{}) // messsage commands // user commands commandsList = append(commandsList, &birthday.UserShow{}) diff --git a/modules/secretsanta/handlerMessageSetup.go b/modules/secretsanta/handlerMessageSetup.go new file mode 100644 index 0000000..c1822e9 --- /dev/null +++ b/modules/secretsanta/handlerMessageSetup.go @@ -0,0 +1,46 @@ +package secretsanta + +import ( + "cake4everybot/data/lang" + "cake4everybot/util" + +) + +func (cmd MsgCmd) handler() { + const emojiName = "👍" + + msg := cmd.data.Resolved.Messages[cmd.data.TargetID] + if len(msg.Reactions) == 0 { + cmd.ReplyHiddenf(lang.GetDefault(tp+"msg.setup.no_reactions"), emojiName) + return + } + var reaction *discordgo.MessageReactions + for _, r := range msg.Reactions { + if r.Emoji.Name != emojiName { + continue + } + reaction = r + break + } + + if reaction == nil { + cmd.ReplyHiddenf(lang.GetDefault(tp+"msg.setup.no_reactions"), emojiName) + return + } + + emojiID := reaction.Emoji.ID + if emojiID == "" { + emojiID = reaction.Emoji.Name + } + users, err := cmd.Session.MessageReactions(msg.ChannelID, msg.ID, emojiID, 100, "", "") + if err != nil { + log.Printf("Error on get users: %v\n", err) + cmd.ReplyError() + return + } + + if len(users) < 2 { + cmd.ReplyHiddenf(lang.GetDefault(tp+"msg.setup.not_enough_reactions"), 2) + return + } +} diff --git a/modules/secretsanta/messageCommandSetup.go b/modules/secretsanta/messageCommandSetup.go new file mode 100644 index 0000000..355c33b --- /dev/null +++ b/modules/secretsanta/messageCommandSetup.go @@ -0,0 +1,49 @@ +package secretsanta + +import ( + "cake4everybot/data/lang" + "cake4everybot/util" + + "github.com/bwmarrin/discordgo" +) + +type MsgCmd struct { + secretSantaBase + + data discordgo.ApplicationCommandInteractionData + ID string +} + +// Definition of a command. +// E.g., name, description, options, subcommands. +func (cmd *MsgCmd) AppCmd() *discordgo.ApplicationCommand { + return &discordgo.ApplicationCommand{ + Type: discordgo.MessageApplicationCommand, + Name: lang.GetDefault(tp + "setup"), + NameLocalizations: util.TranslateLocalization(tp + "setup"), + } +} + +// Function of a command. +// All things that should happen at execution. +func (cmd *MsgCmd) 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 + } + + cmd.data = cmd.Interaction.ApplicationCommandData() + cmd.handler() +} + +// Sets the registered command ID for internal uses after uploading to discord +func (cmd *MsgCmd) SetID(id string) { + cmd.ID = id +} + +// Gets the registered command ID +func (cmd *MsgCmd) GetID() string { + return cmd.ID +} diff --git a/modules/secretsanta/secretsantabase.go b/modules/secretsanta/secretsantabase.go new file mode 100644 index 0000000..9c8e83f --- /dev/null +++ b/modules/secretsanta/secretsantabase.go @@ -0,0 +1,22 @@ +package secretsanta + +import ( + "cake4everybot/util" + logger "log" + + "github.com/bwmarrin/discordgo" +) + +const ( + // Prefix for translation key, i.e.: + // key := tp+"base" // => adventcalendar + tp = "discord.command.secretsanta." +) + +var log = logger.New(logger.Writer(), "[SecretSanta] ", logger.LstdFlags|logger.Lmsgprefix) + +type secretSantaBase struct { + util.InteractionUtil + member *discordgo.Member + user *discordgo.User +}