mirror of https://github.com/velour/catbase.git
Compare commits
No commits in common. "ba0f38b342de2f8acdf2c2125a06a36732c725e1" and "edc56dedc7af057ef222d116d2e002dd03f14093" have entirely different histories.
ba0f38b342
...
edc56dedc7
|
@ -33,9 +33,9 @@ import (
|
||||||
|
|
||||||
// This is a skeleton plugin to serve as an example and quick copy/paste for new plugins.
|
// 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 {
|
type BeersPlugin struct {
|
||||||
b bot.Bot
|
b bot.Bot
|
||||||
|
@ -240,15 +240,14 @@ func (p *BeersPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message,
|
||||||
return true
|
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
|
// TODO: really ought to have an ID here
|
||||||
booze, _ := counter.GetUserItem(db, user, id, itemName)
|
booze, _ := counter.GetUserItem(db, user, id, itemName)
|
||||||
return booze
|
return booze
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BeersPlugin) setBeers(r *bot.Request, user, id string, amount int) {
|
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)
|
||||||
ub := getUserBeers(p.db, user, id, itemName)
|
|
||||||
err := ub.Update(r, amount)
|
err := ub.Update(r, amount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msgf("Error saving beers")
|
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) {
|
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)
|
||||||
ub := getUserBeers(p.db, user, id, itemName)
|
|
||||||
err := ub.UpdateDelta(r, delta)
|
err := ub.UpdateDelta(r, 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(user, id string) int {
|
func (p *BeersPlugin) getBeers(nick, id string) int {
|
||||||
itemName := p.c.Get("beers.itemname", DEFAULT_ITEM)
|
ub := getUserBeers(p.db, nick, id)
|
||||||
ub := getUserBeers(p.db, user, id, itemName)
|
|
||||||
return ub.Count
|
return ub.Count
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,8 +53,8 @@ func makeBeersPlugin(t *testing.T) (*BeersPlugin, *bot.MockBot) {
|
||||||
counter.New(mb)
|
counter.New(mb)
|
||||||
mb.DB().MustExec(`delete from counter; delete from counter_alias;`)
|
mb.DB().MustExec(`delete from counter; delete from counter_alias;`)
|
||||||
b := New(mb)
|
b := New(mb)
|
||||||
counter.MkAlias(mb.DB(), "beer", DEFAULT_ITEM)
|
counter.MkAlias(mb.DB(), "beer", ":beer:")
|
||||||
counter.MkAlias(mb.DB(), "beers", DEFAULT_ITEM)
|
counter.MkAlias(mb.DB(), "beers", ":beer:")
|
||||||
return b, mb
|
return b, mb
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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", "id", DEFAULT_ITEM)
|
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", "id", DEFAULT_ITEM)
|
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", "id", DEFAULT_ITEM)
|
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", "id", DEFAULT_ITEM)
|
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", "id", DEFAULT_ITEM)
|
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", "id", DEFAULT_ITEM)
|
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", "id", DEFAULT_ITEM)
|
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")
|
||||||
|
|
|
@ -289,7 +289,7 @@ func (p *FirstPlugin) recordFirst(c bot.Connector, message msg.Message) {
|
||||||
Msg("Recording first")
|
Msg("Recording first")
|
||||||
first := &FirstEntry{
|
first := &FirstEntry{
|
||||||
day: Midnight(time.Now()),
|
day: Midnight(time.Now()),
|
||||||
time: time.Now(),
|
time: message.Time,
|
||||||
channel: message.Channel,
|
channel: message.Channel,
|
||||||
body: message.Body,
|
body: message.Body,
|
||||||
nick: message.User.Name,
|
nick: message.User.Name,
|
||||||
|
|
Loading…
Reference in New Issue