diff --git a/config.yaml b/config.yaml index 5ae1f5e..428b9d2 100644 --- a/config.yaml +++ b/config.yaml @@ -33,26 +33,42 @@ event: adventcalendar: images: data/images/adventcalendar - # Name: The name of this emoji, e.g. '🎅', '❤️' when a default emoji - # ID: The snowflake ID if when a custom emoji - # Animated: Whether this emoji is animated. Defaults to false - emoji.name: ✅ - #emoji.id: - #emoji.animated: true twitch_giveaway: # The amount of points a single giveaway ticket costs. ticket_cost: 1000 - # Cooldown in minutes before beeing able to by another ticket + # Cooldown in minutes before beeing able to buy another ticket cooldown: 15 # the filepath for of the json giveaway prizes prizes: twitch/prizes.json # the filepath for storing the giveaway cooldown times times: twitch/times.json + emoji: + # Configuration for emojis used by the bot + # Name: The name of this emoji, e.g. '🎅', '❤️' when a default emoji + # ID: The snowflake ID when a custom emoji + # Animated: Whether this emoji is animated. Defaults to false if not set + + # Voting for yes + vote.yes: + name: 👍 + #id: + #animated: true + # Voting for no + vote.no: + name: 👎 + #id: + #animated: true + # Emoji for entering the advent calendar giveaway + adventcalendar: + name: ✅ + #id: + #animated: true + secretsanta: vote.yes + webserver: favicon: webserver/favicon.png - birthday_hour: 8 # Time to trigger daily birthday check (24h format) twitch: name: c4e_bot diff --git a/modules/adventcalendar/post.go b/modules/adventcalendar/post.go index 2269494..f5aa75b 100644 --- a/modules/adventcalendar/post.go +++ b/modules/adventcalendar/post.go @@ -68,11 +68,7 @@ func postData(t time.Time) *discordgo.MessageSend { fmt.Sprintf("%s.post.%s", Component.ID(Component{}), t.Format("2006.01.02")), lang.GetDefault("module.adventcalendar.post.button"), discordgo.PrimaryButton, - discordgo.ComponentEmoji{ - Name: viper.GetString("event.adventcalendar.emoji.name"), - ID: viper.GetString("event.adventcalendar.emoji.id"), - Animated: viper.GetBool("event.adventcalendar.emoji.animated"), - }, + util.GetConfigComponentEmoji("adventcalendar"), ), }}, } diff --git a/modules/secretsanta/handlerMessageSetup.go b/modules/secretsanta/handlerMessageSetup.go index c1822e9..1d55db6 100644 --- a/modules/secretsanta/handlerMessageSetup.go +++ b/modules/secretsanta/handlerMessageSetup.go @@ -7,32 +7,32 @@ import ( ) func (cmd MsgCmd) handler() { - const emojiName = "👍" + joinEmoji := util.GetConfigEmoji("secretsanta") + joinEmojiID := joinEmoji.ID + if joinEmojiID == "" { + joinEmojiID = joinEmoji.Name + } msg := cmd.data.Resolved.Messages[cmd.data.TargetID] if len(msg.Reactions) == 0 { - cmd.ReplyHiddenf(lang.GetDefault(tp+"msg.setup.no_reactions"), emojiName) + cmd.ReplyHiddenf(lang.GetDefault(tp+"msg.setup.no_reactions"), joinEmojiID) return } - var reaction *discordgo.MessageReactions + var hasReaction bool for _, r := range msg.Reactions { - if r.Emoji.Name != emojiName { + if !util.CompareEmoji(r.Emoji, &joinEmoji) { continue } - reaction = r + hasReaction = true break } - if reaction == nil { - cmd.ReplyHiddenf(lang.GetDefault(tp+"msg.setup.no_reactions"), emojiName) + if !hasReaction { + cmd.ReplyHiddenf(lang.GetDefault(tp+"msg.setup.no_reactions"), joinEmojiID) return } - emojiID := reaction.Emoji.ID - if emojiID == "" { - emojiID = reaction.Emoji.Name - } - users, err := cmd.Session.MessageReactions(msg.ChannelID, msg.ID, emojiID, 100, "", "") + users, err := cmd.Session.MessageReactions(msg.ChannelID, msg.ID, joinEmojiID, 100, "", "") if err != nil { log.Printf("Error on get users: %v\n", err) cmd.ReplyError() diff --git a/util/discord.go b/util/discord.go index 4724f92..060758d 100644 --- a/util/discord.go +++ b/util/discord.go @@ -151,3 +151,46 @@ func GetChannelsFromDatabase(s *discordgo.Session, channelName string) (map[stri return IDMap, nil } + +// GetConfigComponentEmoji returns a configured [discordgo.ComponentEmoji] for the given name. +func GetConfigComponentEmoji(name string) discordgo.ComponentEmoji { + e := GetConfigEmoji(name) + return discordgo.ComponentEmoji{ + Name: e.Name, + ID: e.ID, + Animated: e.Animated, + } +} + +// GetConfigEmoji returns a configured [discordgo.Emoji] for the given name. +func GetConfigEmoji(name string) discordgo.Emoji { + override := viper.GetString("event.emoji." + name) + if override != "" && override != name { + return GetConfigEmoji(override) + } + return discordgo.Emoji{ + Name: viper.GetString("event.emoji." + name + ".name"), + ID: viper.GetString("event.emoji." + name + ".id"), + Animated: viper.GetBool("event.emoji." + name + ".animated"), + } +} + +// CompareEmoji returns true if the two emoji are the same +func CompareEmoji[E1, E2 *discordgo.Emoji | *discordgo.ComponentEmoji](e1 E1, e2 E2) bool { + return *componentEmoji(e1) == *componentEmoji(e2) +} + +// componentEmoji returns a [discordgo.ComponentEmoji] for the given [discordgo.Emoji] or [discordgo.ComponentEmoji]. +func componentEmoji[E *discordgo.Emoji | *discordgo.ComponentEmoji](e E) *discordgo.ComponentEmoji { + if ee, ok := any(e).(*discordgo.Emoji); ok { + return &discordgo.ComponentEmoji{ + Name: ee.Name, + ID: ee.ID, + Animated: ee.Animated, + } + } + if ce, ok := any(e).(*discordgo.ComponentEmoji); ok { + return ce + } + panic("Given generic type is not an emoji or component emoji") +}