catbase/plugins/remember/remember.go

153 lines
3.9 KiB
Go
Raw Normal View History

package remember
import (
"fmt"
"strings"
"time"
2019-03-07 16:35:42 +00:00
"github.com/rs/zerolog/log"
2016-03-19 18:02:46 +00:00
"github.com/jmoiron/sqlx"
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 {
bot bot.Bot
log map[string][]msg.Message
2016-03-19 18:02:46 +00:00
db *sqlx.DB
}
func New(b bot.Bot) *RememberPlugin {
p := &RememberPlugin{
bot: b,
log: make(map[string][]msg.Message),
db: b.DB(),
}
b.Register(p, bot.Message, p.message)
b.Register(p, bot.Help, p.help)
return p
}
2019-05-27 23:21:53 +00:00
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()
2019-05-27 23:21:53 +00:00
p.bot.Send(c, bot.Message, message.Channel, q)
// is it evil not to remember that the user said quote?
return true
}
user := message.User
parts := strings.Fields(message.Body)
if message.Command && len(parts) >= 3 &&
strings.ToLower(parts[0]) == "remember" {
// we have a remember!
2013-05-07 23:31:16 +00:00
// look through the logs and find parts[1] as a user, if not,
// fuck this hoser
2016-04-22 17:48:43 +00:00
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]
2019-03-07 16:35:42 +00:00
log.Debug().Msgf("Comparing %s:%s with %s:%s",
2016-04-22 17:48:43 +00:00
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),
) {
2019-03-07 16:35:42 +00:00
log.Debug().Msg("Found!")
2016-04-22 17:48:43 +00:00
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)
2013-05-07 23:31:16 +00:00
fact := fact.Factoid{
2016-04-22 17:48:43 +00:00
Fact: strings.ToLower(trigger),
Verb: "reply",
Tidbit: msg,
Owner: user.Name,
Created: time.Now(),
Accessed: time.Now(),
2016-04-22 17:48:43 +00:00
Count: 0,
}
if err := fact.Save(p.db); err != nil {
2019-03-07 16:35:42 +00:00
log.Error().Err(err)
2019-05-27 23:21:53 +00:00
p.bot.Send(c, bot.Message, message.Channel, "Tell somebody I'm broke.")
2013-06-01 19:00:57 +00:00
}
2013-09-01 03:05:00 +00:00
2019-03-07 16:35:42 +00:00
log.Info().
Str("msg", msg).
Msg("Remembering factoid")
2013-09-01 03:05:00 +00:00
2016-04-22 17:48:43 +00:00
// sorry, not creative with names so we're reusing msg
msg = fmt.Sprintf("Okay, %s, remembering '%s'.",
message.User.Name, msg)
2019-05-27 23:21:53 +00:00
p.bot.Send(c, bot.Message, message.Channel, msg)
2016-04-22 17:48:43 +00:00
p.recordMsg(message)
return true
2013-09-01 03:33:15 +00:00
2016-04-22 17:48:43 +00:00
}
}
2019-05-27 23:21:53 +00:00
p.bot.Send(c, bot.Message, message.Channel, "Sorry, I don't know that phrase.")
2016-04-22 17:48:43 +00:00
p.recordMsg(message)
2013-06-17 03:44:24 +00:00
return true
}
2016-04-22 17:48:43 +00:00
p.recordMsg(message)
return false
}
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
}
// deliver a random quote out of the db.
2013-05-07 23:28:34 +00:00
// 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 (p *RememberPlugin) randQuote() string {
2016-01-17 17:43:37 +00:00
var f fact.Factoid
2016-01-17 17:43:37 +00:00
var tmpCreated int64
var tmpAccessed int64
err := p.db.QueryRow(`select * from factoid where fact like '%quotes'
order by random() limit 1;`).Scan(
&f.ID,
2016-03-29 14:20:44 +00:00
&f.Fact,
&f.Tidbit,
&f.Verb,
&f.Owner,
2016-01-17 17:43:37 +00:00
&tmpCreated,
&tmpAccessed,
2016-03-29 14:20:44 +00:00
&f.Count,
2016-01-17 17:43:37 +00:00
)
if err != nil {
2019-03-07 16:35:42 +00:00
log.Error().Err(err).Msg("Error getting quotes")
2016-01-17 17:43:37 +00:00
return "I had a problem getting your quote."
}
f.Created = time.Unix(tmpCreated, 0)
f.Accessed = time.Unix(tmpAccessed, 0)
2016-03-29 14:20:44 +00:00
return f.Tidbit
}
2016-04-22 17:48:43 +00:00
func (p *RememberPlugin) recordMsg(message msg.Message) {
2019-03-07 16:35:42 +00:00
log.Debug().Msgf("Logging message: %s: %s", message.User.Name, message.Body)
p.log[message.Channel] = append(p.log[message.Channel], message)
2016-04-22 17:48:43 +00:00
}