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 (
"fmt"
"regexp"
"strings"
"time"
@ -26,33 +27,30 @@ func New(b bot.Bot) *RememberPlugin {
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)
return p
}
func (p *RememberPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
if strings.ToLower(message.Body) == "quote" && message.Command {
q := p.randQuote()
p.bot.Send(c, bot.Message, message.Channel, q)
var quoteRegex = regexp.MustCompile(`(?i)^quote$`)
var rememberRegex = regexp.MustCompile(`(?i)^remember (?P<who>\S+) (?P<what>.*)$`)
// 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
}
user := message.User
parts := strings.Fields(message.Body)
func (p *RememberPlugin) rememberCmd(r bot.Request) bool {
user := r.Msg.User
if message.Command && len(parts) >= 3 &&
strings.ToLower(parts[0]) == "remember" {
// we have a remember!
// look through the logs and find parts[1] as a user, if not,
// 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]
nick := r.Values["who"]
snip := r.Values["what"]
for i := len(p.log[r.Msg.Channel]) - 1; i >= 0; i-- {
entry := p.log[r.Msg.Channel][i]
log.Debug().Msgf("Comparing %s:%s with %s:%s",
entry.User.Name, entry.Body, nick, snip)
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 {
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().
@ -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
msg = fmt.Sprintf("Okay, %s, remembering '%s'.",
message.User.Name, msg)
p.bot.Send(c, bot.Message, message.Channel, msg)
p.recordMsg(message)
r.Msg.User.Name, msg)
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
return true
}
}
p.bot.Send(c, bot.Message, message.Channel, "Sorry, I don't know that phrase.")
p.recordMsg(message)
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "Sorry, I don't know that phrase.")
return true
}
p.recordMsg(message)
return false
}
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 " +
"!remember <nick> <snippet> to remember what they said. Snippet can " +
@ -146,7 +138,8 @@ func (p *RememberPlugin) randQuote() string {
return f.Tidbit
}
func (p *RememberPlugin) recordMsg(message msg.Message) {
log.Debug().Msgf("Logging message: %s: %s", message.User.Name, message.Body)
p.log[message.Channel] = append(p.log[message.Channel], message)
func (p *RememberPlugin) recordMsg(r bot.Request) bool {
log.Debug().Msgf("Logging message: %s: %s", r.Msg.User.Name, r.Msg.Body)
p.log[r.Msg.Channel] = append(p.log[r.Msg.Channel], r.Msg)
return false
}

View File

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