From af518ad71a8f038ce395689c440cb33804885729 Mon Sep 17 00:00:00 2001 From: Kesuaheli Date: Wed, 13 Dec 2023 13:40:57 +0100 Subject: [PATCH] added adventcalendar prize draw --- data/lang/de.yaml | 7 ++ data/lang/en.yaml | 9 ++- modules/adventcalendar/chatCommand.go | 10 +++ .../adventcalendar/handlerSubcommandDraw.go | 67 +++++++++++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 modules/adventcalendar/handlerSubcommandDraw.go diff --git a/data/lang/de.yaml b/data/lang/de.yaml index 9370f37..ffe8288 100644 --- a/data/lang/de.yaml +++ b/data/lang/de.yaml @@ -115,6 +115,13 @@ discord.command: adventcalendar: base: adventskalender base.description: Admin Commands für das Adventskalender Giveaway + option.draw: ziehen + option.draw.description: Ziehe einen zufälligen Gewinner aus allen eingetragenen Tickets + + msg.no_entires.draw: Konnte kein Gewinner gezogen werden, da momentan keine Tickets eingetragen sind. + msg.winner.title: Adventskalender Gewinnauslosung + msg.winner.details: "__Gewinner: %s__\nTickets: %d/24\nGewinnchance: %.2f%%" + msg.winner.congratulation: "Herzlichen Glückwunsch, %s! :heart:\nFrohe Weihnachten an alle!" module: adventcalendar: diff --git a/data/lang/en.yaml b/data/lang/en.yaml index 2543bb2..cd31355 100644 --- a/data/lang/en.yaml +++ b/data/lang/en.yaml @@ -115,6 +115,13 @@ discord.command: adventcalendar: base: adventcalendar base.description: Admin commands for the Advent Calendar Giveaway + option.draw: draw + option.draw.description: Draw a random winner from all entered tickets + + msg.no_entires.draw: Tried draw a winner, but there are currently no entries. + msg.winner.title: Advent Calendar Pize Draw + msg.winner.details: "__Winner: %s__\nTickets: %d/24\nProbability of winning: %.2f%%" + msg.winner.congratulation: "Congratulations, %s! :heart:\nMerry XMas everyone!" module: adventcalendar: @@ -125,7 +132,7 @@ module: post.button: Join enter.invalid: This is an old message, you cannot join here anymore! - enter.success: You successfully joined! You know have %d tickets. + enter.success: You successfully joined! You now have %d tickets. enter.already_entered: You already joined for today. (You have %d tickets) embed_footer: Advent Calendar Giveaway diff --git a/modules/adventcalendar/chatCommand.go b/modules/adventcalendar/chatCommand.go index 79b616d..0352631 100644 --- a/modules/adventcalendar/chatCommand.go +++ b/modules/adventcalendar/chatCommand.go @@ -45,6 +45,13 @@ func (Chat) AppCmd() *discordgo.ApplicationCommand { Name: "morning", Description: "Morning trigger", }, + { + Type: discordgo.ApplicationCommandOptionSubCommand, + Name: lang.GetDefault(tp + "option.draw"), + NameLocalizations: *util.TranslateLocalization(tp + "option.draw"), + Description: lang.GetDefault(tp + "option.draw.description"), + DescriptionLocalizations: *util.TranslateLocalization(tp + "option.draw.description"), + }, }, } } @@ -70,6 +77,9 @@ func (cmd Chat) Handle(s *discordgo.Session, i *discordgo.InteractionCreate) { Post(s) cmd.ReplyHidden("Post()") return + case lang.GetDefault(tp + "option.draw"): + cmd.handleSubcommandDraw() + return } } diff --git a/modules/adventcalendar/handlerSubcommandDraw.go b/modules/adventcalendar/handlerSubcommandDraw.go new file mode 100644 index 0000000..17e5f95 --- /dev/null +++ b/modules/adventcalendar/handlerSubcommandDraw.go @@ -0,0 +1,67 @@ +package adventcalendar + +import ( + "cake4everybot/data/lang" + "cake4everybot/util" + "fmt" + "math/rand" + + "github.com/bwmarrin/discordgo" +) + +func (cmd Chat) handleSubcommandDraw() { + var entries []giveawayEntry + for _, e := range getGetAllEntries() { + for i := 0; i < e.weight; i++ { + entries = append(entries, e) + } + } + totalTickets := len(entries) + if totalTickets == 0 { + cmd.ReplyHidden(lang.GetDefault(tp + "msg.no_entries.draw")) + return + } + + rand.Shuffle(len(entries), func(i, j int) { + entries[i], entries[j] = entries[j], entries[i] + }) + winner := entries[rand.Intn(totalTickets-1)] + + member, err := cmd.Session.GuildMember(cmd.Interaction.GuildID, winner.userID) + if err != nil { + log.Printf("WARN: Could not get winner as member '%s' from guild '%s': %v", cmd.Interaction.GuildID, winner.userID, err) + log.Print("Trying to get user instead...") + + user, err := cmd.Session.User(winner.userID) + if err != nil { + log.Printf("ERROR: Could not get winner user '%s': %v", winner.userID, err) + cmd.ReplyError() + return + } + member = &discordgo.Member{User: user} + } + + name := member.Nick + if name == "" { + name = member.User.Username + } + + e := &discordgo.MessageEmbed{ + Title: lang.GetDefault(tp + "msg.winner.title"), + Description: fmt.Sprintf( + lang.GetDefault(tp+"msg.winner.details"), + member.Mention(), + winner.weight, + float64(100*winner.weight)/float64(totalTickets), + ), + Thumbnail: &discordgo.MessageEmbedThumbnail{ + URL: member.AvatarURL(""), + }, + Color: 0x00A000, + Fields: []*discordgo.MessageEmbedField{{ + Value: fmt.Sprintf(lang.GetDefault(tp+"msg.winner.congratulation"), name), + }}, + } + util.SetEmbedFooter(cmd.Session, "module.adventcalendar.embed_footer", e) + cmd.ReplyEmbed(e) +}