mirror of https://github.com/velour/catbase.git
Compare commits
No commits in common. "825a8d267f18e80faf511fe2e726a94f1a679309" and "ec47c31a80ee73531268dea5c80c6ac96466d9ed" have entirely different histories.
825a8d267f
...
ec47c31a80
|
@ -103,18 +103,18 @@ func (p *BeersPlugin) register() {
|
||||||
switch op {
|
switch op {
|
||||||
case "=":
|
case "=":
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
p.puke(r)
|
p.puke(r.Conn, nick, id, r.Msg.Channel)
|
||||||
} else {
|
} else {
|
||||||
p.setBeers(&r, nick, id, 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(&r, nick, id, 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(&r, nick, id, -count)
|
p.addBeers(nick, id, -count)
|
||||||
p.randomReply(r.Conn, r.Msg.Channel)
|
p.randomReply(r.Conn, r.Msg.Channel)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -127,9 +127,9 @@ func (p *BeersPlugin) register() {
|
||||||
nick := r.Msg.User.Name
|
nick := r.Msg.User.Name
|
||||||
id := r.Msg.User.ID
|
id := r.Msg.User.ID
|
||||||
if op == "++" {
|
if op == "++" {
|
||||||
p.addBeers(&r, nick, id, 1)
|
p.addBeers(nick, id, 1)
|
||||||
} else {
|
} else {
|
||||||
p.addBeers(&r, nick, id, -1)
|
p.addBeers(nick, id, -1)
|
||||||
}
|
}
|
||||||
p.randomReply(r.Conn, r.Msg.Channel)
|
p.randomReply(r.Conn, r.Msg.Channel)
|
||||||
return true
|
return true
|
||||||
|
@ -152,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)
|
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, r.Msg.User.Name, r.Msg.User.ID, 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
|
||||||
}},
|
}},
|
||||||
|
@ -245,17 +245,17 @@ func getUserBeers(db *sqlx.DB, user, id string) counter.Item {
|
||||||
return booze
|
return booze
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BeersPlugin) setBeers(r *bot.Request, user, id string, amount int) {
|
func (p *BeersPlugin) setBeers(user, id string, amount int) {
|
||||||
ub := getUserBeers(p.db, user, id)
|
ub := getUserBeers(p.db, user, id)
|
||||||
err := ub.Update(r, 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(r *bot.Request, user, id string, delta int) {
|
func (p *BeersPlugin) addBeers(user, id string, delta int) {
|
||||||
ub := getUserBeers(p.db, user, id)
|
ub := getUserBeers(p.db, user, id)
|
||||||
err := ub.UpdateDelta(r, 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")
|
||||||
}
|
}
|
||||||
|
@ -279,10 +279,10 @@ func (p *BeersPlugin) reportCount(c bot.Connector, nick, id, channel string, him
|
||||||
p.b.Send(c, bot.Message, channel, msg)
|
p.b.Send(c, bot.Message, channel, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BeersPlugin) puke(r bot.Request) {
|
func (p *BeersPlugin) puke(c bot.Connector, user, id string, channel string) {
|
||||||
p.setBeers(&r, r.Msg.User.Name, r.Msg.User.ID, 0)
|
p.setBeers(user, id, 0)
|
||||||
msg := fmt.Sprintf("Ohhhhhh, and a reversal of fortune for %s!", r.Msg.User.Name)
|
msg := fmt.Sprintf("Ohhhhhh, and a reversal of fortune for %s!", user)
|
||||||
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
|
p.b.Send(c, bot.Message, channel, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BeersPlugin) doIKnow(nick, id string) bool {
|
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)
|
// Don't add beers till after a photo has been detected (or failed once)
|
||||||
p.addBeers(nil, 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",
|
||||||
|
|
|
@ -65,7 +65,7 @@ func TestCounter(t *testing.T) {
|
||||||
t.Log(err)
|
t.Log(err)
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
err = i.Update(nil, 5)
|
err = i.Update(5)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -220,7 +220,7 @@ func (i *Item) Create() error {
|
||||||
|
|
||||||
// UpdateDelta sets a value
|
// UpdateDelta sets a value
|
||||||
// This will create or delete the item if necessary
|
// This will create or delete the item if necessary
|
||||||
func (i *Item) Update(r *bot.Request, value int) error {
|
func (i *Item) Update(value int) error {
|
||||||
i.Count = value
|
i.Count = value
|
||||||
if i.Count == 0 && i.ID != -1 {
|
if i.Count == 0 && i.ID != -1 {
|
||||||
return i.Delete()
|
return i.Delete()
|
||||||
|
@ -234,16 +234,16 @@ func (i *Item) Update(r *bot.Request, value int) error {
|
||||||
Msg("Updating item")
|
Msg("Updating item")
|
||||||
_, err := i.Exec(`update counter set count = ? where id = ?`, i.Count, i.ID)
|
_, err := i.Exec(`update counter set count = ? where id = ?`, i.Count, i.ID)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
sendUpdate(r, i.Nick, i.Item, i.Count)
|
sendUpdate(i.Nick, i.Item, i.Count)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateDelta changes a value according to some delta
|
// UpdateDelta changes a value according to some delta
|
||||||
// This will create or delete the item if necessary
|
// This will create or delete the item if necessary
|
||||||
func (i *Item) UpdateDelta(r *bot.Request, delta int) error {
|
func (i *Item) UpdateDelta(delta int) error {
|
||||||
i.Count += delta
|
i.Count += delta
|
||||||
return i.Update(r, i.Count)
|
return i.Update(i.Count)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete removes a counter from the database
|
// Delete removes a counter from the database
|
||||||
|
@ -587,7 +587,7 @@ func (p *CounterPlugin) incrementCmd(r bot.Request) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
log.Debug().Msgf("About to update item: %#v", item)
|
log.Debug().Msgf("About to update item: %#v", item)
|
||||||
item.UpdateDelta(&r, 1)
|
item.UpdateDelta(1)
|
||||||
p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick,
|
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
|
||||||
|
@ -612,7 +612,7 @@ func (p *CounterPlugin) decrementCmd(r bot.Request) bool {
|
||||||
// Item ain't there, I guess
|
// Item ain't there, I guess
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
item.UpdateDelta(&r, -1)
|
item.UpdateDelta(-1)
|
||||||
p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick,
|
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
|
||||||
|
@ -636,7 +636,7 @@ 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(&r, n)
|
item.UpdateDelta(n)
|
||||||
p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick,
|
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
|
||||||
|
@ -660,7 +660,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(&r, -n)
|
item.UpdateDelta(-n)
|
||||||
p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick,
|
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
|
||||||
|
@ -702,7 +702,7 @@ func (p *CounterPlugin) teaMatchCmd(r bot.Request) bool {
|
||||||
if item.Count < 0 {
|
if item.Count < 0 {
|
||||||
delta = -1
|
delta = -1
|
||||||
}
|
}
|
||||||
item.UpdateDelta(&r, delta)
|
item.UpdateDelta(delta)
|
||||||
p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s... %s has %d %s",
|
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))
|
strings.Join(everyDayImShuffling([]string{"bleep", "bloop", "blop"}), "-"), nick, item.Count, itemName))
|
||||||
return true
|
return true
|
||||||
|
@ -764,9 +764,9 @@ func (p *CounterPlugin) handleCounterAPI(w http.ResponseWriter, r *http.Request)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if info.Action == "++" {
|
if info.Action == "++" {
|
||||||
item.UpdateDelta(nil, 1)
|
item.UpdateDelta(1)
|
||||||
} else if info.Action == "--" {
|
} else if info.Action == "--" {
|
||||||
item.UpdateDelta(nil, -1)
|
item.UpdateDelta(-1)
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(400)
|
w.WriteHeader(400)
|
||||||
fmt.Fprint(w, "Invalid increment")
|
fmt.Fprint(w, "Invalid increment")
|
||||||
|
@ -795,7 +795,7 @@ type Update struct {
|
||||||
Amount int
|
Amount int
|
||||||
}
|
}
|
||||||
|
|
||||||
type updateFunc func(bot.Request, Update)
|
type updateFunc func(Update)
|
||||||
|
|
||||||
var updateFuncs = []updateFunc{}
|
var updateFuncs = []updateFunc{}
|
||||||
|
|
||||||
|
@ -803,14 +803,10 @@ func RegisterUpdate(f updateFunc) {
|
||||||
log.Debug().Msgf("registering update func")
|
log.Debug().Msgf("registering update func")
|
||||||
updateFuncs = append(updateFuncs, f)
|
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))
|
log.Debug().Msgf("sending updates to %d places", len(updateFuncs))
|
||||||
for _, f := range updateFuncs {
|
for _, f := range updateFuncs {
|
||||||
f(*r, Update{who, what, amount})
|
f(Update{who, what, amount})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -306,7 +306,7 @@ func (g goal) Delete() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *GoalsPlugin) update(r bot.Request, u counter.Update) {
|
func (p *GoalsPlugin) update(u counter.Update) {
|
||||||
log.Debug().Msgf("entered update for %#v", u)
|
log.Debug().Msgf("entered update for %#v", u)
|
||||||
gs, err := p.getGoal(u.Who, u.What)
|
gs, err := p.getGoal(u.Who, u.What)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -99,10 +99,8 @@ func (p *NewsBid) scoresCmd(r bot.Request) bool {
|
||||||
out := "NGate balances:\n"
|
out := "NGate balances:\n"
|
||||||
sort.Sort(bals)
|
sort.Sort(bals)
|
||||||
for _, b := range bals {
|
for _, b := range bals {
|
||||||
if b.Score > 0 {
|
|
||||||
out += fmt.Sprintf("%s has a total score of %d with %d left to bid this session\n", b.User, b.Score, b.Balance)
|
out += fmt.Sprintf("%s has a total score of %d with %d left to bid this session\n", b.User, b.Score, b.Balance)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
p.bot.Send(r.Conn, bot.Message, ch, out)
|
p.bot.Send(r.Conn, bot.Message, ch, out)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue