From e7acd4730ceda7aca5b57b51dee332538498e0a2 Mon Sep 17 00:00:00 2001 From: Chris Sexton <3216719+chrissexton@users.noreply.github.com> Date: Fri, 19 Apr 2024 13:33:41 -0400 Subject: [PATCH] emojifyme: fix it --- connectors/discord/discord.go | 19 +++++++++++++++---- plugins/emojifyme/emojifyme.go | 23 ++++++++++++++++++----- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/connectors/discord/discord.go b/connectors/discord/discord.go index e37494e..fac9878 100644 --- a/connectors/discord/discord.go +++ b/connectors/discord/discord.go @@ -93,7 +93,9 @@ func (d Discord) Send(kind bot.Kind, args ...any) (string, error) { return d.sendMessage(args[0].(string), msg, false, args...) case bot.Reaction: msg := args[2].(msg.Message) - err := d.client.MessageReactionAdd(args[0].(string), msg.ID, args[1].(string)) + snowflake := d.GetEmojySnowflake(args[1].(string)) + log.Debug().Str("snowflake", snowflake).Msg("bot.Send") + err := d.client.MessageReactionAdd(args[0].(string), msg.ID, snowflake) return args[1].(string), err case bot.Delete: ch := args[0].(string) @@ -213,16 +215,25 @@ func (d *Discord) GetEmojiList(force bool) map[string]string { return emojis } -func (d *Discord) GetEmojySnowflake(name string) string { +func (d *Discord) GetEmojyID(name string) string { list := d.GetEmojiList(true) for k, v := range list { - if name == v { + log.Debug().Str("k", k).Str("v", v).Str("name", name).Msg("emoj") + if strings.Contains(name, v) { return k } } return name } +func (d *Discord) GetEmojySnowflake(name string) string { + id := d.GetEmojyID(name) + if id != name { + return name + ":" + id + } + return name +} + // Who gets the users in the guild // Note that the channel ID does not matter in this particular case func (d *Discord) Who(id string) []string { @@ -374,7 +385,7 @@ func (d *Discord) UploadEmojy(emojy, path string) error { } func (d *Discord) DeleteEmojy(emojy string) error { - emojyID := d.GetEmojySnowflake(emojy) + emojyID := d.GetEmojyID(emojy) return d.client.GuildEmojiDelete(d.guildID, emojyID) } diff --git a/plugins/emojifyme/emojifyme.go b/plugins/emojifyme/emojifyme.go index d7ea9a4..729b520 100644 --- a/plugins/emojifyme/emojifyme.go +++ b/plugins/emojifyme/emojifyme.go @@ -4,7 +4,7 @@ package emojifyme import ( "encoding/json" - "io/ioutil" + "io" "math/rand" "net/http" "regexp" @@ -26,13 +26,14 @@ func New(b bot.Bot) *EmojifyMePlugin { if err != nil { log.Fatal().Err(err).Msg("Error generic emoji list") } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) resp.Body.Close() if err != nil { log.Fatal().Err(err).Msg("Error generic emoji list body") } type Emoji struct { + Emoji string `json:"emoji"` Aliases []string `json:"aliases"` } @@ -45,16 +46,22 @@ func New(b bot.Bot) *EmojifyMePlugin { emojiMap := map[string]string{} for _, e := range emoji { for _, alias := range e.Aliases { - emojiMap[alias] = alias + emojiMap[alias] = e.Emoji } } + emojy := b.GetEmojiList(true) + log.Debug().Interface("emojyList", emojy).Msg("startup") + for _, v := range emojy { + emojiMap[v] = v + } + ep := &EmojifyMePlugin{ Bot: b, GotBotEmoji: false, Emoji: emojiMap, } - b.RegisterRegex(ep, bot.Message, regexp.MustCompile(`.*`), ep.message) + b.RegisterRegex(ep, bot.Any, regexp.MustCompile(`.*`), ep.message) return ep } @@ -95,7 +102,13 @@ func (p *EmojifyMePlugin) message(r bot.Request) bool { if emojied > 0 && rand.Float64() <= p.Bot.Config().GetFloat64("Emojify.Chance", 0.02)*emojied { for _, e := range emojys { - p.Bot.Send(c, bot.Reaction, message.Channel, e, message) + log.Debug(). + Str("emojy", e). + Msgf("trying to react") + _, err := p.Bot.Send(c, bot.Reaction, message.Channel, e, message) + if err != nil { + log.Error().Msgf("emojy react error: %s", err) + } } return false }