csw: refactor

This commit is contained in:
Chris Sexton 2021-02-03 19:51:14 -05:00 committed by Chris Sexton
parent 9345e83adf
commit 1d3748ed06
2 changed files with 18 additions and 14 deletions

View File

@ -5,10 +5,10 @@ package couldashouldawoulda
import ( import (
"fmt" "fmt"
"math/rand" "math/rand"
"regexp"
"strings" "strings"
"github.com/velour/catbase/bot" "github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/config" "github.com/velour/catbase/config"
) )
@ -21,14 +21,12 @@ func New(b bot.Bot) *CSWPlugin {
csw := &CSWPlugin{ csw := &CSWPlugin{
Bot: b, Bot: b,
} }
b.Register(csw, bot.Message, csw.message) b.RegisterRegexCmd(csw, bot.Message, regexp.MustCompile(`.*`), csw.message)
return csw return csw
} }
func (p *CSWPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool { func (p *CSWPlugin) message(r bot.Request) bool {
if !message.Command { message := r.Msg
return false
}
lowercase := strings.ToLower(message.Body) lowercase := strings.ToLower(message.Body)
tokens := strings.Fields(lowercase) 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 return true
} }

View File

@ -3,26 +3,32 @@
package couldashouldawoulda package couldashouldawoulda
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{
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,
},
} }
} }