Compare commits

..

No commits in common. "ba0f38b342de2f8acdf2c2125a06a36732c725e1" and "edc56dedc7af057ef222d116d2e002dd03f14093" have entirely different histories.

3 changed files with 17 additions and 20 deletions

View File

@ -33,9 +33,9 @@ import (
// This is a skeleton plugin to serve as an example and quick copy/paste for new plugins.
var cachedImages = map[string][]byte{}
const itemName = ":beer:"
const DEFAULT_ITEM = "🍺"
var cachedImages = map[string][]byte{}
type BeersPlugin struct {
b bot.Bot
@ -240,15 +240,14 @@ func (p *BeersPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message,
return true
}
func getUserBeers(db *sqlx.DB, user, id, itemName string) counter.Item {
func getUserBeers(db *sqlx.DB, user, id string) counter.Item {
// TODO: really ought to have an ID here
booze, _ := counter.GetUserItem(db, user, id, itemName)
return booze
}
func (p *BeersPlugin) setBeers(r *bot.Request, user, id string, amount int) {
itemName := p.c.Get("beers.itemname", DEFAULT_ITEM)
ub := getUserBeers(p.db, user, id, itemName)
ub := getUserBeers(p.db, user, id)
err := ub.Update(r, amount)
if err != nil {
log.Error().Err(err).Msgf("Error saving beers")
@ -256,17 +255,15 @@ func (p *BeersPlugin) setBeers(r *bot.Request, user, id string, amount int) {
}
func (p *BeersPlugin) addBeers(r *bot.Request, user, id string, delta int) {
itemName := p.c.Get("beers.itemname", DEFAULT_ITEM)
ub := getUserBeers(p.db, user, id, itemName)
ub := getUserBeers(p.db, user, id)
err := ub.UpdateDelta(r, delta)
if err != nil {
log.Error().Err(err).Msgf("Error saving beers")
}
}
func (p *BeersPlugin) getBeers(user, id string) int {
itemName := p.c.Get("beers.itemname", DEFAULT_ITEM)
ub := getUserBeers(p.db, user, id, itemName)
func (p *BeersPlugin) getBeers(nick, id string) int {
ub := getUserBeers(p.db, nick, id)
return ub.Count
}

View File

@ -53,8 +53,8 @@ func makeBeersPlugin(t *testing.T) (*BeersPlugin, *bot.MockBot) {
counter.New(mb)
mb.DB().MustExec(`delete from counter; delete from counter_alias;`)
b := New(mb)
counter.MkAlias(mb.DB(), "beer", DEFAULT_ITEM)
counter.MkAlias(mb.DB(), "beers", DEFAULT_ITEM)
counter.MkAlias(mb.DB(), "beer", ":beer:")
counter.MkAlias(mb.DB(), "beers", ":beer:")
return b, mb
}
@ -75,7 +75,7 @@ func TestImbibe(t *testing.T) {
assert.Len(t, mb.Messages, 1)
testMessage(b, "imbibe")
assert.Len(t, mb.Messages, 2)
it, err := counter.GetUserItem(mb.DB(), "tester", "id", DEFAULT_ITEM)
it, err := counter.GetUserItem(mb.DB(), "tester", "id", itemName)
assert.Nil(t, err)
assert.Equal(t, 2, it.Count)
}
@ -83,7 +83,7 @@ func TestEq(t *testing.T) {
b, mb := makeBeersPlugin(t)
testMessage(b, "beers = 3")
assert.Len(t, mb.Messages, 1)
it, err := counter.GetUserItem(mb.DB(), "tester", "id", DEFAULT_ITEM)
it, err := counter.GetUserItem(mb.DB(), "tester", "id", itemName)
assert.Nil(t, err)
assert.Equal(t, 3, it.Count)
}
@ -94,7 +94,7 @@ func TestEqZero(t *testing.T) {
testMessage(b, "beers = 0")
assert.Len(t, mb.Messages, 2)
assert.Contains(t, mb.Messages[1], "reversal of fortune")
it, err := counter.GetUserItem(mb.DB(), "tester", "id", DEFAULT_ITEM)
it, err := counter.GetUserItem(mb.DB(), "tester", "id", itemName)
assert.Nil(t, err)
assert.Equal(t, 0, it.Count)
}
@ -105,7 +105,7 @@ func TestBeersPlusEq(t *testing.T) {
assert.Len(t, mb.Messages, 1)
testMessage(b, "beers += 5")
assert.Len(t, mb.Messages, 2)
it, err := counter.GetUserItem(mb.DB(), "tester", "id", DEFAULT_ITEM)
it, err := counter.GetUserItem(mb.DB(), "tester", "id", itemName)
assert.Nil(t, err)
assert.Equal(t, 10, it.Count)
}
@ -113,11 +113,11 @@ func TestBeersPlusEq(t *testing.T) {
func TestPuke(t *testing.T) {
b, mb := makeBeersPlugin(t)
testMessage(b, "beers += 5")
it, err := counter.GetUserItem(mb.DB(), "tester", "id", DEFAULT_ITEM)
it, err := counter.GetUserItem(mb.DB(), "tester", "id", itemName)
assert.Nil(t, err)
assert.Equal(t, 5, it.Count)
testMessage(b, "puke")
it, err = counter.GetUserItem(mb.DB(), "tester", "id", DEFAULT_ITEM)
it, err = counter.GetUserItem(mb.DB(), "tester", "id", itemName)
assert.Nil(t, err)
assert.Equal(t, 0, it.Count)
}
@ -125,7 +125,7 @@ func TestPuke(t *testing.T) {
func TestBeersReport(t *testing.T) {
b, mb := makeBeersPlugin(t)
testMessage(b, "beers += 5")
it, err := counter.GetUserItem(mb.DB(), "tester", "id", DEFAULT_ITEM)
it, err := counter.GetUserItem(mb.DB(), "tester", "id", itemName)
assert.Nil(t, err)
assert.Equal(t, 5, it.Count)
testMessage(b, "beers")

View File

@ -289,7 +289,7 @@ func (p *FirstPlugin) recordFirst(c bot.Connector, message msg.Message) {
Msg("Recording first")
first := &FirstEntry{
day: Midnight(time.Now()),
time: time.Now(),
time: message.Time,
channel: message.Channel,
body: message.Body,
nick: message.User.Name,