From 6d004d8d1f7f61e7464c443990bb9de1b0d293fb Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Sun, 28 Nov 2021 14:12:38 -0500 Subject: [PATCH] counter: fix api and add a message when one exists --- plugins/counter/api.go | 28 ++++++++++++++++++++-------- plugins/counter/counter.go | 2 ++ plugins/goals/goals.go | 4 +++- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/plugins/counter/api.go b/plugins/counter/api.go index 9ccb15a..d3c9445 100644 --- a/plugins/counter/api.go +++ b/plugins/counter/api.go @@ -4,6 +4,7 @@ import ( "embed" "encoding/json" "fmt" + "io/ioutil" "net/http" "time" @@ -69,25 +70,36 @@ func (p *CounterPlugin) mkIncrementAPI(delta int) func(w http.ResponseWriter, r fmt.Fprint(w, string(j)) return } + + body, _ := ioutil.ReadAll(r.Body) + postData := map[string]string{} + err = json.Unmarshal(body, &postData) + personalMsg := "" + if inputMsg, ok := postData["message"]; ok { + personalMsg = fmt.Sprintf("\nMessage: %s", inputMsg) + } + + chs := p.cfg.GetArray("channels", []string{p.cfg.Get("channels", "none")}) req := &bot.Request{ Conn: p.b.DefaultConnector(), Kind: bot.Message, Msg: msg.Message{ - User: &u, - ChannelName: "#API", - Body: fmt.Sprintf("%s += %d", itemName, delta), - Time: time.Now(), + User: &u, + // Noting here that we're only going to do goals in a "default" + // channel even if it should send updates to others. + Channel: chs[0], + Body: fmt.Sprintf("%s += %d", itemName, delta), + Time: time.Now(), }, Values: nil, Args: nil, } item.UpdateDelta(req, delta) - msg := fmt.Sprintf("%s changed their %s counter by %d for a total of %d via the amazing %s API", - userName, itemName, delta, item.Count, p.cfg.Get("nick", "catbase")) - for _, ch := range p.cfg.GetArray("channels", []string{}) { + msg := fmt.Sprintf("%s changed their %s counter by %d for a total of %d via the amazing %s API. %s", + userName, itemName, delta, item.Count, p.cfg.Get("nick", "catbase"), personalMsg) + for _, ch := range chs { p.b.Send(p.b.DefaultConnector(), bot.Message, ch, msg) req.Msg.Channel = ch - sendUpdate(req, userName, itemName, item.Count) } j, _ := json.Marshal(struct{ Status bool }{true}) fmt.Fprint(w, string(j)) diff --git a/plugins/counter/counter.go b/plugins/counter/counter.go index ae53b19..cc16a80 100644 --- a/plugins/counter/counter.go +++ b/plugins/counter/counter.go @@ -733,6 +733,8 @@ func RegisterUpdate(f updateFunc) { } func sendUpdate(r *bot.Request, who, what string, amount int) { + log.Debug(). + Msgf("Updating %s for %s with %d", who, what, amount) if r == nil { return } diff --git a/plugins/goals/goals.go b/plugins/goals/goals.go index 666be6d..c05be62 100644 --- a/plugins/goals/goals.go +++ b/plugins/goals/goals.go @@ -213,6 +213,8 @@ func (p *GoalsPlugin) checkGoal(c bot.Connector, ch, what, who string) { perc := float64(item.Count) / float64(g.Amount) * 100.0 remaining := p.remainingText(item, g) + log.Debug().Msgf("ch: %v, perc: %.2f, remaining: %s", ch, perc, remaining) + if perc >= 100 { p.deregister(c, ch, g.Kind, g.What, g.Who) m := fmt.Sprintf("You made it! You have %.2f%% of %s and now it's done.", perc, what) @@ -316,7 +318,7 @@ func (g goal) Delete() error { } func (p *GoalsPlugin) update(r bot.Request, u counter.Update) { - log.Debug().Msgf("entered update for %#v", u) + log.Debug().Msgf("entered update for %#v in ch: %v", u, r.Msg.Channel) gs, err := p.getGoal(u.Who, u.What) if err != nil { log.Error().Err(err).Msgf("could not get goal for %#v", u)