catbase/plugins/picker/picker.go

122 lines
3.0 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"
"github.com/rs/zerolog/log"
2017-12-19 18:37:47 +00:00
"fmt"
"math/rand"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
)
type PickerPlugin struct {
2019-05-27 23:21:53 +00:00
bot bot.Bot
2017-12-19 18:37:47 +00:00
}
// NewPickerPlugin creates a new PickerPlugin with the Plugin interface
func New(b bot.Bot) *PickerPlugin {
pp := &PickerPlugin{
2019-05-27 23:21:53 +00:00
bot: b,
2017-12-19 18:37:47 +00:00
}
2021-02-19 17:12:02 +00:00
b.RegisterRegex(pp, bot.Message, pickRegex, pp.message)
b.Register(pp, bot.Help, pp.help)
return pp
2017-12-19 18:37:47 +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 execution of other plugins.
2021-02-19 17:12:02 +00:00
func (p *PickerPlugin) message(r bot.Request) bool {
message := r.Msg
2018-08-30 18:23:19 +00:00
2018-08-30 17:49:51 +00:00
n, items, err := p.parse(message.Body)
if err != nil {
2021-02-19 17:12:02 +00:00
p.bot.Send(r.Conn, bot.Message, message.Channel, err.Error())
return true
2018-08-30 17:49:51 +00:00
}
2017-12-19 18:37:47 +00:00
2021-02-02 21:02:04 +00:00
preferences := p.bot.Config().GetArray("picker.preferences", []string{})
for _, it := range items {
for _, pref := range preferences {
if pref == it {
out := fmt.Sprintf("I've chosen %q for you.", strings.TrimSpace(it))
2021-02-19 17:12:02 +00:00
p.bot.Send(r.Conn, bot.Message, message.Channel, out)
2021-02-02 21:02:04 +00:00
return true
}
}
}
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))
2021-02-19 17:12:02 +00:00
p.bot.Send(r.Conn, bot.Message, message.Channel, out)
2018-08-30 17:49:51 +00:00
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(" }")
2021-02-19 17:12:02 +00:00
p.bot.Send(r.Conn, bot.Message, message.Channel, b.String())
2018-08-30 17:49:51 +00:00
return true
}
2017-12-19 18:37:47 +00:00
var pickRegex = regexp.MustCompile(`^pick([^\s]+)?(\s\d)?\s+{(.*)}$`)
func (p *PickerPlugin) parse(body string) (int, []string, error) {
subs := pickRegex.FindStringSubmatch(body)
log.Debug().Str("body", body).Strs("subs", subs).Msg("parse")
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")
}
log.Debug().
Str("body", body).
Interface("subs", subs).
Msg("subs")
delim := ","
2018-08-30 17:49:51 +00:00
if subs[1] != "" {
delim = subs[1]
}
n := 1
var err error
if num := strings.TrimSpace(subs[2]); num != "" {
n, err = strconv.Atoi(num)
2018-08-30 17:49:51 +00:00
if err != nil {
return 0, nil, err
}
2018-08-30 17:49:51 +00:00
}
items := strings.Split(subs[3], delim)
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.
2019-05-27 23:21:53 +00:00
func (p *PickerPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
p.bot.Send(c, bot.Message, message.Channel, "Choose from a list of options. Try \"pick {a,b,c}\".")
return true
2017-12-19 18:37:47 +00:00
}