counter: use IDs when available

This should help alleviate user name changes, but may not fix it
entirely. Had to update beers and goals to match an ID search.

Note: this will bust badly for IRC
This commit is contained in:
Chris Sexton 2021-02-14 16:57:22 -05:00 committed by Chris Sexton
parent 4e0c308253
commit 332d992d05
8 changed files with 227 additions and 129 deletions

View File

@ -35,6 +35,8 @@ const (
SelfMessage SelfMessage
// Delete removes a message by ID // Delete removes a message by ID
Delete Delete
// Startup is triggered after the connector has run the Serve function
Startup
) )
type EphemeralID string type EphemeralID string

View File

@ -676,6 +676,7 @@ func (s *SlackApp) Profile(identifier string) (user.User, error) {
} }
for _, u := range users { for _, u := range users {
log.Debug().Str("Name", u.Name).Str("ID", u.ID).Msgf("Looking for %s", identifier)
if u.Name == identifier || u.ID == identifier { if u.Name == identifier || u.ID == identifier {
return user.User{ return user.User{
ID: u.ID, ID: u.ID,

View File

@ -9,6 +9,7 @@ import (
"os" "os"
"time" "time"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/connectors/discord" "github.com/velour/catbase/connectors/discord"
"github.com/velour/catbase/plugins/gpt2" "github.com/velour/catbase/plugins/gpt2"
@ -158,6 +159,8 @@ func main() {
log.Fatal().Err(err) log.Fatal().Err(err)
} }
b.Receive(client, bot.Startup, msg.Message{})
addr := c.Get("HttpAddr", "127.0.0.1:1337") addr := c.Get("HttpAddr", "127.0.0.1:1337")
log.Debug().Msgf("starting web service at %s", addr) log.Debug().Msgf("starting web service at %s", addr)
log.Fatal().Err(http.ListenAndServe(addr, nil)).Msg("bot killed") log.Fatal().Err(http.ListenAndServe(addr, nil)).Msg("bot killed")

View File

@ -98,22 +98,23 @@ func (p *BeersPlugin) register() {
op := r.Values["operator"] op := r.Values["operator"]
count, _ := strconv.Atoi(r.Values["amount"]) count, _ := strconv.Atoi(r.Values["amount"])
nick := r.Msg.User.Name nick := r.Msg.User.Name
id := r.Msg.User.ID
switch op { switch op {
case "=": case "=":
if count == 0 { if count == 0 {
p.puke(r.Conn, nick, r.Msg.Channel) p.puke(r.Conn, nick, id, r.Msg.Channel)
} else { } else {
p.setBeers(nick, count) p.setBeers(nick, id, count)
p.randomReply(r.Conn, r.Msg.Channel) p.randomReply(r.Conn, r.Msg.Channel)
} }
return true return true
case "+=": case "+=":
p.addBeers(nick, count) p.addBeers(nick, id, count)
p.randomReply(r.Conn, r.Msg.Channel) p.randomReply(r.Conn, r.Msg.Channel)
return true return true
case "-=": case "-=":
p.addBeers(nick, -count) p.addBeers(nick, id, -count)
p.randomReply(r.Conn, r.Msg.Channel) p.randomReply(r.Conn, r.Msg.Channel)
return true return true
} }
@ -124,10 +125,11 @@ func (p *BeersPlugin) register() {
Handler: func(r bot.Request) bool { Handler: func(r bot.Request) bool {
op := r.Values["operator"] op := r.Values["operator"]
nick := r.Msg.User.Name nick := r.Msg.User.Name
id := r.Msg.User.ID
if op == "++" { if op == "++" {
p.addBeers(nick, 1) p.addBeers(nick, id, 1)
} else { } else {
p.addBeers(nick, -1) p.addBeers(nick, id, -1)
} }
p.randomReply(r.Conn, r.Msg.Channel) p.randomReply(r.Conn, r.Msg.Channel)
return true return true
@ -139,8 +141,8 @@ func (p *BeersPlugin) register() {
if who == "" { if who == "" {
who = r.Msg.User.Name who = r.Msg.User.Name
} }
if p.doIKnow(who) { if p.doIKnow(who, "") {
p.reportCount(r.Conn, who, r.Msg.Channel, false) p.reportCount(r.Conn, who, "", r.Msg.Channel, false)
} else { } else {
msg := fmt.Sprintf("Sorry, I don't know %s.", who) msg := fmt.Sprintf("Sorry, I don't know %s.", who)
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, msg) p.b.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
@ -150,14 +152,14 @@ func (p *BeersPlugin) register() {
{Kind: bot.Message, IsCmd: true, {Kind: bot.Message, IsCmd: true,
Regex: regexp.MustCompile(`(?i)^puke$`), Regex: regexp.MustCompile(`(?i)^puke$`),
Handler: func(r bot.Request) bool { Handler: func(r bot.Request) bool {
p.puke(r.Conn, r.Msg.User.Name, r.Msg.Channel) p.puke(r.Conn, r.Msg.User.Name, r.Msg.User.ID, r.Msg.Channel)
return true return true
}}, }},
{Kind: bot.Message, IsCmd: true, {Kind: bot.Message, IsCmd: true,
Regex: regexp.MustCompile(`(?i)^` + Regex: regexp.MustCompile(`(?i)^` +
strings.Join(p.c.GetArray("beers.imbibewords", []string{"imbibe", "quaff"}), "|") + `$`), strings.Join(p.c.GetArray("beers.imbibewords", []string{"imbibe", "quaff"}), "|") + `$`),
Handler: func(r bot.Request) bool { Handler: func(r bot.Request) bool {
p.addBeers(r.Msg.User.Name, 1) p.addBeers(r.Msg.User.Name, r.Msg.User.ID, 1)
p.randomReply(r.Conn, r.Msg.Channel) p.randomReply(r.Conn, r.Msg.Channel)
return true return true
}}, }},
@ -237,34 +239,35 @@ func (p *BeersPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message,
return true return true
} }
func getUserBeers(db *sqlx.DB, user string) counter.Item { func getUserBeers(db *sqlx.DB, user, id string) counter.Item {
booze, _ := counter.GetUserItem(db, user, itemName) // TODO: really ought to have an ID here
booze, _ := counter.GetUserItem(db, user, id, itemName)
return booze return booze
} }
func (p *BeersPlugin) setBeers(user string, amount int) { func (p *BeersPlugin) setBeers(user, id string, amount int) {
ub := getUserBeers(p.db, user) ub := getUserBeers(p.db, user, id)
err := ub.Update(amount) err := ub.Update(amount)
if err != nil { if err != nil {
log.Error().Err(err).Msgf("Error saving beers") log.Error().Err(err).Msgf("Error saving beers")
} }
} }
func (p *BeersPlugin) addBeers(user string, delta int) { func (p *BeersPlugin) addBeers(user, id string, delta int) {
ub := getUserBeers(p.db, user) ub := getUserBeers(p.db, user, id)
err := ub.UpdateDelta(delta) err := ub.UpdateDelta(delta)
if err != nil { if err != nil {
log.Error().Err(err).Msgf("Error saving beers") log.Error().Err(err).Msgf("Error saving beers")
} }
} }
func (p *BeersPlugin) getBeers(nick string) int { func (p *BeersPlugin) getBeers(nick, id string) int {
ub := getUserBeers(p.db, nick) ub := getUserBeers(p.db, nick, id)
return ub.Count return ub.Count
} }
func (p *BeersPlugin) reportCount(c bot.Connector, nick, channel string, himself bool) { func (p *BeersPlugin) reportCount(c bot.Connector, nick, id, channel string, himself bool) {
beers := p.getBeers(nick) beers := p.getBeers(nick, id)
msg := fmt.Sprintf("%s has had %d beers so far.", nick, beers) msg := fmt.Sprintf("%s has had %d beers so far.", nick, beers)
if himself { if himself {
if beers == 0 { if beers == 0 {
@ -276,14 +279,14 @@ func (p *BeersPlugin) reportCount(c bot.Connector, nick, channel string, himself
p.b.Send(c, bot.Message, channel, msg) p.b.Send(c, bot.Message, channel, msg)
} }
func (p *BeersPlugin) puke(c bot.Connector, user string, channel string) { func (p *BeersPlugin) puke(c bot.Connector, user, id string, channel string) {
p.setBeers(user, 0) p.setBeers(user, id, 0)
msg := fmt.Sprintf("Ohhhhhh, and a reversal of fortune for %s!", user) msg := fmt.Sprintf("Ohhhhhh, and a reversal of fortune for %s!", user)
p.b.Send(c, bot.Message, channel, msg) p.b.Send(c, bot.Message, channel, msg)
} }
func (p *BeersPlugin) doIKnow(nick string) bool { func (p *BeersPlugin) doIKnow(nick, id string) bool {
count := p.getBeers(nick) count := p.getBeers(nick, id)
return count > 0 return count > 0
} }
@ -471,8 +474,8 @@ 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) // Don't add beers till after a photo has been detected (or failed once)
p.addBeers(user.chanNick, 1) p.addBeers(user.chanNick, "", 1)
drunken := p.getBeers(user.chanNick) drunken := p.getBeers(user.chanNick, "")
msg := fmt.Sprintf("%s just drank %s by %s%s, bringing his drunkeness to %d", msg := fmt.Sprintf("%s just drank %s by %s%s, bringing his drunkeness to %d",
user.chanNick, beerName, breweryName, venue, drunken) user.chanNick, beerName, breweryName, venue, drunken)

View File

@ -28,7 +28,7 @@ func makeMessage(payload string, r *regexp.Regexp) bot.Request {
Kind: bot.Message, Kind: bot.Message,
Values: values, Values: values,
Msg: msg.Message{ Msg: msg.Message{
User: &user.User{Name: "tester"}, User: &user.User{Name: "tester", ID: "id"},
Channel: "test", Channel: "test",
Body: payload, Body: payload,
Command: isCmd, Command: isCmd,
@ -60,7 +60,7 @@ func makeBeersPlugin(t *testing.T) (*BeersPlugin, *bot.MockBot) {
func TestCounter(t *testing.T) { func TestCounter(t *testing.T) {
_, mb := makeBeersPlugin(t) _, mb := makeBeersPlugin(t)
i, err := counter.GetUserItem(mb.DB(), "tester", "test") i, err := counter.GetUserItem(mb.DB(), "tester", "id", "test")
if !assert.Nil(t, err) { if !assert.Nil(t, err) {
t.Log(err) t.Log(err)
t.Fatal() t.Fatal()
@ -75,7 +75,7 @@ func TestImbibe(t *testing.T) {
assert.Len(t, mb.Messages, 1) assert.Len(t, mb.Messages, 1)
testMessage(b, "imbibe") testMessage(b, "imbibe")
assert.Len(t, mb.Messages, 2) assert.Len(t, mb.Messages, 2)
it, err := counter.GetUserItem(mb.DB(), "tester", itemName) it, err := counter.GetUserItem(mb.DB(), "tester", "id", itemName)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 2, it.Count) assert.Equal(t, 2, it.Count)
} }
@ -83,7 +83,7 @@ func TestEq(t *testing.T) {
b, mb := makeBeersPlugin(t) b, mb := makeBeersPlugin(t)
testMessage(b, "beers = 3") testMessage(b, "beers = 3")
assert.Len(t, mb.Messages, 1) assert.Len(t, mb.Messages, 1)
it, err := counter.GetUserItem(mb.DB(), "tester", itemName) it, err := counter.GetUserItem(mb.DB(), "tester", "id", itemName)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 3, it.Count) assert.Equal(t, 3, it.Count)
} }
@ -94,7 +94,7 @@ func TestEqZero(t *testing.T) {
testMessage(b, "beers = 0") testMessage(b, "beers = 0")
assert.Len(t, mb.Messages, 2) assert.Len(t, mb.Messages, 2)
assert.Contains(t, mb.Messages[1], "reversal of fortune") assert.Contains(t, mb.Messages[1], "reversal of fortune")
it, err := counter.GetUserItem(mb.DB(), "tester", itemName) it, err := counter.GetUserItem(mb.DB(), "tester", "id", itemName)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 0, it.Count) assert.Equal(t, 0, it.Count)
} }
@ -105,7 +105,7 @@ func TestBeersPlusEq(t *testing.T) {
assert.Len(t, mb.Messages, 1) assert.Len(t, mb.Messages, 1)
testMessage(b, "beers += 5") testMessage(b, "beers += 5")
assert.Len(t, mb.Messages, 2) assert.Len(t, mb.Messages, 2)
it, err := counter.GetUserItem(mb.DB(), "tester", itemName) it, err := counter.GetUserItem(mb.DB(), "tester", "id", itemName)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 10, it.Count) assert.Equal(t, 10, it.Count)
} }
@ -113,11 +113,11 @@ func TestBeersPlusEq(t *testing.T) {
func TestPuke(t *testing.T) { func TestPuke(t *testing.T) {
b, mb := makeBeersPlugin(t) b, mb := makeBeersPlugin(t)
testMessage(b, "beers += 5") testMessage(b, "beers += 5")
it, err := counter.GetUserItem(mb.DB(), "tester", itemName) it, err := counter.GetUserItem(mb.DB(), "tester", "id", itemName)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 5, it.Count) assert.Equal(t, 5, it.Count)
testMessage(b, "puke") testMessage(b, "puke")
it, err = counter.GetUserItem(mb.DB(), "tester", itemName) it, err = counter.GetUserItem(mb.DB(), "tester", "id", itemName)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 0, it.Count) assert.Equal(t, 0, it.Count)
} }
@ -125,7 +125,7 @@ func TestPuke(t *testing.T) {
func TestBeersReport(t *testing.T) { func TestBeersReport(t *testing.T) {
b, mb := makeBeersPlugin(t) b, mb := makeBeersPlugin(t)
testMessage(b, "beers += 5") testMessage(b, "beers += 5")
it, err := counter.GetUserItem(mb.DB(), "tester", itemName) it, err := counter.GetUserItem(mb.DB(), "tester", "id", itemName)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 5, it.Count) assert.Equal(t, 5, it.Count)
testMessage(b, "beers") testMessage(b, "beers")

View File

@ -32,6 +32,7 @@ type Item struct {
Nick string Nick string
Item string Item string
Count int Count int
UserID string
} }
type alias struct { type alias struct {
@ -57,9 +58,14 @@ func GetAllItems(db *sqlx.DB) ([]Item, error) {
} }
// GetItems returns all counters for a subject // GetItems returns all counters for a subject
func GetItems(db *sqlx.DB, nick string) ([]Item, error) { func GetItems(db *sqlx.DB, nick, id string) ([]Item, error) {
var items []Item var items []Item
err := db.Select(&items, `select * from counter where nick = ?`, nick) var err error
if id != "" {
err = db.Select(&items, `select * from counter where userid = ?`, id)
} else {
err = db.Select(&items, `select * from counter where nick = ?`, nick)
}
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -162,7 +168,7 @@ func GetItem(db *sqlx.DB, itemName string) ([]Item, error) {
} }
// GetUserItem returns a specific counter for a subject // GetUserItem returns a specific counter for a subject
func GetUserItem(db *sqlx.DB, nick, itemName string) (Item, error) { func GetUserItem(db *sqlx.DB, nick, id, itemName string) (Item, error) {
itemName = trimUnicode(itemName) itemName = trimUnicode(itemName)
var item Item var item Item
item.DB = db item.DB = db
@ -173,19 +179,25 @@ func GetUserItem(db *sqlx.DB, nick, itemName string) (Item, error) {
log.Error().Err(err).Interface("alias", a) log.Error().Err(err).Interface("alias", a)
} }
err := db.Get(&item, `select * from counter where nick = ? and item= ?`, var err error
nick, itemName) if id != "" {
err = db.Get(&item, `select * from counter where userid = ? and item= ?`, id, itemName)
} else {
err = db.Get(&item, `select * from counter where nick = ? and item= ?`, nick, itemName)
}
switch err { switch err {
case sql.ErrNoRows: case sql.ErrNoRows:
item.ID = -1 item.ID = -1
item.Nick = nick item.Nick = nick
item.Item = itemName item.Item = itemName
item.UserID = id
case nil: case nil:
default: default:
return Item{}, err return Item{}, err
} }
log.Debug(). log.Debug().
Str("nick", nick). Str("nick", nick).
Str("id", id).
Str("itemName", itemName). Str("itemName", itemName).
Interface("item", item). Interface("item", item).
Msg("got item") Msg("got item")
@ -195,8 +207,8 @@ func GetUserItem(db *sqlx.DB, nick, itemName string) (Item, error) {
// GetUserItem returns a specific counter for a subject // GetUserItem returns a specific counter for a subject
// Create saves a counter // Create saves a counter
func (i *Item) Create() error { func (i *Item) Create() error {
res, err := i.Exec(`insert into counter (nick, item, count) values (?, ?, ?);`, res, err := i.Exec(`insert into counter (nick, item, count, userid) values (?, ?, ?, ?);`,
i.Nick, i.Item, i.Count) i.Nick, i.Item, i.Count, i.UserID)
if err != nil { if err != nil {
return err return err
} }
@ -241,26 +253,80 @@ func (i *Item) Delete() error {
return err return err
} }
// NewCounterPlugin creates a new CounterPlugin with the Plugin interface func (p *CounterPlugin) migrate(r bot.Request) bool {
func New(b bot.Bot) *CounterPlugin { db := p.DB
tx := b.DB().MustBegin()
b.DB().MustExec(`create table if not exists counter ( nicks := []string{}
err := db.Select(&nicks, `select distinct nick from counter where userid is null`)
if err != nil {
log.Error().Err(err).Msg("could not get nick list")
return false
}
log.Debug().Msgf("Migrating %d nicks to IDs", len(nicks))
tx := db.MustBegin()
for _, nick := range nicks {
user, err := r.Conn.Profile(nick)
if err != nil {
continue
}
if _, err = tx.Exec(`update counter set userid=? where nick=?`, user.ID, nick); err != nil {
log.Error().Err(err).Msg("Could not migrate users")
continue
}
}
if err := tx.Commit(); err != nil {
log.Error().Err(err).Msg("Could not migrate users")
}
return false
}
func setupDB(b bot.Bot) error {
db := b.DB()
tx := db.MustBegin()
db.MustExec(`create table if not exists counter (
id integer primary key, id integer primary key,
nick string, nick string,
item string, item string,
count integer count integer
);`) );`)
b.DB().MustExec(`create table if not exists counter_alias ( db.MustExec(`create table if not exists counter_alias (
id integer PRIMARY KEY AUTOINCREMENT, id integer PRIMARY KEY AUTOINCREMENT,
item string NOT NULL UNIQUE, item string NOT NULL UNIQUE,
points_to string NOT NULL points_to string NOT NULL
);`) );`)
tx.Commit() tx.Commit()
tx = db.MustBegin()
count := 0
err := tx.Get(&count, `SELECT count(*) FROM pragma_table_info('counter') where name='userid'`)
if err != nil {
return err
}
if count == 0 {
tx.MustExec(`alter table counter add column userid string`)
}
tx.Commit()
return nil
}
// NewCounterPlugin creates a new CounterPlugin with the Plugin interface
func New(b bot.Bot) *CounterPlugin {
if err := setupDB(b); err != nil {
panic(err)
}
cp := &CounterPlugin{ cp := &CounterPlugin{
Bot: b, Bot: b,
DB: b.DB(), DB: b.DB(),
} }
b.RegisterRegex(cp, bot.Startup, regexp.MustCompile(`.*`), cp.migrate)
b.RegisterRegexCmd(cp, bot.Message, mkAliasRegex, cp.mkAliasCmd) b.RegisterRegexCmd(cp, bot.Message, mkAliasRegex, cp.mkAliasCmd)
b.RegisterRegexCmd(cp, bot.Message, rmAliasRegex, cp.rmAliasCmd) b.RegisterRegexCmd(cp, bot.Message, rmAliasRegex, cp.rmAliasCmd)
b.RegisterRegexCmd(cp, bot.Message, leaderboardRegex, cp.leaderboardCmd) b.RegisterRegexCmd(cp, bot.Message, leaderboardRegex, cp.leaderboardCmd)
@ -363,10 +429,10 @@ func (p *CounterPlugin) leaderboardCmd(r bot.Request) bool {
} }
func (p *CounterPlugin) resetCmd(r bot.Request) bool { func (p *CounterPlugin) resetCmd(r bot.Request) bool {
nick := r.Msg.User.Name nick, id := p.resolveUser(r, "")
channel := r.Msg.Channel channel := r.Msg.Channel
items, err := GetItems(p.DB, strings.ToLower(nick)) items, err := GetItems(p.DB, nick, id)
if err != nil { if err != nil {
log.Error(). log.Error().
Err(err). Err(err).
@ -384,32 +450,34 @@ func (p *CounterPlugin) resetCmd(r bot.Request) bool {
} }
func (p *CounterPlugin) inspectCmd(r bot.Request) bool { func (p *CounterPlugin) inspectCmd(r bot.Request) bool {
var subject string who := r.Values["who"]
subject = r.Values["who"] nick, id := "", ""
if who == "me" {
who = r.Msg.User.Name
nick, id = p.resolveUser(r, "")
} else {
nick, id = p.resolveUser(r, who)
}
channel := r.Msg.Channel channel := r.Msg.Channel
c := r.Conn c := r.Conn
if subject == "me" {
subject = strings.ToLower(r.Msg.User.Name)
} else {
subject = strings.ToLower(subject)
}
log.Debug(). log.Debug().
Str("subject", subject). Str("nick", nick).
Str("id", id).
Msg("Getting counter") Msg("Getting counter")
// pull all of the items associated with "subject" // pull all of the items associated with "subject"
items, err := GetItems(p.DB, subject) items, err := GetItems(p.DB, nick, id)
if err != nil { if err != nil {
log.Error(). log.Error().
Err(err). Err(err).
Str("subject", subject). Str("nick", nick).
Str("id", id).
Msg("Error retrieving items") Msg("Error retrieving items")
p.Bot.Send(c, bot.Message, channel, "Something went wrong finding that counter;") p.Bot.Send(c, bot.Message, channel, "Something went wrong finding that counter;")
return true return true
} }
resp := fmt.Sprintf("%s has the following counters:", subject) resp := fmt.Sprintf("%s has the following counters:", nick)
count := 0 count := 0
for _, it := range items { for _, it := range items {
count += 1 count += 1
@ -425,7 +493,7 @@ func (p *CounterPlugin) inspectCmd(r bot.Request) bool {
resp += "." resp += "."
if count == 0 { if count == 0 {
p.Bot.Send(c, bot.Message, channel, fmt.Sprintf("%s has no counters.", subject)) p.Bot.Send(c, bot.Message, channel, fmt.Sprintf("%s has no counters.", nick))
return true return true
} }
@ -434,16 +502,17 @@ func (p *CounterPlugin) inspectCmd(r bot.Request) bool {
} }
func (p *CounterPlugin) clearCmd(r bot.Request) bool { func (p *CounterPlugin) clearCmd(r bot.Request) bool {
subject := strings.ToLower(r.Msg.User.Name) nick, id := p.resolveUser(r, "")
itemName := strings.ToLower(r.Values["what"]) itemName := strings.ToLower(r.Values["what"])
channel := r.Msg.Channel channel := r.Msg.Channel
c := r.Conn c := r.Conn
it, err := GetUserItem(p.DB, subject, itemName) it, err := GetUserItem(p.DB, nick, id, itemName)
if err != nil { if err != nil {
log.Error(). log.Error().
Err(err). Err(err).
Str("subject", subject). Str("nick", nick).
Str("id", id).
Str("itemName", itemName). Str("itemName", itemName).
Msg("Error getting item to remove") Msg("Error getting item to remove")
p.Bot.Send(c, bot.Message, channel, "Something went wrong removing that counter;") p.Bot.Send(c, bot.Message, channel, "Something went wrong removing that counter;")
@ -453,7 +522,8 @@ func (p *CounterPlugin) clearCmd(r bot.Request) bool {
if err != nil { if err != nil {
log.Error(). log.Error().
Err(err). Err(err).
Str("subject", subject). Str("nick", nick).
Str("id", id).
Str("itemName", itemName). Str("itemName", itemName).
Msg("Error removing item") Msg("Error removing item")
p.Bot.Send(c, bot.Message, channel, "Something went wrong removing that counter;") p.Bot.Send(c, bot.Message, channel, "Something went wrong removing that counter;")
@ -466,54 +536,51 @@ func (p *CounterPlugin) clearCmd(r bot.Request) bool {
} }
func (p *CounterPlugin) countCmd(r bot.Request) bool { func (p *CounterPlugin) countCmd(r bot.Request) bool {
var subject string itemName := strings.ToLower(r.Values["what"])
var itemName string nick, id := r.Msg.User.Name, r.Msg.User.ID
if r.Values["what"] == "" {
if r.Values["what"] == "" && r.Values["who"] == "" { itemName = r.Values["who"]
return false
} else if r.Values["what"] != "" {
subject = strings.ToLower(r.Values["who"])
itemName = strings.ToLower(r.Values["what"])
} else { } else {
subject = strings.ToLower(r.Msg.User.Name) nick, id = p.resolveUser(r, r.Values["who"])
itemName = strings.ToLower(r.Values["who"])
} }
var item Item var item Item
item, err := GetUserItem(p.DB, subject, itemName) item, err := GetUserItem(p.DB, nick, id, itemName)
switch { switch {
case err == sql.ErrNoRows: case err == sql.ErrNoRows:
p.Bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("I don't think %s has any %s.", p.Bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("I don't think %s has any %s.",
subject, itemName)) nick, itemName))
return true return true
case err != nil: case err != nil:
log.Error(). log.Error().
Err(err). Err(err).
Str("subject", subject). Str("nick", nick).
Str("id", id).
Str("itemName", itemName). Str("itemName", itemName).
Msg("Error retrieving item count") Msg("Error retrieving item count")
return true return true
} }
p.Bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("%s has %d %s.", subject, item.Count, p.Bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("%s has %d %s.", nick, item.Count,
itemName)) itemName))
return true return true
} }
func (p *CounterPlugin) incrementCmd(r bot.Request) bool { func (p *CounterPlugin) incrementCmd(r bot.Request) bool {
subject := r.Msg.User.Name nick, id := r.Msg.User.Name, r.Msg.User.ID
if r.Values["who"] != "" { if r.Values["who"] != "" {
subject = strings.TrimSuffix(r.Values["who"], ".") nick, id = p.resolveUser(r, r.Values["who"])
} }
itemName := r.Values["thing"] itemName := r.Values["thing"]
channel := r.Msg.Channel channel := r.Msg.Channel
// ++ those fuckers // ++ those fuckers
item, err := GetUserItem(p.DB, subject, itemName) item, err := GetUserItem(p.DB, nick, id, itemName)
if err != nil { if err != nil {
log.Error(). log.Error().
Err(err). Err(err).
Str("subject", subject). Str("nick", nick).
Str("id", id).
Str("itemName", itemName). Str("itemName", itemName).
Msg("error finding item") Msg("error finding item")
// Item ain't there, I guess // Item ain't there, I guess
@ -521,48 +588,47 @@ func (p *CounterPlugin) incrementCmd(r bot.Request) bool {
} }
log.Debug().Msgf("About to update item: %#v", item) log.Debug().Msgf("About to update item: %#v", item)
item.UpdateDelta(1) item.UpdateDelta(1)
p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", subject, p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick,
item.Count, item.Item)) item.Count, item.Item))
return true return true
} }
func (p *CounterPlugin) decrementCmd(r bot.Request) bool { func (p *CounterPlugin) decrementCmd(r bot.Request) bool {
subject := r.Msg.User.Name nick, id := r.Msg.User.Name, r.Msg.User.ID
if r.Values["who"] != "" { if r.Values["who"] != "" {
subject = strings.TrimSuffix(r.Values["who"], ".") nick, id = p.resolveUser(r, r.Values["who"])
} }
itemName := r.Values["thing"] itemName := r.Values["thing"]
channel := r.Msg.Channel channel := r.Msg.Channel
// -- those fuckers // -- those fuckers
item, err := GetUserItem(p.DB, subject, itemName) item, err := GetUserItem(p.DB, nick, id, itemName)
if err != nil { if err != nil {
log.Error(). log.Error().
Err(err). Err(err).
Str("subject", subject). Str("nick", nick).
Str("id", id).
Str("itemName", itemName). Str("itemName", itemName).
Msg("Error finding item") Msg("Error finding item")
// Item ain't there, I guess // Item ain't there, I guess
return false return false
} }
item.UpdateDelta(-1) item.UpdateDelta(-1)
p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", subject, p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick,
item.Count, item.Item)) item.Count, item.Item))
return true return true
} }
func (p *CounterPlugin) addToCmd(r bot.Request) bool { func (p *CounterPlugin) addToCmd(r bot.Request) bool {
subject := r.Msg.User.Name nick, id := p.resolveUser(r, r.Values["who"])
if r.Values["who"] != "" {
subject = strings.TrimSuffix(r.Values["who"], ".")
}
itemName := r.Values["thing"] itemName := r.Values["thing"]
channel := r.Msg.Channel channel := r.Msg.Channel
// += those fuckers // += those fuckers
item, err := GetUserItem(p.DB, subject, itemName) item, err := GetUserItem(p.DB, nick, id, itemName)
if err != nil { if err != nil {
log.Error(). log.Error().
Err(err). Err(err).
Str("subject", subject). Str("nick", nick).
Str("id", id).
Str("itemName", itemName). Str("itemName", itemName).
Msg("Error finding item") Msg("Error finding item")
// Item ain't there, I guess // Item ain't there, I guess
@ -571,24 +637,22 @@ func (p *CounterPlugin) addToCmd(r bot.Request) bool {
n, _ := strconv.Atoi(r.Values["amount"]) n, _ := strconv.Atoi(r.Values["amount"])
log.Debug().Msgf("About to update item by %d: %#v", n, item) log.Debug().Msgf("About to update item by %d: %#v", n, item)
item.UpdateDelta(n) item.UpdateDelta(n)
p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", subject, p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick,
item.Count, item.Item)) item.Count, item.Item))
return true return true
} }
func (p *CounterPlugin) removeFromCmd(r bot.Request) bool { func (p *CounterPlugin) removeFromCmd(r bot.Request) bool {
subject := r.Msg.User.Name nick, id := p.resolveUser(r, r.Values["who"])
if r.Values["who"] != "" {
subject = strings.TrimSuffix(r.Values["who"], ".")
}
itemName := r.Values["thing"] itemName := r.Values["thing"]
channel := r.Msg.Channel channel := r.Msg.Channel
// -= those fuckers // -= those fuckers
item, err := GetUserItem(p.DB, subject, itemName) item, err := GetUserItem(p.DB, nick, id, itemName)
if err != nil { if err != nil {
log.Error(). log.Error().
Err(err). Err(err).
Str("subject", subject). Str("nick", nick).
Str("id", id).
Str("itemName", itemName). Str("itemName", itemName).
Msg("Error finding item") Msg("Error finding item")
// Item ain't there, I guess // Item ain't there, I guess
@ -597,7 +661,7 @@ func (p *CounterPlugin) removeFromCmd(r bot.Request) bool {
n, _ := strconv.Atoi(r.Values["amount"]) n, _ := strconv.Atoi(r.Values["amount"])
log.Debug().Msgf("About to update item by -%d: %#v", n, item) log.Debug().Msgf("About to update item by -%d: %#v", n, item)
item.UpdateDelta(-n) item.UpdateDelta(-n)
p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", subject, p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick,
item.Count, item.Item)) item.Count, item.Item))
return true return true
} }
@ -614,7 +678,7 @@ func (p *CounterPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message
} }
func (p *CounterPlugin) teaMatchCmd(r bot.Request) bool { func (p *CounterPlugin) teaMatchCmd(r bot.Request) bool {
nick := r.Msg.User.Name nick, id := r.Msg.User.Name, r.Msg.User.ID
channel := r.Msg.Channel channel := r.Msg.Channel
submatches := teaRegex.FindStringSubmatch(r.Msg.Body) submatches := teaRegex.FindStringSubmatch(r.Msg.Body)
@ -624,7 +688,7 @@ func (p *CounterPlugin) teaMatchCmd(r bot.Request) bool {
itemName := strings.ToLower(submatches[1]) itemName := strings.ToLower(submatches[1])
// We will specifically allow :tea: to keep compatability // We will specifically allow :tea: to keep compatability
item, err := GetUserItem(p.DB, nick, itemName) item, err := GetUserItem(p.DB, nick, id, itemName)
if err != nil || (item.Count == 0 && item.Item != ":tea:") { if err != nil || (item.Count == 0 && item.Item != ":tea:") {
log.Error(). log.Error().
Err(err). Err(err).
@ -687,7 +751,8 @@ func (p *CounterPlugin) handleCounterAPI(w http.ResponseWriter, r *http.Request)
w.Write(j) w.Write(j)
return return
} }
item, err := GetUserItem(p.DB, info.User, info.Thing) nick, id := p.resolveUser(bot.Request{Conn: p.Bot.DefaultConnector()}, info.User)
item, err := GetUserItem(p.DB, nick, id, info.Thing)
if err != nil { if err != nil {
log.Error(). log.Error().
Err(err). Err(err).
@ -744,3 +809,19 @@ func sendUpdate(who, what string, amount int) {
f(Update{who, what, amount}) f(Update{who, what, amount})
} }
} }
func (p *CounterPlugin) resolveUser(r bot.Request, nick string) (string, string) {
id := ""
if nick != "" {
nick = strings.TrimSuffix(nick, ".")
u, err := r.Conn.Profile(nick)
if err == nil && u.ID != "" {
id = u.ID
}
} else if r.Msg.User != nil {
nick, id = r.Msg.User.Name, r.Msg.User.ID
}
nick = strings.ToLower(nick)
log.Debug().Msgf("resolveUser(%s, %s) => (%s, %s)", r.Msg.User.ID, nick, nick, id)
return nick, id
}

View File

@ -33,9 +33,9 @@ func makeMessage(payload string, r *regexp.Regexp) bot.Request {
} }
values := bot.ParseValues(r, payload) values := bot.ParseValues(r, payload)
return bot.Request{ return bot.Request{
Conn: nil, Conn: &cli.CliPlugin{},
Msg: msg.Message{ Msg: msg.Message{
User: &user.User{Name: "tester"}, User: &user.User{Name: "tester", ID: "id"},
Body: payload, Body: payload,
Command: isCmd, Command: isCmd,
}, },
@ -48,7 +48,7 @@ func TestMkAlias(t *testing.T) {
assert.NotNil(t, c) assert.NotNil(t, c)
c.mkAliasCmd(makeMessage("mkalias fuck mornings", mkAliasRegex)) c.mkAliasCmd(makeMessage("mkalias fuck mornings", mkAliasRegex))
c.incrementCmd(makeMessage("fuck++", incrementRegex)) c.incrementCmd(makeMessage("fuck++", incrementRegex))
item, err := GetUserItem(mb.DB(), "tester", "mornings") item, err := GetUserItem(mb.DB(), "tester", "id", "mornings")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 1, item.Count) assert.Equal(t, 1, item.Count)
} }
@ -59,7 +59,7 @@ func TestRmAlias(t *testing.T) {
c.mkAliasCmd(makeMessage("mkalias fuck mornings", mkAliasRegex)) c.mkAliasCmd(makeMessage("mkalias fuck mornings", mkAliasRegex))
c.rmAliasCmd(makeMessage("rmalias fuck", rmAliasRegex)) c.rmAliasCmd(makeMessage("rmalias fuck", rmAliasRegex))
c.incrementCmd(makeMessage("fuck++", incrementRegex)) c.incrementCmd(makeMessage("fuck++", incrementRegex))
item, err := GetUserItem(mb.DB(), "tester", "mornings") item, err := GetUserItem(mb.DB(), "tester", "id", "mornings")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 0, item.Count) assert.Equal(t, 0, item.Count)
} }
@ -69,7 +69,7 @@ func TestThreeSentencesExists(t *testing.T) {
assert.NotNil(t, c) assert.NotNil(t, c)
c.incrementCmd(makeMessage(":beer:++", incrementRegex)) c.incrementCmd(makeMessage(":beer:++", incrementRegex))
c.teaMatchCmd(makeMessage(":beer:. Earl Grey. Hot.", teaRegex)) c.teaMatchCmd(makeMessage(":beer:. Earl Grey. Hot.", teaRegex))
item, err := GetUserItem(mb.DB(), "tester", ":beer:") item, err := GetUserItem(mb.DB(), "tester", "id", ":beer:")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 2, item.Count) assert.Equal(t, 2, item.Count)
} }
@ -77,9 +77,9 @@ func TestThreeSentencesExists(t *testing.T) {
func TestThreeSentencesNotExists(t *testing.T) { func TestThreeSentencesNotExists(t *testing.T) {
mb, c := setup(t) mb, c := setup(t)
assert.NotNil(t, c) assert.NotNil(t, c)
item, err := GetUserItem(mb.DB(), "tester", ":beer:") item, err := GetUserItem(mb.DB(), "tester", "id", ":beer:")
c.teaMatchCmd(makeMessage(":beer:. Earl Grey. Hot.", teaRegex)) c.teaMatchCmd(makeMessage(":beer:. Earl Grey. Hot.", teaRegex))
item, err = GetUserItem(mb.DB(), "tester", ":beer:") item, err = GetUserItem(mb.DB(), "tester", "id", ":beer:")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 0, item.Count) assert.Equal(t, 0, item.Count)
} }
@ -89,7 +89,7 @@ func TestTeaEarlGreyHot(t *testing.T) {
assert.NotNil(t, c) assert.NotNil(t, c)
c.teaMatchCmd(makeMessage("Tea. Earl Grey. Hot.", teaRegex)) c.teaMatchCmd(makeMessage("Tea. Earl Grey. Hot.", teaRegex))
c.teaMatchCmd(makeMessage("Tea. Earl Grey. Hot.", teaRegex)) c.teaMatchCmd(makeMessage("Tea. Earl Grey. Hot.", teaRegex))
item, err := GetUserItem(mb.DB(), "tester", ":tea:") item, err := GetUserItem(mb.DB(), "tester", "id", ":tea:")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 2, item.Count) assert.Equal(t, 2, item.Count)
} }
@ -99,7 +99,7 @@ func TestTeaTwoPeriods(t *testing.T) {
assert.NotNil(t, c) assert.NotNil(t, c)
c.teaMatchCmd(makeMessage("Tea. Earl Grey.", teaRegex)) c.teaMatchCmd(makeMessage("Tea. Earl Grey.", teaRegex))
c.teaMatchCmd(makeMessage("Tea. Earl Grey.", teaRegex)) c.teaMatchCmd(makeMessage("Tea. Earl Grey.", teaRegex))
item, err := GetUserItem(mb.DB(), "tester", ":tea:") item, err := GetUserItem(mb.DB(), "tester", "id", ":tea:")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 0, item.Count) assert.Equal(t, 0, item.Count)
} }
@ -109,7 +109,7 @@ func TestTeaMultiplePeriods(t *testing.T) {
assert.NotNil(t, c) assert.NotNil(t, c)
c.teaMatchCmd(makeMessage("Tea. Earl Grey. Spiked. Hot.", teaRegex)) c.teaMatchCmd(makeMessage("Tea. Earl Grey. Spiked. Hot.", teaRegex))
c.teaMatchCmd(makeMessage("Tea. Earl Grey. Spiked. Hot.", teaRegex)) c.teaMatchCmd(makeMessage("Tea. Earl Grey. Spiked. Hot.", teaRegex))
item, err := GetUserItem(mb.DB(), "tester", ":tea:") item, err := GetUserItem(mb.DB(), "tester", "id", ":tea:")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 2, item.Count) assert.Equal(t, 2, item.Count)
} }
@ -120,7 +120,7 @@ func TestTeaGreenHot(t *testing.T) {
c.teaMatchCmd(makeMessage("Tea. Green. Hot.", teaRegex)) c.teaMatchCmd(makeMessage("Tea. Green. Hot.", teaRegex))
c.teaMatchCmd(makeMessage("Tea. Green. Hot", teaRegex)) c.teaMatchCmd(makeMessage("Tea. Green. Hot", teaRegex))
c.teaMatchCmd(makeMessage("Tea. Green. Iced.", teaRegex)) c.teaMatchCmd(makeMessage("Tea. Green. Iced.", teaRegex))
item, err := GetUserItem(mb.DB(), "tester", ":tea:") item, err := GetUserItem(mb.DB(), "tester", "id", ":tea:")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 3, item.Count) assert.Equal(t, 3, item.Count)
} }
@ -130,7 +130,7 @@ func TestTeaUnrelated(t *testing.T) {
assert.NotNil(t, c) assert.NotNil(t, c)
c.teaMatchCmd(makeMessage("Tea.", teaRegex)) c.teaMatchCmd(makeMessage("Tea.", teaRegex))
c.teaMatchCmd(makeMessage("Tea. It's great.", teaRegex)) c.teaMatchCmd(makeMessage("Tea. It's great.", teaRegex))
item, err := GetUserItem(mb.DB(), "tester", ":tea:") item, err := GetUserItem(mb.DB(), "tester", "id", ":tea:")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 0, item.Count) assert.Equal(t, 0, item.Count)
} }
@ -139,7 +139,7 @@ func TestTeaSkieselQuote(t *testing.T) {
mb, c := setup(t) mb, c := setup(t)
assert.NotNil(t, c) assert.NotNil(t, c)
c.teaMatchCmd(makeMessage("blah, this is a whole page of explanation where \"we did local search and used a tabu list\" would have sufficed", teaRegex)) c.teaMatchCmd(makeMessage("blah, this is a whole page of explanation where \"we did local search and used a tabu list\" would have sufficed", teaRegex))
item, err := GetUserItem(mb.DB(), "tester", ":tea:") item, err := GetUserItem(mb.DB(), "tester", "id", ":tea:")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 0, item.Count) assert.Equal(t, 0, item.Count)
} }
@ -147,7 +147,7 @@ func TestTeaUnicodeJapanese(t *testing.T) {
mb, c := setup(t) mb, c := setup(t)
assert.NotNil(t, c) assert.NotNil(t, c)
c.teaMatchCmd(makeMessage("Tea. おちや. Hot.", teaRegex)) c.teaMatchCmd(makeMessage("Tea. おちや. Hot.", teaRegex))
item, err := GetUserItem(mb.DB(), "tester", ":tea:") item, err := GetUserItem(mb.DB(), "tester", "id", ":tea:")
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 1, item.Count) assert.Equal(t, 1, item.Count)
} }
@ -157,7 +157,7 @@ func TestResetMe(t *testing.T) {
assert.NotNil(t, c) assert.NotNil(t, c)
c.incrementCmd(makeMessage("test++", incrementRegex)) c.incrementCmd(makeMessage("test++", incrementRegex))
c.resetCmd(makeMessage("!reset me", resetRegex)) c.resetCmd(makeMessage("!reset me", resetRegex))
items, err := GetItems(mb.DB(), "tester") items, err := GetItems(mb.DB(), "tester", "id")
assert.Nil(t, err) assert.Nil(t, err)
assert.Len(t, items, 0) assert.Len(t, items, 0)
} }
@ -251,8 +251,9 @@ func TestCount(t *testing.T) {
} }
res := c.countCmd(makeMessage("!count test", countRegex)) res := c.countCmd(makeMessage("!count test", countRegex))
assert.True(t, res) assert.True(t, res)
assert.Len(t, mb.Messages, 5) if assert.Len(t, mb.Messages, 5) {
assert.Equal(t, mb.Messages[4], "tester has 4 test.") assert.Equal(t, "tester has 4 test.", mb.Messages[4])
}
} }
func TestInspectMe(t *testing.T) { func TestInspectMe(t *testing.T) {

View File

@ -188,7 +188,14 @@ func (p *GoalsPlugin) checkGoal(c bot.Connector, ch, what, who string) {
p.b.Send(c, bot.Message, ch, fmt.Sprintf("I couldn't find %s", what)) p.b.Send(c, bot.Message, ch, fmt.Sprintf("I couldn't find %s", what))
} }
item, err := counter.GetUserItem(p.db, who, what) nick, id := "", ""
user, err := c.Profile(who)
if err == nil && user.ID != "" {
id = user.ID
nick = user.Name
}
item, err := counter.GetUserItem(p.db, nick, id, what)
if err != nil { if err != nil {
p.b.Send(c, bot.Message, ch, fmt.Sprintf("I couldn't find any %s", what)) p.b.Send(c, bot.Message, ch, fmt.Sprintf("I couldn't find any %s", what))
return return