2016-01-17 18:00:44 +00:00
|
|
|
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
|
2013-12-10 23:37:07 +00:00
|
|
|
|
2016-03-19 15:44:27 +00:00
|
|
|
package counter
|
2013-01-23 21:25:04 +00:00
|
|
|
|
|
|
|
import (
|
2016-01-15 06:12:26 +00:00
|
|
|
"database/sql"
|
2013-01-23 21:25:04 +00:00
|
|
|
"fmt"
|
2016-01-15 06:12:26 +00:00
|
|
|
"log"
|
2017-02-15 04:56:29 +00:00
|
|
|
"regexp"
|
2017-01-23 15:10:54 +00:00
|
|
|
"strconv"
|
2013-01-23 21:25:04 +00:00
|
|
|
"strings"
|
2016-01-15 06:12:26 +00:00
|
|
|
|
2016-03-19 18:02:46 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2016-01-17 18:00:44 +00:00
|
|
|
"github.com/velour/catbase/bot"
|
2016-04-01 14:20:03 +00:00
|
|
|
"github.com/velour/catbase/bot/msg"
|
2013-01-23 21:25:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// This is a counter plugin to count arbitrary things.
|
|
|
|
|
2018-10-19 18:20:40 +00:00
|
|
|
var teaMatcher = regexp.MustCompile("(?i)^([^.]+)\\. [^.]*\\. ([^.]*\\.?)+$")
|
|
|
|
|
2013-01-23 21:25:04 +00:00
|
|
|
type CounterPlugin struct {
|
2016-03-30 14:00:20 +00:00
|
|
|
Bot bot.Bot
|
2016-03-19 18:02:46 +00:00
|
|
|
DB *sqlx.DB
|
2013-01-23 21:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Item struct {
|
2016-03-19 18:02:46 +00:00
|
|
|
*sqlx.DB
|
|
|
|
|
2016-01-15 06:12:26 +00:00
|
|
|
ID int64
|
2013-01-23 21:25:04 +00:00
|
|
|
Nick string
|
|
|
|
Item string
|
|
|
|
Count int
|
|
|
|
}
|
|
|
|
|
2018-01-04 17:23:59 +00:00
|
|
|
type alias struct {
|
|
|
|
*sqlx.DB
|
|
|
|
|
|
|
|
ID int64
|
|
|
|
Item string
|
|
|
|
PointsTo string `db:"points_to"`
|
|
|
|
}
|
|
|
|
|
2016-03-19 18:27:02 +00:00
|
|
|
// GetItems returns all counters for a subject
|
2016-03-19 18:02:46 +00:00
|
|
|
func GetItems(db *sqlx.DB, nick string) ([]Item, error) {
|
|
|
|
var items []Item
|
|
|
|
err := db.Select(&items, `select * from counter where nick = ?`, nick)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Don't forget to embed the DB into all of that shiz
|
2017-01-24 02:13:21 +00:00
|
|
|
for i := range items {
|
|
|
|
items[i].DB = db
|
2016-03-19 18:02:46 +00:00
|
|
|
}
|
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
2018-01-04 12:39:24 +00:00
|
|
|
func LeaderAll(db *sqlx.DB) ([]Item, error) {
|
|
|
|
s := `select id,item,nick,max(count) as count from counter group by item having count(nick) > 1 and max(count) > 1 order by count desc`
|
|
|
|
var items []Item
|
|
|
|
err := db.Select(&items, s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for i := range items {
|
|
|
|
items[i].DB = db
|
|
|
|
}
|
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Leader(db *sqlx.DB, itemName string) ([]Item, error) {
|
2018-01-04 17:23:59 +00:00
|
|
|
itemName = strings.ToLower(itemName)
|
2018-01-04 12:39:24 +00:00
|
|
|
s := `select * from counter where item=? order by count desc`
|
|
|
|
var items []Item
|
|
|
|
err := db.Select(&items, s, itemName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for i := range items {
|
|
|
|
items[i].DB = db
|
|
|
|
}
|
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
2018-01-04 17:23:59 +00:00
|
|
|
func MkAlias(db *sqlx.DB, item, pointsTo string) (*alias, error) {
|
|
|
|
item = strings.ToLower(item)
|
|
|
|
pointsTo = strings.ToLower(pointsTo)
|
|
|
|
res, err := db.Exec(`insert into counter_alias (item, points_to) values (?, ?)`,
|
|
|
|
item, pointsTo)
|
|
|
|
if err != nil {
|
|
|
|
_, err := db.Exec(`update counter_alias set points_to=? where item=?`, pointsTo, item)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var a alias
|
|
|
|
if err := db.Get(&a, `select * from counter_alias where item=?`, item); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &a, nil
|
|
|
|
}
|
|
|
|
id, _ := res.LastInsertId()
|
|
|
|
|
|
|
|
return &alias{db, id, item, pointsTo}, nil
|
|
|
|
}
|
|
|
|
|
2016-03-19 18:27:02 +00:00
|
|
|
// GetItem returns a specific counter for a subject
|
2016-03-19 18:02:46 +00:00
|
|
|
func GetItem(db *sqlx.DB, nick, itemName string) (Item, error) {
|
|
|
|
var item Item
|
|
|
|
item.DB = db
|
2018-01-04 17:23:59 +00:00
|
|
|
var a alias
|
|
|
|
if err := db.Get(&a, `select * from counter_alias where item=?`, itemName); err == nil {
|
|
|
|
itemName = a.PointsTo
|
|
|
|
} else {
|
|
|
|
log.Println(err, a)
|
|
|
|
}
|
|
|
|
|
2016-03-19 18:02:46 +00:00
|
|
|
err := db.Get(&item, `select * from counter where nick = ? and item= ?`,
|
|
|
|
nick, itemName)
|
|
|
|
switch err {
|
|
|
|
case sql.ErrNoRows:
|
|
|
|
item.ID = -1
|
|
|
|
item.Nick = nick
|
|
|
|
item.Item = itemName
|
|
|
|
case nil:
|
|
|
|
default:
|
|
|
|
return Item{}, err
|
|
|
|
}
|
|
|
|
log.Printf("Got item %s.%s: %#v", nick, itemName, item)
|
|
|
|
return item, nil
|
|
|
|
}
|
|
|
|
|
2016-03-19 18:27:02 +00:00
|
|
|
// Create saves a counter
|
2016-03-19 18:02:46 +00:00
|
|
|
func (i *Item) Create() error {
|
|
|
|
res, err := i.Exec(`insert into counter (nick, item, count) values (?, ?, ?);`,
|
|
|
|
i.Nick, i.Item, i.Count)
|
2019-01-20 20:21:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-19 18:02:46 +00:00
|
|
|
id, _ := res.LastInsertId()
|
|
|
|
// hackhackhack?
|
|
|
|
i.ID = id
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-03-19 18:27:02 +00:00
|
|
|
// UpdateDelta sets a value
|
|
|
|
// This will create or delete the item if necessary
|
|
|
|
func (i *Item) Update(value int) error {
|
|
|
|
i.Count = value
|
2016-03-19 18:02:46 +00:00
|
|
|
if i.Count == 0 && i.ID != -1 {
|
|
|
|
return i.Delete()
|
|
|
|
}
|
|
|
|
if i.ID == -1 {
|
|
|
|
i.Create()
|
|
|
|
}
|
2016-03-19 18:27:02 +00:00
|
|
|
log.Printf("Updating item: %#v, value: %d", i, value)
|
2016-03-19 18:02:46 +00:00
|
|
|
_, err := i.Exec(`update counter set count = ? where id = ?`, i.Count, i.ID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-03-19 18:27:02 +00:00
|
|
|
// UpdateDelta changes a value according to some delta
|
|
|
|
// This will create or delete the item if necessary
|
|
|
|
func (i *Item) UpdateDelta(delta int) error {
|
|
|
|
i.Count += delta
|
|
|
|
return i.Update(i.Count)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes a counter from the database
|
2016-03-19 18:02:46 +00:00
|
|
|
func (i *Item) Delete() error {
|
|
|
|
_, err := i.Exec(`delete from counter where id = ?`, i.ID)
|
|
|
|
i.ID = -1
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-01-23 21:25:04 +00:00
|
|
|
// NewCounterPlugin creates a new CounterPlugin with the Plugin interface
|
2019-02-05 19:41:38 +00:00
|
|
|
func New(b bot.Bot) *CounterPlugin {
|
|
|
|
tx := b.DB().MustBegin()
|
|
|
|
b.DB().MustExec(`create table if not exists counter (
|
2016-01-15 06:12:26 +00:00
|
|
|
id integer primary key,
|
|
|
|
nick string,
|
|
|
|
item string,
|
|
|
|
count integer
|
2019-01-21 17:36:02 +00:00
|
|
|
);`)
|
2019-02-05 19:41:38 +00:00
|
|
|
b.DB().MustExec(`create table if not exists counter_alias (
|
2018-01-04 17:23:59 +00:00
|
|
|
id integer PRIMARY KEY AUTOINCREMENT,
|
|
|
|
item string NOT NULL UNIQUE,
|
|
|
|
points_to string NOT NULL
|
2019-01-21 17:36:02 +00:00
|
|
|
);`)
|
|
|
|
tx.Commit()
|
2019-02-05 19:41:38 +00:00
|
|
|
cp := &CounterPlugin{
|
|
|
|
Bot: b,
|
|
|
|
DB: b.DB(),
|
2013-01-23 21:25:04 +00:00
|
|
|
}
|
2019-02-05 19:41:38 +00:00
|
|
|
b.Register(cp, bot.Message, cp.message)
|
|
|
|
b.Register(cp, bot.Help, cp.help)
|
|
|
|
return cp
|
2013-01-23 21:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Message responds to the bot hook on recieving messages.
|
2013-05-07 23:30:37 +00:00
|
|
|
// This function returns true if the plugin responds in a meaningful way to the
|
|
|
|
// users message. Otherwise, the function returns false and the bot continues
|
2013-01-24 15:20:15 +00:00
|
|
|
// execution of other plugins.
|
2019-02-05 19:41:38 +00:00
|
|
|
func (p *CounterPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
2013-01-23 21:25:04 +00:00
|
|
|
// This bot does not reply to anything
|
|
|
|
nick := message.User.Name
|
|
|
|
channel := message.Channel
|
2016-05-11 16:10:15 +00:00
|
|
|
parts := strings.Fields(message.Body)
|
2013-01-23 21:25:04 +00:00
|
|
|
|
|
|
|
if len(parts) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-01-04 17:23:59 +00:00
|
|
|
if len(parts) == 3 && strings.ToLower(parts[0]) == "mkalias" {
|
|
|
|
if _, err := MkAlias(p.DB, parts[1], parts[2]); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return false
|
|
|
|
}
|
2019-02-05 15:54:13 +00:00
|
|
|
p.Bot.Send(bot.Message, channel, fmt.Sprintf("Created alias %s -> %s",
|
2018-01-04 17:23:59 +00:00
|
|
|
parts[1], parts[2]))
|
|
|
|
return true
|
|
|
|
} else if strings.ToLower(parts[0]) == "leaderboard" {
|
2018-01-04 12:39:24 +00:00
|
|
|
var cmd func() ([]Item, error)
|
|
|
|
itNameTxt := ""
|
|
|
|
|
|
|
|
if len(parts) == 1 {
|
|
|
|
cmd = func() ([]Item, error) { return LeaderAll(p.DB) }
|
|
|
|
} else {
|
|
|
|
itNameTxt = fmt.Sprintf(" for %s", parts[1])
|
|
|
|
cmd = func() ([]Item, error) { return Leader(p.DB, parts[1]) }
|
|
|
|
}
|
|
|
|
|
|
|
|
its, err := cmd()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return false
|
|
|
|
} else if len(its) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
out := fmt.Sprintf("Leaderboard%s:\n", itNameTxt)
|
|
|
|
for _, it := range its {
|
|
|
|
out += fmt.Sprintf("%s with %d %s\n",
|
|
|
|
it.Nick,
|
|
|
|
it.Count,
|
|
|
|
it.Item,
|
|
|
|
)
|
|
|
|
}
|
2019-02-05 15:54:13 +00:00
|
|
|
p.Bot.Send(bot.Message, channel, out)
|
2018-01-04 12:39:24 +00:00
|
|
|
return true
|
2018-10-19 18:20:40 +00:00
|
|
|
} else if match := teaMatcher.MatchString(message.Body); match {
|
|
|
|
// check for tea match TTT
|
|
|
|
return p.checkMatch(message)
|
2017-01-31 20:35:05 +00:00
|
|
|
} else if message.Command && message.Body == "reset me" {
|
2017-01-24 02:13:21 +00:00
|
|
|
items, err := GetItems(p.DB, strings.ToLower(nick))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error getting items to reset %s: %s", nick, err)
|
2019-02-05 15:54:13 +00:00
|
|
|
p.Bot.Send(bot.Message, channel, "Something is technically wrong with your counters.")
|
2017-01-24 02:13:21 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
log.Printf("Items: %+v", items)
|
|
|
|
for _, item := range items {
|
|
|
|
item.Delete()
|
|
|
|
}
|
2019-02-05 15:54:13 +00:00
|
|
|
p.Bot.Send(bot.Message, channel, fmt.Sprintf("%s, you are as new, my son.", nick))
|
2017-01-24 02:13:21 +00:00
|
|
|
return true
|
|
|
|
} else if message.Command && parts[0] == "inspect" && len(parts) == 2 {
|
2013-01-23 21:40:51 +00:00
|
|
|
var subject string
|
|
|
|
|
|
|
|
if parts[1] == "me" {
|
|
|
|
subject = strings.ToLower(nick)
|
|
|
|
} else {
|
2016-03-19 18:02:46 +00:00
|
|
|
subject = strings.ToLower(parts[1])
|
2013-01-23 21:40:51 +00:00
|
|
|
}
|
|
|
|
|
2016-03-13 14:13:41 +00:00
|
|
|
log.Printf("Getting counter for %s", subject)
|
2013-01-23 21:40:51 +00:00
|
|
|
// pull all of the items associated with "subject"
|
2016-03-19 18:02:46 +00:00
|
|
|
items, err := GetItems(p.DB, subject)
|
2016-01-15 06:12:26 +00:00
|
|
|
if err != nil {
|
2016-03-19 18:02:46 +00:00
|
|
|
log.Fatalf("Error retrieving items for %s: %s", subject, err)
|
2019-02-05 15:54:13 +00:00
|
|
|
p.Bot.Send(bot.Message, channel, "Something went wrong finding that counter;")
|
2016-03-19 18:02:46 +00:00
|
|
|
return true
|
2013-01-23 21:45:37 +00:00
|
|
|
}
|
|
|
|
|
2013-01-23 21:40:51 +00:00
|
|
|
resp := fmt.Sprintf("%s has the following counters:", subject)
|
2016-01-15 06:12:26 +00:00
|
|
|
count := 0
|
2016-03-19 18:02:46 +00:00
|
|
|
for _, it := range items {
|
2016-01-15 06:12:26 +00:00
|
|
|
count += 1
|
|
|
|
if count > 1 {
|
2016-03-30 14:00:20 +00:00
|
|
|
resp += ","
|
2013-01-23 21:40:51 +00:00
|
|
|
}
|
2016-03-19 18:02:46 +00:00
|
|
|
resp += fmt.Sprintf(" %s: %d", it.Item, it.Count)
|
2016-01-15 06:12:26 +00:00
|
|
|
if count > 20 {
|
2016-03-19 18:02:46 +00:00
|
|
|
resp += ", and a few others"
|
2013-01-23 21:40:51 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-03-19 18:02:46 +00:00
|
|
|
resp += "."
|
2013-01-23 21:40:51 +00:00
|
|
|
|
2016-01-15 06:12:26 +00:00
|
|
|
if count == 0 {
|
2019-02-05 15:54:13 +00:00
|
|
|
p.Bot.Send(bot.Message, channel, fmt.Sprintf("%s has no counters.", subject))
|
2016-01-15 06:12:26 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-02-05 15:54:13 +00:00
|
|
|
p.Bot.Send(bot.Message, channel, resp)
|
2013-01-23 21:40:51 +00:00
|
|
|
return true
|
|
|
|
} else if message.Command && len(parts) == 2 && parts[0] == "clear" {
|
|
|
|
subject := strings.ToLower(nick)
|
|
|
|
itemName := strings.ToLower(parts[1])
|
|
|
|
|
2016-03-19 18:02:46 +00:00
|
|
|
it, err := GetItem(p.DB, subject, itemName)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error getting item to remove %s.%s: %s", subject, itemName, err)
|
2019-02-05 15:54:13 +00:00
|
|
|
p.Bot.Send(bot.Message, channel, "Something went wrong removing that counter;")
|
2016-03-19 18:02:46 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
err = it.Delete()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error removing item %s.%s: %s", subject, itemName, err)
|
2019-02-05 15:54:13 +00:00
|
|
|
p.Bot.Send(bot.Message, channel, "Something went wrong removing that counter;")
|
2016-01-15 06:12:26 +00:00
|
|
|
return true
|
|
|
|
}
|
2013-01-23 21:40:51 +00:00
|
|
|
|
2019-02-05 15:54:13 +00:00
|
|
|
p.Bot.Send(bot.Action, channel, fmt.Sprintf("chops a few %s out of his brain",
|
2013-01-24 15:20:15 +00:00
|
|
|
itemName))
|
2013-01-23 21:40:51 +00:00
|
|
|
return true
|
|
|
|
|
|
|
|
} else if message.Command && parts[0] == "count" {
|
2013-01-23 21:25:04 +00:00
|
|
|
var subject string
|
|
|
|
var itemName string
|
|
|
|
|
|
|
|
if len(parts) == 3 {
|
|
|
|
// report count for parts[1]
|
|
|
|
subject = strings.ToLower(parts[1])
|
|
|
|
itemName = strings.ToLower(parts[2])
|
|
|
|
} else if len(parts) == 2 {
|
|
|
|
subject = strings.ToLower(nick)
|
|
|
|
itemName = strings.ToLower(parts[1])
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var item Item
|
2016-03-19 18:02:46 +00:00
|
|
|
item, err := GetItem(p.DB, subject, itemName)
|
2016-01-15 06:12:26 +00:00
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
2019-02-05 15:54:13 +00:00
|
|
|
p.Bot.Send(bot.Message, channel, fmt.Sprintf("I don't think %s has any %s.",
|
2013-01-23 21:25:04 +00:00
|
|
|
subject, itemName))
|
|
|
|
return true
|
2016-01-15 06:12:26 +00:00
|
|
|
case err != nil:
|
2016-03-19 18:02:46 +00:00
|
|
|
log.Printf("Error retrieving item count for %s.%s: %s",
|
|
|
|
subject, itemName, err)
|
2016-01-15 06:12:26 +00:00
|
|
|
return true
|
2013-01-23 21:25:04 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 15:54:13 +00:00
|
|
|
p.Bot.Send(bot.Message, channel, fmt.Sprintf("%s has %d %s.", subject, item.Count,
|
2013-01-24 15:20:15 +00:00
|
|
|
itemName))
|
2013-01-23 21:25:04 +00:00
|
|
|
|
|
|
|
return true
|
2018-09-28 02:47:24 +00:00
|
|
|
} else if len(parts) <= 2 {
|
|
|
|
if (len(parts) == 2) && (parts[1] == "++" || parts[1] == "--") {
|
|
|
|
parts = []string{parts[0] + parts[1]}
|
|
|
|
}
|
2016-03-19 18:02:46 +00:00
|
|
|
// Need to have at least 3 characters to ++ or --
|
2013-01-29 21:27:38 +00:00
|
|
|
if len(parts[0]) < 3 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-01-23 21:25:04 +00:00
|
|
|
subject := strings.ToLower(nick)
|
|
|
|
itemName := strings.ToLower(parts[0])[:len(parts[0])-2]
|
|
|
|
|
2013-01-24 15:35:39 +00:00
|
|
|
if nameParts := strings.SplitN(itemName, ".", 2); len(nameParts) == 2 {
|
|
|
|
subject = nameParts[0]
|
|
|
|
itemName = nameParts[1]
|
|
|
|
}
|
|
|
|
|
2013-01-23 21:25:04 +00:00
|
|
|
if strings.HasSuffix(parts[0], "++") {
|
|
|
|
// ++ those fuckers
|
2016-03-19 18:02:46 +00:00
|
|
|
item, err := GetItem(p.DB, subject, itemName)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error finding item %s.%s: %s.", subject, itemName, err)
|
|
|
|
// Item ain't there, I guess
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
log.Printf("About to update item: %#v", item)
|
2016-03-19 18:27:02 +00:00
|
|
|
item.UpdateDelta(1)
|
2019-02-05 15:54:13 +00:00
|
|
|
p.Bot.Send(bot.Message, channel, fmt.Sprintf("%s has %d %s.", subject,
|
2013-01-24 15:35:39 +00:00
|
|
|
item.Count, item.Item))
|
2013-01-23 21:25:04 +00:00
|
|
|
return true
|
|
|
|
} else if strings.HasSuffix(parts[0], "--") {
|
|
|
|
// -- those fuckers
|
2016-03-19 18:02:46 +00:00
|
|
|
item, err := GetItem(p.DB, subject, itemName)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error finding item %s.%s: %s.", subject, itemName, err)
|
|
|
|
// Item ain't there, I guess
|
|
|
|
return false
|
|
|
|
}
|
2016-03-19 18:27:02 +00:00
|
|
|
item.UpdateDelta(-1)
|
2019-02-05 15:54:13 +00:00
|
|
|
p.Bot.Send(bot.Message, channel, fmt.Sprintf("%s has %d %s.", subject,
|
2013-01-24 15:35:39 +00:00
|
|
|
item.Count, item.Item))
|
2013-01-23 21:25:04 +00:00
|
|
|
return true
|
|
|
|
}
|
2017-01-17 22:57:39 +00:00
|
|
|
} else if len(parts) == 3 {
|
|
|
|
// Need to have at least 3 characters to ++ or --
|
|
|
|
if len(parts[0]) < 3 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
subject := strings.ToLower(nick)
|
2017-01-23 15:10:54 +00:00
|
|
|
itemName := strings.ToLower(parts[0])
|
2017-01-17 22:57:39 +00:00
|
|
|
|
|
|
|
if nameParts := strings.SplitN(itemName, ".", 2); len(nameParts) == 2 {
|
|
|
|
subject = nameParts[0]
|
|
|
|
itemName = nameParts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
if parts[1] == "+=" {
|
|
|
|
// += those fuckers
|
|
|
|
item, err := GetItem(p.DB, subject, itemName)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error finding item %s.%s: %s.", subject, itemName, err)
|
|
|
|
// Item ain't there, I guess
|
|
|
|
return false
|
|
|
|
}
|
2017-01-23 15:10:54 +00:00
|
|
|
n, _ := strconv.Atoi(parts[2])
|
2017-01-17 22:57:39 +00:00
|
|
|
log.Printf("About to update item by %d: %#v", n, item)
|
|
|
|
item.UpdateDelta(n)
|
2019-02-05 15:54:13 +00:00
|
|
|
p.Bot.Send(bot.Message, channel, fmt.Sprintf("%s has %d %s.", subject,
|
2017-01-17 22:57:39 +00:00
|
|
|
item.Count, item.Item))
|
|
|
|
return true
|
|
|
|
} else if parts[1] == "-=" {
|
|
|
|
// -= those fuckers
|
|
|
|
item, err := GetItem(p.DB, subject, itemName)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error finding item %s.%s: %s.", subject, itemName, err)
|
|
|
|
// Item ain't there, I guess
|
|
|
|
return false
|
|
|
|
}
|
2017-01-23 15:10:54 +00:00
|
|
|
n, _ := strconv.Atoi(parts[2])
|
2017-01-17 22:57:39 +00:00
|
|
|
log.Printf("About to update item by -%d: %#v", n, item)
|
|
|
|
item.UpdateDelta(-n)
|
2019-02-05 15:54:13 +00:00
|
|
|
p.Bot.Send(bot.Message, channel, fmt.Sprintf("%s has %d %s.", subject,
|
2017-01-17 22:57:39 +00:00
|
|
|
item.Count, item.Item))
|
|
|
|
return true
|
|
|
|
}
|
2013-01-23 21:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Help responds to help requests. Every plugin must implement a help function.
|
2019-02-05 19:41:38 +00:00
|
|
|
func (p *CounterPlugin) help(kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
|
|
|
p.Bot.Send(bot.Message, message.Channel, "You can set counters incrementally by using "+
|
2013-01-24 15:35:39 +00:00
|
|
|
"<noun>++ and <noun>--. You can see all of your counters using "+
|
|
|
|
"\"inspect\", erase them with \"clear\", and view single counters with "+
|
|
|
|
"\"count\".")
|
2019-02-05 19:41:38 +00:00
|
|
|
return true
|
2013-05-08 00:08:18 +00:00
|
|
|
}
|
2013-06-01 17:10:15 +00:00
|
|
|
|
2018-10-19 18:20:40 +00:00
|
|
|
func (p *CounterPlugin) checkMatch(message msg.Message) bool {
|
|
|
|
nick := message.User.Name
|
|
|
|
channel := message.Channel
|
|
|
|
|
|
|
|
submatches := teaMatcher.FindStringSubmatch(message.Body)
|
|
|
|
if len(submatches) <= 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
itemName := strings.ToLower(submatches[1])
|
|
|
|
|
|
|
|
// We will specifically allow :tea: to keep compatability
|
|
|
|
item, err := GetItem(p.DB, nick, itemName)
|
|
|
|
if err != nil || (item.Count == 0 && item.Item != ":tea:") {
|
|
|
|
log.Printf("Error finding item %s.%s: %s.", nick, itemName, err)
|
|
|
|
// Item ain't there, I guess
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
log.Printf("About to update item: %#v", item)
|
|
|
|
item.UpdateDelta(1)
|
2019-02-05 15:54:13 +00:00
|
|
|
p.Bot.Send(bot.Message, channel, fmt.Sprintf("bleep-bloop-blop... %s has %d %s",
|
2018-10-19 18:20:40 +00:00
|
|
|
nick, item.Count, itemName))
|
|
|
|
return true
|
|
|
|
}
|