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"
|
|
|
|
|
2019-10-08 22:15:07 +00:00
|
|
|
"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
|
2019-02-05 19:41:38 +00:00
|
|
|
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)
|
2019-02-05 19:41:38 +00:00
|
|
|
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())
|
2018-08-30 20:11:32 +00:00
|
|
|
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
|
|
|
|
2019-11-22 16:52:20 +00:00
|
|
|
var pickRegex = regexp.MustCompile(`^pick([^\s]+)?(\s\d)?\s+{(.*)}$`)
|
2018-08-28 19:03:54 +00:00
|
|
|
|
2018-08-30 20:05:03 +00:00
|
|
|
func (p *PickerPlugin) parse(body string) (int, []string, error) {
|
2019-11-22 16:52:20 +00:00
|
|
|
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")
|
|
|
|
}
|
2018-08-28 19:03:54 +00:00
|
|
|
|
2019-10-08 22:15:07 +00:00
|
|
|
log.Debug().
|
|
|
|
Str("body", body).
|
|
|
|
Interface("subs", subs).
|
|
|
|
Msg("subs")
|
|
|
|
|
|
|
|
delim := ","
|
2018-08-30 17:49:51 +00:00
|
|
|
if subs[1] != "" {
|
2019-10-08 22:15:07 +00:00
|
|
|
delim = subs[1]
|
|
|
|
}
|
|
|
|
|
2019-11-22 16:52:20 +00:00
|
|
|
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-28 19:03:54 +00:00
|
|
|
}
|
2018-08-30 17:49:51 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 16:52:20 +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.
|
2022-03-22 01:32:44 +00:00
|
|
|
func (p *PickerPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...any) bool {
|
2019-05-27 23:21:53 +00:00
|
|
|
p.bot.Send(c, bot.Message, message.Channel, "Choose from a list of options. Try \"pick {a,b,c}\".")
|
2019-02-05 19:41:38 +00:00
|
|
|
return true
|
2017-12-19 18:37:47 +00:00
|
|
|
}
|