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,85 +27,76 @@ 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 {
return true q := p.randQuote()
} p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, q)
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 log.Debug().Msgf("Comparing %s:%s with %s:%s",
nick := parts[1] entry.User.Name, entry.Body, nick, snip)
snip := strings.Join(parts[2:], " ") if strings.ToLower(entry.User.Name) == strings.ToLower(nick) &&
for i := len(p.log[message.Channel]) - 1; i >= 0; i-- { strings.Contains(
entry := p.log[message.Channel][i] strings.ToLower(entry.Body),
log.Debug().Msgf("Comparing %s:%s with %s:%s", strings.ToLower(snip),
entry.User.Name, entry.Body, nick, snip) ) {
if strings.ToLower(entry.User.Name) == strings.ToLower(nick) && log.Debug().Msg("Found!")
strings.Contains(
strings.ToLower(entry.Body),
strings.ToLower(snip),
) {
log.Debug().Msg("Found!")
var msg string
if entry.Action {
msg = fmt.Sprintf("*%s* %s", entry.User.Name, entry.Body)
} else {
msg = fmt.Sprintf("<%s> %s", entry.User.Name, entry.Body)
}
trigger := fmt.Sprintf("%s quotes", entry.User.Name)
fact := fact.Factoid{
Fact: strings.ToLower(trigger),
Verb: "reply",
Tidbit: msg,
Owner: user.Name,
Created: time.Now(),
Accessed: time.Now(),
Count: 0,
}
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.")
}
log.Info().
Str("msg", msg).
Msg("Remembering factoid")
// 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)
return true
var msg string
if entry.Action {
msg = fmt.Sprintf("*%s* %s", entry.User.Name, entry.Body)
} else {
msg = fmt.Sprintf("<%s> %s", entry.User.Name, entry.Body)
} }
}
p.bot.Send(c, bot.Message, message.Channel, "Sorry, I don't know that phrase.")
p.recordMsg(message)
return true
}
p.recordMsg(message) trigger := fmt.Sprintf("%s quotes", entry.User.Name)
return false
fact := fact.Factoid{
Fact: strings.ToLower(trigger),
Verb: "reply",
Tidbit: msg,
Owner: user.Name,
Created: time.Now(),
Accessed: time.Now(),
Count: 0,
}
if err := fact.Save(p.db); err != nil {
log.Error().Err(err)
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "Tell somebody I'm broke.")
}
log.Info().
Str("msg", msg).
Msg("Remembering factoid")
// sorry, not creative with names so we're reusing msg
msg = fmt.Sprintf("Okay, %s, remembering '%s'.",
r.Msg.User.Name, msg)
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
return true
}
}
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "Sorry, I don't know that phrase.")
return true
} }
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 {
@ -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{
User: &user.User{Name: nick}, Conn: &cli.CliPlugin{},
Channel: "test", Kind: bot.Message,
Body: payload, Values: bot.ParseValues(r, payload),
Command: isCmd, 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 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")