Compare commits

..

2 Commits

Author SHA1 Message Date
Chris Sexton c7c315ad8c reaction: refactor 2021-02-19 12:23:40 -05:00
Chris Sexton 8cd79d486e picker: refactor 2021-02-19 12:14:59 -05:00
3 changed files with 26 additions and 29 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)
}

View File

@ -3,6 +3,7 @@
package reaction package reaction
import ( import (
"regexp"
"strings" "strings"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -35,12 +36,12 @@ func New(b bot.Bot) *ReactionPlugin {
model: model, model: model,
br: newBayesReactor(path), br: newBayesReactor(path),
} }
b.Register(rp, bot.Message, rp.message) b.RegisterRegex(rp, bot.Message, regexp.MustCompile(`.*`), rp.message)
return rp return rp
} }
func (p *ReactionPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool { func (p *ReactionPlugin) message(r bot.Request) bool {
emojy, prob := p.br.React(message.Body) emojy, prob := p.br.React(r.Msg.Body)
target := p.config.GetFloat64("reaction.confidence", 0.5) target := p.config.GetFloat64("reaction.confidence", 0.5)
log.Debug(). log.Debug().
@ -51,10 +52,10 @@ func (p *ReactionPlugin) message(c bot.Connector, kind bot.Kind, message msg.Mes
Msgf("Reaction check") Msgf("Reaction check")
if prob > target { if prob > target {
p.bot.Send(c, bot.Reaction, message.Channel, emojy, message) p.bot.Send(r.Conn, bot.Reaction, r.Msg.Channel, emojy, r.Msg)
} }
p.checkReactions(c, message) p.checkReactions(r.Conn, r.Msg)
return false return false
} }