remember: refactor

This commit is contained in:
Chris Sexton 2021-02-19 13:23:29 -05:00 committed by Chris Sexton
parent c7c315ad8c
commit fe5e9a6226
2 changed files with 86 additions and 83 deletions

View File

@ -2,6 +2,7 @@ package remember
import ( import (
"fmt" "fmt"
"regexp"
"strings" "strings"
"time" "time"
@ -26,33 +27,30 @@ func New(b bot.Bot) *RememberPlugin {
db: b.DB(), db: b.DB(),
} }
b.Register(p, bot.Message, p.message) b.RegisterRegex(p, bot.Message, rememberRegex, p.rememberCmd)
b.RegisterRegex(p, bot.Message, quoteRegex, p.quoteCmd)
b.RegisterRegex(p, bot.Message, regexp.MustCompile(`.*`), p.recordMsg)
b.Register(p, bot.Help, p.help) b.Register(p, bot.Help, p.help)
return p return p
} }
func (p *RememberPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool { var quoteRegex = regexp.MustCompile(`(?i)^quote$`)
if strings.ToLower(message.Body) == "quote" && message.Command { var rememberRegex = regexp.MustCompile(`(?i)^remember (?P<who>\S+) (?P<what>.*)$`)
q := p.randQuote()
p.bot.Send(c, bot.Message, message.Channel, q)
// is it evil not to remember that the user said quote? func (p *RememberPlugin) quoteCmd(r bot.Request) bool {
q := p.randQuote()
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, q)
return true return true
} }
user := message.User func (p *RememberPlugin) rememberCmd(r bot.Request) bool {
parts := strings.Fields(message.Body) user := r.Msg.User
if message.Command && len(parts) >= 3 && nick := r.Values["who"]
strings.ToLower(parts[0]) == "remember" { snip := r.Values["what"]
// we have a remember! for i := len(p.log[r.Msg.Channel]) - 1; i >= 0; i-- {
// look through the logs and find parts[1] as a user, if not, entry := p.log[r.Msg.Channel][i]
// fuck this hoser
nick := parts[1]
snip := strings.Join(parts[2:], " ")
for i := len(p.log[message.Channel]) - 1; i >= 0; i-- {
entry := p.log[message.Channel][i]
log.Debug().Msgf("Comparing %s:%s with %s:%s", log.Debug().Msgf("Comparing %s:%s with %s:%s",
entry.User.Name, entry.Body, nick, snip) entry.User.Name, entry.Body, nick, snip)
if strings.ToLower(entry.User.Name) == strings.ToLower(nick) && if strings.ToLower(entry.User.Name) == strings.ToLower(nick) &&
@ -82,7 +80,7 @@ func (p *RememberPlugin) message(c bot.Connector, kind bot.Kind, message msg.Mes
} }
if err := fact.Save(p.db); err != nil { if err := fact.Save(p.db); err != nil {
log.Error().Err(err) log.Error().Err(err)
p.bot.Send(c, bot.Message, message.Channel, "Tell somebody I'm broke.") p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "Tell somebody I'm broke.")
} }
log.Info(). log.Info().
@ -91,22 +89,16 @@ func (p *RememberPlugin) message(c bot.Connector, kind bot.Kind, message msg.Mes
// sorry, not creative with names so we're reusing msg // sorry, not creative with names so we're reusing msg
msg = fmt.Sprintf("Okay, %s, remembering '%s'.", msg = fmt.Sprintf("Okay, %s, remembering '%s'.",
message.User.Name, msg) r.Msg.User.Name, msg)
p.bot.Send(c, bot.Message, message.Channel, msg) p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
p.recordMsg(message)
return true return true
} }
} }
p.bot.Send(c, bot.Message, message.Channel, "Sorry, I don't know that phrase.") p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "Sorry, I don't know that phrase.")
p.recordMsg(message)
return true return true
} }
p.recordMsg(message)
return false
}
func (p *RememberPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool { func (p *RememberPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
msg := "remember will let you quote your idiot friends. Just type " + msg := "remember will let you quote your idiot friends. Just type " +
"!remember <nick> <snippet> to remember what they said. Snippet can " + "!remember <nick> <snippet> to remember what they said. Snippet can " +
@ -146,7 +138,8 @@ func (p *RememberPlugin) randQuote() string {
return f.Tidbit return f.Tidbit
} }
func (p *RememberPlugin) recordMsg(message msg.Message) { func (p *RememberPlugin) recordMsg(r bot.Request) bool {
log.Debug().Msgf("Logging message: %s: %s", message.User.Name, message.Body) log.Debug().Msgf("Logging message: %s: %s", r.Msg.User.Name, r.Msg.Body)
p.log[message.Channel] = append(p.log[message.Channel], message) p.log[r.Msg.Channel] = append(p.log[r.Msg.Channel], r.Msg)
return false
} }

View File

@ -1,10 +1,12 @@
package remember package remember
import ( import (
"github.com/velour/catbase/plugins/cli" "regexp"
"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"
@ -12,16 +14,21 @@ import (
"github.com/velour/catbase/plugins/fact" "github.com/velour/catbase/plugins/fact"
) )
func makeMessage(nick, payload string) msg.Message { func makeMessage(nick, payload string, r *regexp.Regexp) bot.Request {
isCmd := strings.HasPrefix(payload, "!") isCmd := strings.HasPrefix(payload, "!")
if isCmd { if isCmd {
payload = payload[1:] payload = payload[1:]
} }
return msg.Message{ return bot.Request{
Conn: &cli.CliPlugin{},
Kind: bot.Message,
Values: bot.ParseValues(r, payload),
Msg: msg.Message{
User: &user.User{Name: nick}, User: &user.User{Name: nick},
Channel: "test", Channel: "test",
Body: payload, Body: payload,
Command: isCmd, Command: isCmd,
},
} }
} }
@ -33,19 +40,22 @@ func makePlugin(t *testing.T) (*RememberPlugin, *fact.FactoidPlugin, *bot.MockBo
return p, f, mb return p, f, mb
} }
var allMsg = regexp.MustCompile(`.*`)
// Test case // Test case
func TestCornerCaseBug(t *testing.T) { func TestCornerCaseBug(t *testing.T) {
msgs := []msg.Message{ msgs := []bot.Request{
makeMessage("user1", "I dont want to personally touch a horse dick."), makeMessage("user1", "I dont want to personally touch a horse dick.", allMsg),
makeMessage("user3", "idk my bff rose?"), makeMessage("user3", "idk my bff rose?", allMsg),
makeMessage("user2", "!remember user1 touch"),
} }
rememberMsg := makeMessage("user2", "!remember user1 touch", rememberRegex)
p, _, mb := makePlugin(t) p, _, mb := makePlugin(t)
for _, m := range msgs { for _, m := range msgs {
p.message(&cli.CliPlugin{}, bot.Message, m) p.recordMsg(m)
} }
p.rememberCmd(rememberMsg)
assert.Len(t, mb.Messages, 1) assert.Len(t, mb.Messages, 1)
assert.Contains(t, mb.Messages[0], "horse dick") assert.Contains(t, mb.Messages[0], "horse dick")
q, err := fact.GetSingleFact(mb.DB(), "user1 quotes") q, err := fact.GetSingleFact(mb.DB(), "user1 quotes")