catbase/plugins/counter/counter.go

369 lines
9.7 KiB
Go
Raw Normal View History

2016-01-17 18:00:44 +00:00
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
2016-03-19 15:44:27 +00:00
package counter
2013-01-23 21:25:04 +00:00
import (
"database/sql"
2013-01-23 21:25:04 +00:00
"fmt"
"log"
"regexp"
2017-01-23 15:10:54 +00:00
"strconv"
2013-01-23 21:25:04 +00:00
"strings"
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"
"github.com/velour/catbase/bot/msg"
2013-01-23 21:25:04 +00:00
)
// This is a counter plugin to count arbitrary things.
type CounterPlugin struct {
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
ID int64
2013-01-23 21:25:04 +00:00
Nick string
Item string
Count int
}
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
}
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
log.Printf("GetItem db: %#v", db)
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)
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
func New(bot bot.Bot) *CounterPlugin {
if bot.DBVersion() == 1 {
if _, err := bot.DB().Exec(`create table if not exists counter (
id integer primary key,
nick string,
item string,
count integer
);`); err != nil {
log.Fatal(err)
}
}
2013-01-23 21:25:04 +00:00
return &CounterPlugin{
Bot: bot,
DB: bot.DB(),
2013-01-23 21:25:04 +00:00
}
}
// Message responds to the bot hook on recieving messages.
// 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.
func (p *CounterPlugin) Message(message msg.Message) bool {
2013-01-23 21:25:04 +00:00
// This bot does not reply to anything
nick := message.User.Name
channel := message.Channel
parts := strings.Fields(message.Body)
2013-01-23 21:25:04 +00:00
if len(parts) == 0 {
return false
}
if tea, _ := regexp.MatchString("(?i)^tea\\. [^.]*\\. ((hot)|(iced))\\.?$", message.Body); tea {
2017-02-16 21:28:45 +00:00
item, err := GetItem(p.DB, nick, ":tea:")
2017-01-31 20:35:05 +00:00
if err != nil {
2017-02-16 21:28:45 +00:00
log.Printf("Error finding item %s.%s: %s.", nick, ":tea:", err)
2017-01-31 20:35:05 +00:00
// Item ain't there, I guess
return false
}
log.Printf("About to update item: %#v", item)
item.UpdateDelta(1)
2017-02-16 21:28:45 +00:00
p.Bot.SendMessage(channel, fmt.Sprintf("bleep-bloop-blop... %s has %d :tea:",
2017-01-31 20:35:05 +00:00
nick, item.Count))
return true
} 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)
p.Bot.SendMessage(channel, "Something is technically wrong with your counters.")
return true
}
log.Printf("Items: %+v", items)
for _, item := range items {
item.Delete()
}
p.Bot.SendMessage(channel, fmt.Sprintf("%s, you are as new, my son.", nick))
return true
} else if message.Command && parts[0] == "inspect" && len(parts) == 2 {
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])
}
log.Printf("Getting counter for %s", subject)
// pull all of the items associated with "subject"
2016-03-19 18:02:46 +00:00
items, err := GetItems(p.DB, subject)
if err != nil {
2016-03-19 18:02:46 +00:00
log.Fatalf("Error retrieving items for %s: %s", subject, err)
p.Bot.SendMessage(channel, "Something went wrong finding that counter;")
return true
}
resp := fmt.Sprintf("%s has the following counters:", subject)
count := 0
2016-03-19 18:02:46 +00:00
for _, it := range items {
count += 1
if count > 1 {
resp += ","
}
2016-03-19 18:02:46 +00:00
resp += fmt.Sprintf(" %s: %d", it.Item, it.Count)
if count > 20 {
2016-03-19 18:02:46 +00:00
resp += ", and a few others"
break
}
}
2016-03-19 18:02:46 +00:00
resp += "."
if count == 0 {
p.Bot.SendMessage(channel, fmt.Sprintf("%s has no counters.", subject))
return true
}
p.Bot.SendMessage(channel, resp)
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)
p.Bot.SendMessage(channel, "Something went wrong removing that counter;")
return true
}
err = it.Delete()
if err != nil {
log.Printf("Error removing item %s.%s: %s", subject, itemName, err)
p.Bot.SendMessage(channel, "Something went wrong removing that counter;")
return true
}
2013-01-24 15:20:15 +00:00
p.Bot.SendAction(channel, fmt.Sprintf("chops a few %s out of his brain",
itemName))
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)
switch {
case err == sql.ErrNoRows:
2013-01-23 21:25:04 +00:00
p.Bot.SendMessage(channel, fmt.Sprintf("I don't think %s has any %s.",
subject, itemName))
return true
case err != nil:
2016-03-19 18:02:46 +00:00
log.Printf("Error retrieving item count for %s.%s: %s",
subject, itemName, err)
return true
2013-01-23 21:25:04 +00:00
}
2013-01-24 15:20:15 +00:00
p.Bot.SendMessage(channel, fmt.Sprintf("%s has %d %s.", subject, item.Count,
itemName))
2013-01-23 21:25:04 +00:00
return true
} else if len(parts) == 1 {
2016-03-19 18:02:46 +00:00
// Need to have at least 3 characters to ++ or --
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)
2013-01-24 15:35:39 +00:00
p.Bot.SendMessage(channel, fmt.Sprintf("%s has %d %s.", subject,
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)
2013-01-24 15:35:39 +00:00
p.Bot.SendMessage(channel, fmt.Sprintf("%s has %d %s.", subject,
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)
p.Bot.SendMessage(channel, fmt.Sprintf("%s has %d %s.", subject,
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)
p.Bot.SendMessage(channel, fmt.Sprintf("%s has %d %s.", subject,
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.
func (p *CounterPlugin) Help(channel string, parts []string) {
2013-01-24 15:35:39 +00:00
p.Bot.SendMessage(channel, "You can set counters incrementally by using "+
"<noun>++ and <noun>--. You can see all of your counters using "+
"\"inspect\", erase them with \"clear\", and view single counters with "+
"\"count\".")
2013-01-23 21:25:04 +00:00
}
// Empty event handler because this plugin does not do anything on event recv
func (p *CounterPlugin) Event(kind string, message msg.Message) bool {
2013-01-23 21:25:04 +00:00
return false
}
// Handler for bot's own messages
func (p *CounterPlugin) BotMessage(message msg.Message) bool {
return false
}
2013-06-01 17:10:15 +00:00
// Register any web URLs desired
func (p *CounterPlugin) RegisterWeb() *string {
return nil
}
func (p *CounterPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false }