mirror of https://github.com/velour/catbase.git
csw: refactor
This commit is contained in:
parent
9345e83adf
commit
1d3748ed06
|
@ -5,10 +5,10 @@ package couldashouldawoulda
|
|||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/velour/catbase/bot"
|
||||
"github.com/velour/catbase/bot/msg"
|
||||
"github.com/velour/catbase/config"
|
||||
)
|
||||
|
||||
|
@ -21,14 +21,12 @@ func New(b bot.Bot) *CSWPlugin {
|
|||
csw := &CSWPlugin{
|
||||
Bot: b,
|
||||
}
|
||||
b.Register(csw, bot.Message, csw.message)
|
||||
b.RegisterRegexCmd(csw, bot.Message, regexp.MustCompile(`.*`), csw.message)
|
||||
return csw
|
||||
}
|
||||
|
||||
func (p *CSWPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
||||
if !message.Command {
|
||||
return false
|
||||
}
|
||||
func (p *CSWPlugin) message(r bot.Request) bool {
|
||||
message := r.Msg
|
||||
|
||||
lowercase := strings.ToLower(message.Body)
|
||||
tokens := strings.Fields(lowercase)
|
||||
|
@ -65,7 +63,7 @@ func (p *CSWPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message,
|
|||
}
|
||||
}
|
||||
|
||||
p.Bot.Send(c, bot.Message, message.Channel, responses[rand.Intn(len(responses))])
|
||||
p.Bot.Send(r.Conn, bot.Message, message.Channel, responses[rand.Intn(len(responses))])
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
|
@ -3,26 +3,32 @@
|
|||
package couldashouldawoulda
|
||||
|
||||
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,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue