diff --git a/plugins/beers/beers.go b/plugins/beers/beers.go index f1ae2d3..c600ff2 100644 --- a/plugins/beers/beers.go +++ b/plugins/beers/beers.go @@ -103,18 +103,18 @@ func (p *BeersPlugin) register() { switch op { case "=": if count == 0 { - p.puke(r.Conn, nick, id, r.Msg.Channel) + p.puke(r) } else { - p.setBeers(nick, id, count) + p.setBeers(&r, nick, id, count) p.randomReply(r.Conn, r.Msg.Channel) } return true case "+=": - p.addBeers(nick, id, count) + p.addBeers(&r, nick, id, count) p.randomReply(r.Conn, r.Msg.Channel) return true case "-=": - p.addBeers(nick, id, -count) + p.addBeers(&r, nick, id, -count) p.randomReply(r.Conn, r.Msg.Channel) return true } @@ -127,9 +127,9 @@ func (p *BeersPlugin) register() { nick := r.Msg.User.Name id := r.Msg.User.ID if op == "++" { - p.addBeers(nick, id, 1) + p.addBeers(&r, nick, id, 1) } else { - p.addBeers(nick, id, -1) + p.addBeers(&r, nick, id, -1) } p.randomReply(r.Conn, r.Msg.Channel) return true @@ -152,14 +152,14 @@ func (p *BeersPlugin) register() { {Kind: bot.Message, IsCmd: true, Regex: regexp.MustCompile(`(?i)^puke$`), Handler: func(r bot.Request) bool { - p.puke(r.Conn, r.Msg.User.Name, r.Msg.User.ID, r.Msg.Channel) + p.puke(r) return true }}, {Kind: bot.Message, IsCmd: true, Regex: regexp.MustCompile(`(?i)^` + strings.Join(p.c.GetArray("beers.imbibewords", []string{"imbibe", "quaff"}), "|") + `$`), Handler: func(r bot.Request) bool { - p.addBeers(r.Msg.User.Name, r.Msg.User.ID, 1) + p.addBeers(&r, r.Msg.User.Name, r.Msg.User.ID, 1) p.randomReply(r.Conn, r.Msg.Channel) return true }}, @@ -245,17 +245,17 @@ func getUserBeers(db *sqlx.DB, user, id string) counter.Item { return booze } -func (p *BeersPlugin) setBeers(user, id string, amount int) { +func (p *BeersPlugin) setBeers(r *bot.Request, user, id string, amount int) { ub := getUserBeers(p.db, user, id) - err := ub.Update(amount) + err := ub.Update(r, amount) if err != nil { log.Error().Err(err).Msgf("Error saving beers") } } -func (p *BeersPlugin) addBeers(user, id string, delta int) { +func (p *BeersPlugin) addBeers(r *bot.Request, user, id string, delta int) { ub := getUserBeers(p.db, user, id) - err := ub.UpdateDelta(delta) + err := ub.UpdateDelta(r, delta) if err != nil { log.Error().Err(err).Msgf("Error saving beers") } @@ -279,10 +279,10 @@ func (p *BeersPlugin) reportCount(c bot.Connector, nick, id, channel string, him p.b.Send(c, bot.Message, channel, msg) } -func (p *BeersPlugin) puke(c bot.Connector, user, id string, channel string) { - p.setBeers(user, id, 0) - msg := fmt.Sprintf("Ohhhhhh, and a reversal of fortune for %s!", user) - p.b.Send(c, bot.Message, channel, msg) +func (p *BeersPlugin) puke(r bot.Request) { + p.setBeers(&r, r.Msg.User.Name, r.Msg.User.ID, 0) + msg := fmt.Sprintf("Ohhhhhh, and a reversal of fortune for %s!", r.Msg.User.Name) + p.b.Send(r.Conn, bot.Message, r.Msg.Channel, msg) } func (p *BeersPlugin) doIKnow(nick, id string) bool { @@ -474,7 +474,7 @@ func (p *BeersPlugin) sendCheckin(c bot.Connector, channel string, user untappdU } // Don't add beers till after a photo has been detected (or failed once) - p.addBeers(user.chanNick, "", 1) + p.addBeers(nil, user.chanNick, "", 1) drunken := p.getBeers(user.chanNick, "") msg := fmt.Sprintf("%s just drank %s by %s%s, bringing his drunkeness to %d", diff --git a/plugins/beers/beers_test.go b/plugins/beers/beers_test.go index cd5f677..48aa277 100644 --- a/plugins/beers/beers_test.go +++ b/plugins/beers/beers_test.go @@ -65,7 +65,7 @@ func TestCounter(t *testing.T) { t.Log(err) t.Fatal() } - err = i.Update(5) + err = i.Update(nil, 5) assert.Nil(t, err) } diff --git a/plugins/counter/counter.go b/plugins/counter/counter.go index c2e2fcc..7f47fc4 100644 --- a/plugins/counter/counter.go +++ b/plugins/counter/counter.go @@ -220,7 +220,7 @@ func (i *Item) Create() error { // UpdateDelta sets a value // This will create or delete the item if necessary -func (i *Item) Update(value int) error { +func (i *Item) Update(r *bot.Request, value int) error { i.Count = value if i.Count == 0 && i.ID != -1 { return i.Delete() @@ -234,16 +234,16 @@ func (i *Item) Update(value int) error { Msg("Updating item") _, err := i.Exec(`update counter set count = ? where id = ?`, i.Count, i.ID) if err == nil { - sendUpdate(i.Nick, i.Item, i.Count) + sendUpdate(r, i.Nick, i.Item, i.Count) } return err } // UpdateDelta changes a value according to some delta // This will create or delete the item if necessary -func (i *Item) UpdateDelta(delta int) error { +func (i *Item) UpdateDelta(r *bot.Request, delta int) error { i.Count += delta - return i.Update(i.Count) + return i.Update(r, i.Count) } // Delete removes a counter from the database @@ -587,7 +587,7 @@ func (p *CounterPlugin) incrementCmd(r bot.Request) bool { return false } log.Debug().Msgf("About to update item: %#v", item) - item.UpdateDelta(1) + item.UpdateDelta(&r, 1) p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick, item.Count, item.Item)) return true @@ -612,7 +612,7 @@ func (p *CounterPlugin) decrementCmd(r bot.Request) bool { // Item ain't there, I guess return false } - item.UpdateDelta(-1) + item.UpdateDelta(&r, -1) p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick, item.Count, item.Item)) return true @@ -636,7 +636,7 @@ func (p *CounterPlugin) addToCmd(r bot.Request) bool { } n, _ := strconv.Atoi(r.Values["amount"]) log.Debug().Msgf("About to update item by %d: %#v", n, item) - item.UpdateDelta(n) + item.UpdateDelta(&r, n) p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick, item.Count, item.Item)) return true @@ -660,7 +660,7 @@ func (p *CounterPlugin) removeFromCmd(r bot.Request) bool { } n, _ := strconv.Atoi(r.Values["amount"]) log.Debug().Msgf("About to update item by -%d: %#v", n, item) - item.UpdateDelta(-n) + item.UpdateDelta(&r, -n) p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick, item.Count, item.Item)) return true @@ -702,7 +702,7 @@ func (p *CounterPlugin) teaMatchCmd(r bot.Request) bool { if item.Count < 0 { delta = -1 } - item.UpdateDelta(delta) + item.UpdateDelta(&r, delta) p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s... %s has %d %s", strings.Join(everyDayImShuffling([]string{"bleep", "bloop", "blop"}), "-"), nick, item.Count, itemName)) return true @@ -764,9 +764,9 @@ func (p *CounterPlugin) handleCounterAPI(w http.ResponseWriter, r *http.Request) return } if info.Action == "++" { - item.UpdateDelta(1) + item.UpdateDelta(nil, 1) } else if info.Action == "--" { - item.UpdateDelta(-1) + item.UpdateDelta(nil, -1) } else { w.WriteHeader(400) fmt.Fprint(w, "Invalid increment") @@ -795,7 +795,7 @@ type Update struct { Amount int } -type updateFunc func(Update) +type updateFunc func(bot.Request, Update) var updateFuncs = []updateFunc{} @@ -803,10 +803,14 @@ func RegisterUpdate(f updateFunc) { log.Debug().Msgf("registering update func") updateFuncs = append(updateFuncs, f) } -func sendUpdate(who, what string, amount int) { + +func sendUpdate(r *bot.Request, who, what string, amount int) { + if r == nil { + return + } log.Debug().Msgf("sending updates to %d places", len(updateFuncs)) for _, f := range updateFuncs { - f(Update{who, what, amount}) + f(*r, Update{who, what, amount}) } } diff --git a/plugins/goals/goals.go b/plugins/goals/goals.go index cee58ab..86611ce 100644 --- a/plugins/goals/goals.go +++ b/plugins/goals/goals.go @@ -306,7 +306,7 @@ func (g goal) Delete() error { return err } -func (p *GoalsPlugin) update(u counter.Update) { +func (p *GoalsPlugin) update(r bot.Request, u counter.Update) { log.Debug().Msgf("entered update for %#v", u) gs, err := p.getGoal(u.Who, u.What) if err != nil {