refactor: nerdepedia the lazy way

This commit is contained in:
Chris Sexton 2021-02-07 15:10:24 -05:00 committed by Chris Sexton
parent 7dfa5bf891
commit 4e0c308253
2 changed files with 15 additions and 8 deletions

View File

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"html" "html"
"net/http" "net/http"
"regexp"
"strings" "strings"
"github.com/velour/catbase/bot" "github.com/velour/catbase/bot"
@ -44,7 +45,7 @@ func New(b bot.Bot) *NerdepediaPlugin {
bot: b, bot: b,
config: b.Config(), 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) b.Register(np, bot.Help, np.help)
return np return np
} }
@ -79,7 +80,9 @@ func defaultSites() map[string]string {
// 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 *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) lowerCase := strings.ToLower(message.Body)
query := "" query := ""
queries := p.config.GetMap("nerdepedia.sites", defaultSites()) queries := p.config.GetMap("nerdepedia.sites", defaultSites())

View File

@ -39,16 +39,20 @@ func (cl MockClient) Do(req *http.Request) (*http.Response, error) {
}, cl.Err }, cl.Err
} }
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,
},
} }
} }