From 4e0c308253d514aff85c6dac77d93598a7ce5aa8 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Sun, 7 Feb 2021 15:10:24 -0500 Subject: [PATCH] refactor: nerdepedia the lazy way --- plugins/nerdepedia/nerdepedia.go | 7 +++++-- plugins/nerdepedia/nerdepeida_test.go | 16 ++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/plugins/nerdepedia/nerdepedia.go b/plugins/nerdepedia/nerdepedia.go index d360806..1cde5bc 100644 --- a/plugins/nerdepedia/nerdepedia.go +++ b/plugins/nerdepedia/nerdepedia.go @@ -7,6 +7,7 @@ import ( "fmt" "html" "net/http" + "regexp" "strings" "github.com/velour/catbase/bot" @@ -44,7 +45,7 @@ func New(b bot.Bot) *NerdepediaPlugin { bot: b, config: b.Config(), } - b.Register(np, bot.Message, np.message) + b.RegisterRegex(np, bot.Message, regexp.MustCompile(`.*`), np.message) b.Register(np, bot.Help, np.help) return np } @@ -79,7 +80,9 @@ func defaultSites() map[string]string { // 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 *NerdepediaPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool { +func (p *NerdepediaPlugin) message(r bot.Request) bool { + c := r.Conn + message := r.Msg lowerCase := strings.ToLower(message.Body) query := "" queries := p.config.GetMap("nerdepedia.sites", defaultSites()) diff --git a/plugins/nerdepedia/nerdepeida_test.go b/plugins/nerdepedia/nerdepeida_test.go index 9e759af..05ce338 100644 --- a/plugins/nerdepedia/nerdepeida_test.go +++ b/plugins/nerdepedia/nerdepeida_test.go @@ -39,16 +39,20 @@ func (cl MockClient) Do(req *http.Request) (*http.Response, error) { }, cl.Err } -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, + }, } }