Compare commits

..

No commits in common. "c7c315ad8c075749fe5697d6998bc448320bcd08" and "defeb9b3b19f73d7c09f472721a12d1a1526e02a" have entirely different histories.

3 changed files with 29 additions and 26 deletions

View File

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

View File

@ -8,29 +8,24 @@ import (
"github.com/rs/zerolog/log"
"github.com/velour/catbase/plugins/cli"
"github.com/stretchr/testify/assert"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/bot/user"
"github.com/velour/catbase/plugins/cli"
)
func makeMessage(payload string) bot.Request {
func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) {
isCmd := strings.HasPrefix(payload, "!")
if isCmd {
payload = payload[1:]
}
values := bot.ParseValues(pickRegex, payload)
return bot.Request{
Conn: &cli.CliPlugin{},
Kind: bot.Message,
Values: values,
Msg: msg.Message{
User: &user.User{Name: "tester"},
Channel: "test",
Body: payload,
Command: isCmd,
},
return &cli.CliPlugin{}, bot.Message, msg.Message{
User: &user.User{Name: "tester"},
Channel: "test",
Body: payload,
Command: isCmd,
}
}
@ -113,3 +108,10 @@ func TestPickIsCommand(t *testing.T) {
assert.NotContains(t, mb.Messages[0], "hot picks")
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,7 +3,6 @@
package reaction
import (
"regexp"
"strings"
"github.com/rs/zerolog/log"
@ -36,12 +35,12 @@ func New(b bot.Bot) *ReactionPlugin {
model: model,
br: newBayesReactor(path),
}
b.RegisterRegex(rp, bot.Message, regexp.MustCompile(`.*`), rp.message)
b.Register(rp, bot.Message, rp.message)
return rp
}
func (p *ReactionPlugin) message(r bot.Request) bool {
emojy, prob := p.br.React(r.Msg.Body)
func (p *ReactionPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
emojy, prob := p.br.React(message.Body)
target := p.config.GetFloat64("reaction.confidence", 0.5)
log.Debug().
@ -52,10 +51,10 @@ func (p *ReactionPlugin) message(r bot.Request) bool {
Msgf("Reaction check")
if prob > target {
p.bot.Send(r.Conn, bot.Reaction, r.Msg.Channel, emojy, r.Msg)
p.bot.Send(c, bot.Reaction, message.Channel, emojy, message)
}
p.checkReactions(r.Conn, r.Msg)
p.checkReactions(c, message)
return false
}