mirror of https://github.com/velour/catbase.git
refactor: nerdepedia the lazy way
This commit is contained in:
parent
7dfa5bf891
commit
4e0c308253
|
@ -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())
|
||||||
|
|
|
@ -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{
|
||||||
User: &user.User{Name: "tester"},
|
Conn: &cli.CliPlugin{},
|
||||||
Channel: "test",
|
Kind: bot.Message,
|
||||||
Body: payload,
|
Msg: msg.Message{
|
||||||
Command: isCmd,
|
User: &user.User{Name: "tester"},
|
||||||
|
Channel: "test",
|
||||||
|
Body: payload,
|
||||||
|
Command: isCmd,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue