Skip to content

Commit

Permalink
feat: more config and util functions for emojis
Browse files Browse the repository at this point in the history
  • Loading branch information
Kesuaheli committed Nov 16, 2024
1 parent d5e7e68 commit 567b746
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 25 deletions.
32 changes: 24 additions & 8 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions modules/adventcalendar/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
}},
}
Expand Down
24 changes: 12 additions & 12 deletions modules/secretsanta/handlerMessageSetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
43 changes: 43 additions & 0 deletions util/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit 567b746

Please sign in to comment.