From 40d057b6a638656666a40d75caba3c724cb1c786 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Tue, 23 Mar 2021 13:40:18 -0400 Subject: [PATCH] your: refactor Ref #346 --- plugins/your/your.go | 8 +++++--- plugins/your/your_test.go | 20 +++++++++++++------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/plugins/your/your.go b/plugins/your/your.go index 10dfc2b..ab8cbd7 100644 --- a/plugins/your/your.go +++ b/plugins/your/your.go @@ -4,6 +4,7 @@ package your import ( "math/rand" + "regexp" "strings" "github.com/velour/catbase/bot" @@ -22,7 +23,7 @@ func New(b bot.Bot) *YourPlugin { bot: b, config: b.Config(), } - b.Register(yp, bot.Message, yp.message) + b.RegisterRegex(yp, bot.Message, regexp.MustCompile(`.*`), yp.message) b.Register(yp, bot.Help, yp.help) return yp } @@ -30,7 +31,8 @@ func New(b bot.Bot) *YourPlugin { // 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 *YourPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool { +func (p *YourPlugin) message(r bot.Request) bool { + message := r.Msg maxLen := p.config.GetInt("your.maxlength", 140) if len(message.Body) > maxLen { return false @@ -46,7 +48,7 @@ func (p *YourPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message } } if msg != message.Body { - p.bot.Send(c, bot.Message, message.Channel, msg) + p.bot.Send(r.Conn, bot.Message, message.Channel, msg) return true } return false diff --git a/plugins/your/your_test.go b/plugins/your/your_test.go index ea13638..93d47b8 100644 --- a/plugins/your/your_test.go +++ b/plugins/your/your_test.go @@ -3,26 +3,32 @@ package your 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, + }, + Values: bot.RegexValues{}, } }