Skip to content

Commit

Permalink
Hotfix for missing emojis
Browse files Browse the repository at this point in the history
  • Loading branch information
denverquane committed Nov 2, 2023
1 parent 488074e commit f0971d9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
15 changes: 8 additions & 7 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,20 +185,21 @@ func (bot *Bot) newGuild(emojiGuildID string) func(s *discordgo.Session, m *disc
log.Println("[This is not an error] No explicit guildID provided for emojis; using the current guild default")
emojiGuildID = m.Guild.ID
}

// only check/add emojis to the server denoted for emojis, OR, this server that we picked as a fallback above ^
if emojiGuildID == m.Guild.ID {
EmojiLock.Lock()
uploadMissingEmojis := emojiGuildID == m.Guild.ID

EmojiLock.Lock()
// only add the emojis if they haven't been added already. Saves api calls for bots in guilds
if bot.StatusEmojis.isEmpty() {
allEmojis, err := s.GuildEmojis(emojiGuildID)
if err != nil {
log.Println(err)
} else {
log.Println("Adding any missing emojis now")
bot.addAllMissingEmojis(s, emojiGuildID, true, allEmojis)
bot.addAllMissingEmojis(s, emojiGuildID, false, allEmojis)
bot.verifyEmojis(s, emojiGuildID, true, allEmojis, uploadMissingEmojis)
bot.verifyEmojis(s, emojiGuildID, false, allEmojis, uploadMissingEmojis)
}
EmojiLock.Unlock()
}
EmojiLock.Unlock()

games := bot.RedisInterface.LoadAllActiveGames(m.Guild.ID)

Expand Down
26 changes: 24 additions & 2 deletions bot/emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,36 @@ func (e *Emoji) DownloadAndBase64Encode() string {
return "data:image/png;base64," + encodedStr
}

func (a AlivenessEmojis) isEmpty() bool {
if v, ok := a[true]; ok {
for _, vv := range v {
if vv.Name == "" || vv.ID == "" {
return true
}
}
} else {
return true
}
if v, ok := a[false]; ok {
for _, vv := range v {
if vv.Name == "" || vv.ID == "" {
return true
}
}
} else {
return true
}
return false
}

func emptyStatusEmojis() AlivenessEmojis {
topMap := make(AlivenessEmojis)
topMap[true] = make([]Emoji, 18) // 18 colors for alive/dead
topMap[false] = make([]Emoji, 18)
return topMap
}

func (bot *Bot) addAllMissingEmojis(s *discordgo.Session, guildID string, alive bool, serverEmojis []*discordgo.Emoji) {
func (bot *Bot) verifyEmojis(s *discordgo.Session, guildID string, alive bool, serverEmojis []*discordgo.Emoji, add bool) {
for i, emoji := range GlobalAlivenessEmojis[alive] {
alreadyExists := false
for _, v := range serverEmojis {
Expand All @@ -68,7 +90,7 @@ func (bot *Bot) addAllMissingEmojis(s *discordgo.Session, guildID string, alive
break
}
}
if !alreadyExists {
if add && !alreadyExists {
b64 := emoji.DownloadAndBase64Encode()
p := discordgo.EmojiParams{
Name: emoji.Name,
Expand Down
17 changes: 17 additions & 0 deletions bot/emoji_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package bot

import "testing"

func TestIsEmpty(t *testing.T) {
e := emptyStatusEmojis()

if !e.isEmpty() {
t.Fatalf("fresh initialized emojis should be empty")
}

e = GlobalAlivenessEmojis

if e.isEmpty() {
t.Fatalf("valid emojis shouldn't report as empty")
}
}

0 comments on commit f0971d9

Please sign in to comment.