Skip to content

Commit

Permalink
added adventcalendar prize draw
Browse files Browse the repository at this point in the history
  • Loading branch information
Kesuaheli committed Dec 13, 2023
1 parent 9d12e0b commit af518ad
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
7 changes: 7 additions & 0 deletions data/lang/de.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 8 additions & 1 deletion data/lang/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions modules/adventcalendar/chatCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
},
}
}
Expand All @@ -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
}

}
Expand Down
67 changes: 67 additions & 0 deletions modules/adventcalendar/handlerSubcommandDraw.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit af518ad

Please sign in to comment.