From 353f289cae32138626962c57b8ff6065796ea564 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Sun, 7 Feb 2021 14:00:41 -0500 Subject: [PATCH] impossible: refactor --- plugins/impossible/impossible.go | 75 +++++++++++++++++---------- plugins/impossible/impossible_test.go | 42 ++++++++++----- 2 files changed, 78 insertions(+), 39 deletions(-) diff --git a/plugins/impossible/impossible.go b/plugins/impossible/impossible.go index 488a69b..64b2cce 100644 --- a/plugins/impossible/impossible.go +++ b/plugins/impossible/impossible.go @@ -19,8 +19,9 @@ import ( ) type Impossible struct { - b bot.Bot - c *config.Config + 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)) - 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)) - for !p.refreshImpossible() { - } +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() { + } + 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 true + }}, } - - return messaged + p.b.RegisterTable(p, p.handlers) } func (p *Impossible) refreshImpossible() bool { diff --git a/plugins/impossible/impossible_test.go b/plugins/impossible/impossible_test.go index 284a87c..b68c80a 100644 --- a/plugins/impossible/impossible_test.go +++ b/plugins/impossible/impossible_test.go @@ -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{ - User: &user.User{Name: "tester"}, - Channel: "test", - Body: payload, - Command: isCmd, + 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])