From cf30dad0555ec6df10bf16338ac47358da335527 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Mon, 23 Aug 2021 11:28:08 -0400 Subject: [PATCH] goals: fix goal reporting --- connectors/discord/discord.go | 34 +++++++++++++++++++++++++++------- plugins/beers/beers.go | 5 ++++- plugins/goals/goals.go | 13 +++++++++++-- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/connectors/discord/discord.go b/connectors/discord/discord.go index 896e6b7..6e41ef6 100644 --- a/connectors/discord/discord.go +++ b/connectors/discord/discord.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "net/http" + "strconv" "strings" "github.com/velour/catbase/bot/msg" @@ -22,6 +23,9 @@ type Discord struct { event bot.Callback emojiCache map[string]string + + // store IDs -> nick and vice versa for quick conversion + uidCache map[string]string } func New(config *config.Config) *Discord { @@ -30,8 +34,9 @@ func New(config *config.Config) *Discord { log.Fatal().Err(err).Msg("Could not connect to Discord") } d := &Discord{ - config: config, - client: client, + config: config, + client: client, + uidCache: map[string]string{}, } return d } @@ -117,7 +122,11 @@ func (d *Discord) sendMessage(channel, message string, meMessage bool, args ...i //st, err := d.client.ChannelMessageSend(channel, message) if err != nil { - log.Error().Err(err).Msg("Error sending message") + log.Error(). + Interface("data", data). + Str("channel", channel). + Err(err). + Msg("Error sending message") return "", err } @@ -159,12 +168,22 @@ func (d *Discord) Who(id string) []string { } func (d *Discord) Profile(id string) (user.User, error) { + if _, err := strconv.Atoi(id); err != nil { + if newID, ok := d.uidCache[id]; ok { + id = newID + } else { + return user.User{}, fmt.Errorf("invalid ID snowflake: %s", id) + } + } u, err := d.client.User(id) if err != nil { log.Error().Err(err).Msg("Error getting user") return user.User{}, err } - return *d.convertUser(u), nil + user := d.convertUser(u) + d.uidCache[user.Name] = user.ID + d.uidCache[user.ID] = user.Name + return *user, nil } func (d *Discord) convertUser(u *discordgo.User) *user.User { @@ -215,6 +234,9 @@ func (d *Discord) Serve() error { } func (d *Discord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { + // if we haven't seen this user, we need to resolve their nick before continuing + author, _ := d.Profile(m.Author.ID) + if m.Author.ID == s.State.User.ID { return } @@ -228,11 +250,9 @@ func (d *Discord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate tStamp, _ := m.Timestamp.Parse() - author := d.convertUser(m.Author) - msg := msg.Message{ ID: m.ID, - User: author, + User: &author, Channel: m.ChannelID, ChannelName: ch.Name, Body: text, diff --git a/plugins/beers/beers.go b/plugins/beers/beers.go index 4cf4897..b1f47f5 100644 --- a/plugins/beers/beers.go +++ b/plugins/beers/beers.go @@ -383,7 +383,10 @@ func (p *BeersPlugin) pullUntappd() ([]checkin, error) { var beers Beers err = json.Unmarshal(body, &beers) if err != nil { - log.Error().Err(err) + log.Error(). + Str("body", string(body)). + Err(err). + Msg("could not unmarshal") return []checkin{}, err } return beers.Response.Checkins.Items, nil diff --git a/plugins/goals/goals.go b/plugins/goals/goals.go index 86611ce..7c60f16 100644 --- a/plugins/goals/goals.go +++ b/plugins/goals/goals.go @@ -54,15 +54,16 @@ func (p *GoalsPlugin) mkDB() { func (p *GoalsPlugin) registerCmds() { p.handlers = bot.HandlerTable{ {Kind: bot.Message, IsCmd: true, - Regex: regexp.MustCompile(`(?i)^register (?Pcompetition|goal) for (?P[[:punct:][:alnum:]]+) (?P[[:punct:][:alnum:]]+) (?P[[:digit:]]+)?`), + Regex: regexp.MustCompile(`(?i)^register (?Pcompetition|goal) for (?P[[:punct:][:alnum:]]+) (?P[^\s]+) (?P[[:digit:]]+)?`), HelpText: "Register with `%s` for other people", Handler: func(r bot.Request) bool { + log.Debug().Interface("values", r.Values).Msg("trying to register a goal") amount, _ := strconv.Atoi(r.Values["amount"]) p.register(r.Conn, r.Msg.Channel, r.Values["type"], r.Values["what"], r.Values["who"], amount) return true }}, {Kind: bot.Message, IsCmd: true, - Regex: regexp.MustCompile(`(?i)^register (?Pcompetition|goal) (?P[[:punct:][:alnum:]]+) (?P[[:digit:]]+)?`), + Regex: regexp.MustCompile(`(?i)^register (?Pcompetition|goal) (?P[^\s]+) (?P[[:digit:]]+)?`), HelpText: "Register with `%s` for yourself", Handler: func(r bot.Request) bool { amount, _ := strconv.Atoi(r.Values["amount"]) @@ -134,6 +135,7 @@ func (p *GoalsPlugin) deregister(c bot.Connector, ch, kind, what, who string) { } func (p *GoalsPlugin) check(c bot.Connector, ch, kind, what, who string) { + log.Debug().Msgf("checking goal in channel %s", ch) if kind == "goal" { p.checkGoal(c, ch, what, who) return @@ -193,8 +195,15 @@ func (p *GoalsPlugin) checkGoal(c bot.Connector, ch, what, who string) { if err == nil && user.ID != "" { id = user.ID nick = user.Name + } else { + log.Error().Err(err).Msg("no user returned for goal check") } + log.Debug(). + Str("nick", nick). + Str("id", id). + Str("what", what). + Msg("looking for item") item, err := counter.GetUserItem(p.db, nick, id, what) if err != nil { p.b.Send(c, bot.Message, ch, fmt.Sprintf("I couldn't find any %s", what))