catbase/plugins/remember/remember.go

147 lines
4.0 KiB
Go
Raw Normal View History

package remember
import (
"fmt"
2021-12-20 17:40:10 +00:00
bh "github.com/timshannon/bolthold"
"math/rand"
2021-02-19 18:23:29 +00:00
"regexp"
"strings"
"time"
2019-03-07 16:35:42 +00:00
"github.com/rs/zerolog/log"
2016-01-17 18:00:44 +00:00
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/plugins/fact"
)
type RememberPlugin struct {
2021-12-20 17:40:10 +00:00
bot bot.Bot
log map[string][]msg.Message
store *bh.Store
}
func New(b bot.Bot) *RememberPlugin {
p := &RememberPlugin{
2021-12-20 17:40:10 +00:00
bot: b,
log: make(map[string][]msg.Message),
store: b.Store(),
}
2021-02-19 18:23:29 +00:00
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
}
2021-02-19 18:23:29 +00:00
var quoteRegex = regexp.MustCompile(`(?i)^quote$`)
var rememberRegex = regexp.MustCompile(`(?i)^remember (?P<who>\S+) (?P<what>.*)$`)
2021-02-19 18:23:29 +00:00
func (p *RememberPlugin) quoteCmd(r bot.Request) bool {
q := p.randQuote()
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, q)
return true
}
2021-02-19 18:23:29 +00:00
func (p *RememberPlugin) rememberCmd(r bot.Request) bool {
user := r.Msg.User
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) &&
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)
}
2013-09-01 03:33:15 +00:00
2021-02-19 18:23:29 +00:00
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,
}
2021-12-20 17:40:10 +00:00
if err := fact.Save(p.store); err != nil {
2021-02-19 18:23:29 +00:00
log.Error().Err(err)
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "Tell somebody I'm broke.")
2016-04-22 17:48:43 +00:00
}
2021-02-19 18:23:29 +00:00
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
}
}
2021-02-19 18:23:29 +00:00
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "Sorry, I don't know that phrase.")
return true
}
2019-05-27 23:21:53 +00:00
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 " +
2013-05-07 23:31:16 +00:00
"!remember <nick> <snippet> to remember what they said. Snippet can " +
"be any part of their message. Later on, you can ask for a random " +
"!quote."
2013-05-07 23:28:34 +00:00
2019-05-27 23:21:53 +00:00
p.bot.Send(c, bot.Message, message.Channel, msg)
return true
}
func (p *RememberPlugin) randQuote() string {
2021-12-20 17:40:10 +00:00
return RandQuote(p.store)
}
2016-01-17 17:43:37 +00:00
2021-12-20 17:40:10 +00:00
// AllQuotesFrom delivers quotes out of the db from who.
func AllQuotesFrom(store *bh.Store, who string) []fact.Factoid {
allQuotes := []fact.Factoid{}
2021-12-21 04:31:19 +00:00
err := store.Find(&allQuotes, bh.Where("Fact").Eq(who+" quotes"))
if err != nil {
2019-03-07 16:35:42 +00:00
log.Error().Err(err).Msg("Error getting quotes")
2021-12-20 17:40:10 +00:00
return []fact.Factoid{}
}
2021-12-20 17:40:10 +00:00
return allQuotes
}
2021-12-20 17:40:10 +00:00
// RandQuote delivers a random quote out of the db.
// Note: this is the same cache for all channels joined. This plugin needs to be
// expanded to have this function execute a quote for a particular channel
func RandQuote(store *bh.Store) string {
allQuotes := []fact.Factoid{}
2021-12-21 04:31:19 +00:00
err := store.Find(&allQuotes, bh.Where("Fact").RegExp(regexp.MustCompile(`.+ quotes$`)))
2021-12-20 17:40:10 +00:00
if err != nil {
log.Error().Err(err).Msg("Error getting quotes")
return "I had a problem getting your quote."
}
f := allQuotes[rand.Intn(len(allQuotes))]
2016-03-29 14:20:44 +00:00
return f.Tidbit
}
2021-02-19 18:23:29 +00:00
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
2016-04-22 17:48:43 +00:00
}