picker: refactor

This commit is contained in:
Chris Sexton 2021-02-19 12:12:02 -05:00 committed by Chris Sexton
parent defeb9b3b1
commit 8cd79d486e
2 changed files with 20 additions and 24 deletions

View File

@ -26,7 +26,7 @@ func New(b bot.Bot) *PickerPlugin {
pp := &PickerPlugin{ pp := &PickerPlugin{
bot: b, bot: b,
} }
b.Register(pp, bot.Message, pp.message) b.RegisterRegex(pp, bot.Message, pickRegex, pp.message)
b.Register(pp, bot.Help, pp.help) b.Register(pp, bot.Help, pp.help)
return pp return pp
} }
@ -34,14 +34,12 @@ func New(b bot.Bot) *PickerPlugin {
// Message responds to the bot hook on recieving messages. // 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. // 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. // Otherwise, the function returns false and the bot continues execution of other plugins.
func (p *PickerPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool { func (p *PickerPlugin) message(r bot.Request) bool {
if !strings.HasPrefix(message.Body, "pick") || !message.Command { message := r.Msg
return false
}
n, items, err := p.parse(message.Body) n, items, err := p.parse(message.Body)
if err != nil { if err != nil {
p.bot.Send(c, bot.Message, message.Channel, err.Error()) p.bot.Send(r.Conn, bot.Message, message.Channel, err.Error())
return true return true
} }
@ -50,7 +48,7 @@ func (p *PickerPlugin) message(c bot.Connector, kind bot.Kind, message msg.Messa
for _, pref := range preferences { for _, pref := range preferences {
if pref == it { if pref == it {
out := fmt.Sprintf("I've chosen %q for you.", strings.TrimSpace(it)) out := fmt.Sprintf("I've chosen %q for you.", strings.TrimSpace(it))
p.bot.Send(c, bot.Message, message.Channel, out) p.bot.Send(r.Conn, bot.Message, message.Channel, out)
return true return true
} }
} }
@ -59,7 +57,7 @@ func (p *PickerPlugin) message(c bot.Connector, kind bot.Kind, message msg.Messa
if n == 1 { if n == 1 {
item := items[rand.Intn(len(items))] item := items[rand.Intn(len(items))]
out := fmt.Sprintf("I've chosen %q for you.", strings.TrimSpace(item)) out := fmt.Sprintf("I've chosen %q for you.", strings.TrimSpace(item))
p.bot.Send(c, bot.Message, message.Channel, out) p.bot.Send(r.Conn, bot.Message, message.Channel, out)
return true return true
} }
@ -75,7 +73,7 @@ func (p *PickerPlugin) message(c bot.Connector, kind bot.Kind, message msg.Messa
fmt.Fprintf(&b, ", %q", item) fmt.Fprintf(&b, ", %q", item)
} }
b.WriteString(" }") b.WriteString(" }")
p.bot.Send(c, bot.Message, message.Channel, b.String()) p.bot.Send(r.Conn, bot.Message, message.Channel, b.String())
return true return true
} }

View File

@ -8,24 +8,29 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/velour/catbase/plugins/cli"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/velour/catbase/bot" "github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg" "github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/bot/user" "github.com/velour/catbase/bot/user"
"github.com/velour/catbase/plugins/cli"
) )
func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) { func makeMessage(payload string) bot.Request {
isCmd := strings.HasPrefix(payload, "!") isCmd := strings.HasPrefix(payload, "!")
if isCmd { if isCmd {
payload = payload[1:] payload = payload[1:]
} }
return &cli.CliPlugin{}, bot.Message, msg.Message{ values := bot.ParseValues(pickRegex, payload)
User: &user.User{Name: "tester"}, return bot.Request{
Channel: "test", Conn: &cli.CliPlugin{},
Body: payload, Kind: bot.Message,
Command: isCmd, Values: values,
Msg: msg.Message{
User: &user.User{Name: "tester"},
Channel: "test",
Body: payload,
Command: isCmd,
},
} }
} }
@ -108,10 +113,3 @@ func TestPickIsCommand(t *testing.T) {
assert.NotContains(t, mb.Messages[0], "hot picks") assert.NotContains(t, mb.Messages[0], "hot picks")
log.Debug().Str("resp", mb.Messages[0]).Msg("choose") log.Debug().Str("resp", mb.Messages[0]).Msg("choose")
} }
func TestPickMustBeCommand(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
_ = c.message(makeMessage("pick⌘ { bagel/egg/smoked turkey/butte/cheese ⌘ fuck all that, just have a bagel and cream cheese }"))
assert.Len(t, mb.Messages, 0)
}