mirror of https://github.com/velour/catbase.git
impossible: refactor
This commit is contained in:
parent
ca97a07a4d
commit
353f289cae
|
@ -21,6 +21,7 @@ import (
|
|||
type Impossible struct {
|
||||
b bot.Bot
|
||||
c *config.Config
|
||||
handlers bot.HandlerTable
|
||||
|
||||
title string
|
||||
content []string
|
||||
|
@ -39,7 +40,7 @@ func New(b bot.Bot) *Impossible {
|
|||
}
|
||||
|
||||
b.Register(i, bot.Help, i.help)
|
||||
b.Register(i, bot.Message, i.message)
|
||||
i.register()
|
||||
|
||||
return i
|
||||
}
|
||||
|
@ -55,7 +56,7 @@ func newTesting(b bot.Bot) *Impossible {
|
|||
}
|
||||
|
||||
b.Register(i, bot.Help, i.help)
|
||||
b.Register(i, bot.Message, i.message)
|
||||
i.register()
|
||||
|
||||
return i
|
||||
}
|
||||
|
@ -65,39 +66,61 @@ func (p *Impossible) help(c bot.Connector, kind bot.Kind, message msg.Message, a
|
|||
return true
|
||||
}
|
||||
|
||||
func (p *Impossible) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
||||
messaged := false
|
||||
func (p *Impossible) tryRefresh(r bot.Request) (sent bool) {
|
||||
if p.updated.Before(time.Now()) {
|
||||
if p.title != "" {
|
||||
p.b.Send(c, bot.Message, message.Channel, fmt.Sprintf("The last impossible wikipedia article was: \"%s\"", p.title))
|
||||
messaged = true
|
||||
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("The last impossible wikipedia article was: \"%s\"", p.title))
|
||||
sent = true
|
||||
}
|
||||
for !p.refreshImpossible() {
|
||||
}
|
||||
|
||||
if p.testing {
|
||||
p.b.Send(c, bot.Message, message.Channel, p.title)
|
||||
messaged = true
|
||||
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, p.title)
|
||||
sent = true
|
||||
}
|
||||
}
|
||||
return sent
|
||||
}
|
||||
|
||||
lowercase := strings.ToLower(message.Body)
|
||||
if lowercase == "hint" || lowercase == "clue" {
|
||||
messaged = true
|
||||
p.b.Send(c, bot.Message, message.Channel, p.content[rand.Intn(len(p.content))])
|
||||
} else if strings.Contains(lowercase, strings.ToLower(p.title)) {
|
||||
messaged = true
|
||||
p.b.Send(c, bot.Message, message.Channel, fmt.Sprintf("You guessed the last impossible wikipedia article: \"%s\"", p.title))
|
||||
func (p *Impossible) register() {
|
||||
p.handlers = bot.HandlerTable{
|
||||
{Kind: bot.Message, IsCmd: false,
|
||||
Regex: regexp.MustCompile(`(?i)^hint|clue$`),
|
||||
Handler: func(r bot.Request) bool {
|
||||
if p.tryRefresh(r) {
|
||||
return true
|
||||
}
|
||||
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, p.content[rand.Intn(len(p.content))])
|
||||
return true
|
||||
}},
|
||||
{Kind: bot.Message, IsCmd: false,
|
||||
Regex: regexp.MustCompile(`(?i)^i friggin give up.?$`),
|
||||
Handler: func(r bot.Request) bool {
|
||||
if p.tryRefresh(r) {
|
||||
return true
|
||||
}
|
||||
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("You guessed the last impossible wikipedia article: \"%s\"", p.title))
|
||||
for !p.refreshImpossible() {
|
||||
}
|
||||
} else if strings.Contains(lowercase, "i friggin give up") {
|
||||
messaged = true
|
||||
p.b.Send(c, bot.Message, message.Channel, fmt.Sprintf("You're a failure the last impossible wikipedia article: \"%s\"", p.title))
|
||||
return true
|
||||
}},
|
||||
{Kind: bot.Message, IsCmd: false,
|
||||
Regex: regexp.MustCompile(`.*`),
|
||||
Handler: func(r bot.Request) bool {
|
||||
if p.tryRefresh(r) {
|
||||
return true
|
||||
}
|
||||
|
||||
if strings.Contains(strings.ToLower(r.Msg.Body), strings.ToLower(p.title)) {
|
||||
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("You guessed the last impossible wikipedia article: \"%s\"", p.title))
|
||||
for !p.refreshImpossible() {
|
||||
}
|
||||
}
|
||||
|
||||
return messaged
|
||||
return true
|
||||
}},
|
||||
}
|
||||
p.b.RegisterTable(p, p.handlers)
|
||||
}
|
||||
|
||||
func (p *Impossible) refreshImpossible() bool {
|
||||
|
|
|
@ -2,10 +2,12 @@ package impossible
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/velour/catbase/plugins/cli"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/velour/catbase/plugins/cli"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/velour/catbase/bot"
|
||||
"github.com/velour/catbase/bot/msg"
|
||||
|
@ -13,16 +15,22 @@ import (
|
|||
"github.com/velour/catbase/plugins/counter"
|
||||
)
|
||||
|
||||
func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) {
|
||||
func makeMessage(payload string, r *regexp.Regexp) bot.Request {
|
||||
isCmd := strings.HasPrefix(payload, "!")
|
||||
if isCmd {
|
||||
payload = payload[1:]
|
||||
}
|
||||
return &cli.CliPlugin{}, bot.Message, msg.Message{
|
||||
values := bot.ParseValues(r, payload)
|
||||
return bot.Request{
|
||||
Conn: &cli.CliPlugin{},
|
||||
Kind: bot.Message,
|
||||
Values: values,
|
||||
Msg: msg.Message{
|
||||
User: &user.User{Name: "tester"},
|
||||
Channel: "test",
|
||||
Body: payload,
|
||||
Command: isCmd,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -34,24 +42,32 @@ func makePlugin(t *testing.T) (*Impossible, *bot.MockBot) {
|
|||
return p, mb
|
||||
}
|
||||
|
||||
func testMessage(p *Impossible, body string) {
|
||||
for _, h := range p.handlers {
|
||||
if h.Regex.MatchString(body) && h.Handler(makeMessage(body, h.Regex)) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNothing(t *testing.T) {
|
||||
p, mb := makePlugin(t)
|
||||
p.message(makeMessage("hi"))
|
||||
p.message(makeMessage("nothing"))
|
||||
testMessage(p, "hi")
|
||||
testMessage(p, "nothing")
|
||||
assert.Len(t, mb.Messages, 1)
|
||||
}
|
||||
|
||||
func TestHint(t *testing.T) {
|
||||
p, mb := makePlugin(t)
|
||||
p.message(makeMessage("hi"))
|
||||
p.message(makeMessage("!hint"))
|
||||
testMessage(p, "hi")
|
||||
testMessage(p, "hint")
|
||||
assert.Len(t, mb.Messages, 2)
|
||||
}
|
||||
|
||||
func TestCorrect(t *testing.T) {
|
||||
p, mb := makePlugin(t)
|
||||
p.message(makeMessage("hi"))
|
||||
p.message(makeMessage(mb.Messages[0]))
|
||||
testMessage(p, "hi")
|
||||
testMessage(p, mb.Messages[0])
|
||||
|
||||
congrats := fmt.Sprintf("You guessed the last impossible wikipedia article: \"%s\"", mb.Messages[0])
|
||||
|
||||
|
|
Loading…
Reference in New Issue