your: refactor

Ref #346
This commit is contained in:
Chris Sexton 2021-03-23 13:40:18 -04:00 committed by Chris Sexton
parent 67c88190ab
commit 40d057b6a6
2 changed files with 18 additions and 10 deletions

View File

@ -4,6 +4,7 @@ package your
import ( import (
"math/rand" "math/rand"
"regexp"
"strings" "strings"
"github.com/velour/catbase/bot" "github.com/velour/catbase/bot"
@ -22,7 +23,7 @@ func New(b bot.Bot) *YourPlugin {
bot: b, bot: b,
config: b.Config(), 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) b.Register(yp, bot.Help, yp.help)
return yp return yp
} }
@ -30,7 +31,8 @@ func New(b bot.Bot) *YourPlugin {
// 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 *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) maxLen := p.config.GetInt("your.maxlength", 140)
if len(message.Body) > maxLen { if len(message.Body) > maxLen {
return false return false
@ -46,7 +48,7 @@ func (p *YourPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message
} }
} }
if msg != message.Body { 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 true
} }
return false return false

View File

@ -3,26 +3,32 @@
package your package your
import ( import (
"github.com/velour/catbase/plugins/cli"
"strings" "strings"
"testing" "testing"
"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"
) )
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{ return bot.Request{
Conn: &cli.CliPlugin{},
Kind: bot.Message,
Msg: msg.Message{
User: &user.User{Name: "tester"}, User: &user.User{Name: "tester"},
Channel: "test", Channel: "test",
Body: payload, Body: payload,
Command: isCmd, Command: isCmd,
},
Values: bot.RegexValues{},
} }
} }