catbase/plugins/picker/picker.go

131 lines
3.2 KiB
Go
Raw Normal View History

2017-12-19 18:37:47 +00:00
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
package picker
import (
2018-08-30 18:20:44 +00:00
"errors"
2018-08-30 17:49:51 +00:00
"regexp"
2018-08-30 18:20:44 +00:00
"strconv"
2017-12-19 18:37:47 +00:00
"strings"
"fmt"
"math/rand"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
)
type PickerPlugin struct {
Bot bot.Bot
}
// NewPickerPlugin creates a new PickerPlugin with the Plugin interface
func New(bot bot.Bot) *PickerPlugin {
return &PickerPlugin{
Bot: bot,
}
}
// 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 execution of other plugins.
func (p *PickerPlugin) Message(message msg.Message) bool {
if !strings.HasPrefix(message.Body, "pick") {
2018-08-30 18:23:19 +00:00
return false
}
2018-08-30 17:49:51 +00:00
n, items, err := p.parse(message.Body)
if err != nil {
p.Bot.SendMessage(message.Channel, err.Error())
return false
}
2017-12-19 18:37:47 +00:00
2018-08-30 17:49:51 +00:00
if n == 1 {
2017-12-19 18:37:47 +00:00
item := items[rand.Intn(len(items))]
2018-08-30 17:49:51 +00:00
out := fmt.Sprintf("I've chosen %q for you.", strings.TrimSpace(item))
p.Bot.SendMessage(message.Channel, out)
return true
}
2017-12-19 18:37:47 +00:00
2018-08-30 17:49:51 +00:00
rand.Shuffle(len(items), func(i, j int) {
items[i], items[j] = items[j], items[i]
})
items = items[:n]
2017-12-19 18:37:47 +00:00
2018-08-30 17:49:51 +00:00
var b strings.Builder
b.WriteString("I've chosen these hot picks for you: { ")
fmt.Fprintf(&b, "%q", items[0])
for _, item := range items[1:] {
fmt.Fprintf(&b, ", %q", item)
}
b.WriteString(" }")
p.Bot.SendMessage(message.Channel, b.String())
return true
}
2017-12-19 18:37:47 +00:00
var pickerListPrologue = regexp.MustCompile(`^pick[ \t]+([0-9]*)[ \t]+\{[ \t]*`)
var pickerListItem = regexp.MustCompile(`^([^,]+),[ \t]*`)
var pickerListFinalItem = regexp.MustCompile(`^([^,}]+),?[ \t]*\}[ \t]*`)
func (p *PickerPlugin) parse(body string) (int, []string, error) {
2018-08-30 17:49:51 +00:00
subs := pickerListPrologue.FindStringSubmatch(body)
2018-08-30 18:23:19 +00:00
if subs == nil {
2018-08-30 17:49:51 +00:00
return 0, nil, errors.New("saddle up for a syntax error")
}
2018-08-30 17:49:51 +00:00
n := 0
var err error
if subs[1] != "" {
n, err = strconv.Atoi(subs[1])
if err != nil {
return 0, nil, err
}
2018-08-30 17:49:51 +00:00
}
2018-08-30 17:49:51 +00:00
var items []string
rest := body[len(subs[0]):]
for {
subs = pickerListItem.FindStringSubmatch(rest)
if subs == nil {
break
}
2018-08-30 17:49:51 +00:00
items = append(items, subs[1])
rest = rest[len(subs[0]):]
2017-12-19 18:37:47 +00:00
}
2018-08-30 17:49:51 +00:00
subs = pickerListFinalItem.FindStringSubmatch(rest)
if subs == nil {
2018-08-30 17:49:51 +00:00
return 0, nil, errors.New("lasso yerself that final curly brace, compadre")
}
items = append(items, subs[1])
2018-08-30 17:49:51 +00:00
if n < 1 || n > len(items) {
return 0, nil, errors.New("whoah there, bucko, I can't create something out of nothing")
}
return n, items, nil
2017-12-19 18:37:47 +00:00
}
// Help responds to help requests. Every plugin must implement a help function.
func (p *PickerPlugin) Help(channel string, parts []string) {
p.Bot.SendMessage(channel, "Choose from a list of options. Try \"pick {a,b,c}\".")
}
// Empty event handler because this plugin does not do anything on event recv
func (p *PickerPlugin) Event(kind string, message msg.Message) bool {
return false
}
// Handler for bot's own messages
func (p *PickerPlugin) BotMessage(message msg.Message) bool {
return false
}
// Register any web URLs desired
func (p *PickerPlugin) RegisterWeb() *string {
return nil
}
func (p *PickerPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false }