From 1d3748ed0690d42d4c2055343a809f220a43532c Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Wed, 3 Feb 2021 19:51:14 -0500 Subject: [PATCH] csw: refactor --- plugins/couldashouldawoulda/csw.go | 12 +++++------- plugins/couldashouldawoulda/csw_test.go | 20 +++++++++++++------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/plugins/couldashouldawoulda/csw.go b/plugins/couldashouldawoulda/csw.go index c424fba..e38caee 100644 --- a/plugins/couldashouldawoulda/csw.go +++ b/plugins/couldashouldawoulda/csw.go @@ -5,10 +5,10 @@ package couldashouldawoulda import ( "fmt" "math/rand" + "regexp" "strings" "github.com/velour/catbase/bot" - "github.com/velour/catbase/bot/msg" "github.com/velour/catbase/config" ) @@ -21,14 +21,12 @@ func New(b bot.Bot) *CSWPlugin { csw := &CSWPlugin{ Bot: b, } - b.Register(csw, bot.Message, csw.message) + b.RegisterRegexCmd(csw, bot.Message, regexp.MustCompile(`.*`), csw.message) return csw } -func (p *CSWPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool { - if !message.Command { - return false - } +func (p *CSWPlugin) message(r bot.Request) bool { + message := r.Msg lowercase := strings.ToLower(message.Body) tokens := strings.Fields(lowercase) @@ -65,7 +63,7 @@ func (p *CSWPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, } } - p.Bot.Send(c, bot.Message, message.Channel, responses[rand.Intn(len(responses))]) + p.Bot.Send(r.Conn, bot.Message, message.Channel, responses[rand.Intn(len(responses))]) return true } diff --git a/plugins/couldashouldawoulda/csw_test.go b/plugins/couldashouldawoulda/csw_test.go index e647ede..6a75f46 100644 --- a/plugins/couldashouldawoulda/csw_test.go +++ b/plugins/couldashouldawoulda/csw_test.go @@ -3,26 +3,32 @@ package couldashouldawoulda import ( - "github.com/velour/catbase/plugins/cli" "strings" "testing" + "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" ) -func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) { +func makeMessage(payload string) bot.Request { isCmd := strings.HasPrefix(payload, "!") if isCmd { payload = payload[1:] } - return &cli.CliPlugin{}, bot.Message, msg.Message{ - User: &user.User{Name: "tester"}, - Channel: "test", - Body: payload, - Command: isCmd, + return bot.Request{ + Conn: &cli.CliPlugin{}, + Kind: bot.Message, + Msg: msg.Message{ + User: &user.User{Name: "tester"}, + Channel: "test", + Body: payload, + Command: isCmd, + }, } }