catbase/plugins/first/first.go

270 lines
6.5 KiB
Go
Raw Normal View History

2016-01-17 18:00:44 +00:00
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
package first
import (
2016-01-15 16:54:09 +00:00
"database/sql"
"fmt"
"regexp"
"strings"
"time"
2016-03-19 18:02:46 +00:00
"github.com/jmoiron/sqlx"
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"
)
// This is a first plugin to serve as an example and quick copy/paste for new plugins.
type FirstPlugin struct {
Bot bot.Bot
db *sqlx.DB
}
type FirstEntry struct {
id int64
day time.Time
time time.Time
channel string
body string
nick string
saved bool
2016-01-15 16:54:09 +00:00
}
// Insert or update the first entry
2016-03-19 18:02:46 +00:00
func (fe *FirstEntry) save(db *sqlx.DB) error {
if _, err := db.Exec(`insert into first (day, time, channel, body, nick)
values (?, ?, ?, ?, ?)`,
2016-01-15 18:37:54 +00:00
fe.day.Unix(),
fe.time.Unix(),
fe.channel,
2016-01-15 16:54:09 +00:00
fe.body,
fe.nick,
); err != nil {
return err
}
return nil
}
// NewFirstPlugin creates a new FirstPlugin with the Plugin interface
func New(b bot.Bot) *FirstPlugin {
_, err := b.DB().Exec(`create table if not exists first (
2016-01-15 16:54:09 +00:00
id integer primary key,
2016-01-15 18:37:54 +00:00
day integer,
time integer,
channel string,
2016-01-15 16:54:09 +00:00
body string,
nick string
);`)
if err != nil {
2019-03-07 16:35:42 +00:00
log.Fatal().
Err(err).
Msg("Could not create first table")
2016-01-15 16:54:09 +00:00
}
2019-03-07 16:35:42 +00:00
log.Info().Msgf("First plugin initialized with day: %s",
midnight(time.Now()))
2016-01-15 16:54:09 +00:00
fp := &FirstPlugin{
Bot: b,
db: b.DB(),
}
b.Register(fp, bot.Message, fp.message)
b.Register(fp, bot.Help, fp.help)
return fp
}
func getLastFirst(db *sqlx.DB, channel string) (*FirstEntry, error) {
2016-01-15 16:54:09 +00:00
// Get last first entry
var id sql.NullInt64
var day sql.NullInt64
var timeEntered sql.NullInt64
var body sql.NullString
var nick sql.NullString
err := db.QueryRow(`select
id, max(day), time, body, nick from first
where channel = ?
2016-01-15 16:54:09 +00:00
limit 1;
`, channel).Scan(
2016-01-15 16:54:09 +00:00
&id,
&day,
&timeEntered,
&body,
&nick,
)
switch {
case err == sql.ErrNoRows || !id.Valid:
2019-03-07 16:35:42 +00:00
log.Info().Msg("No previous first entries")
2018-06-22 18:31:33 +00:00
return nil, nil
2016-01-15 16:54:09 +00:00
case err != nil:
2019-03-07 16:35:42 +00:00
log.Warn().Err(err).Msg("Error on first query row")
2018-06-22 18:31:33 +00:00
return nil, err
2016-01-15 16:54:09 +00:00
}
2019-03-07 16:35:42 +00:00
log.Debug().Msgf("id: %v day %v time %v body %v nick %v",
id, day, timeEntered, body, nick)
2018-06-22 18:31:33 +00:00
return &FirstEntry{
id: id.Int64,
day: time.Unix(day.Int64, 0),
time: time.Unix(timeEntered.Int64, 0),
channel: channel,
body: body.String,
nick: nick.String,
saved: true,
2018-06-22 18:31:33 +00:00
}, nil
2016-01-15 16:54:09 +00:00
}
func midnight(t time.Time) time.Time {
y, m, d := t.Date()
return time.Date(y, m, d, 0, 0, 0, 0, time.UTC)
}
func isNotToday(f *FirstEntry) bool {
if f == nil {
return true
}
t := f.time
t0 := midnight(t)
return t0.Before(midnight(time.Now()))
}
// Message responds to the bot hook on recieving messages.
// This function returns true if the plugin responds in a meaningful way to the users message.
// Otherwise, the function returns false and the bot continues execution of other plugins.
2019-05-27 23:21:53 +00:00
func (p *FirstPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
if message.IsIM {
log.Debug().Msg("Skipping IM")
return false
}
first, err := getLastFirst(p.db, message.Channel)
if err != nil {
log.Error().
Err(err).
Msg("Error getting last first")
}
log.Debug().Bool("first == nil", first == nil).Msg("Is first nil?")
log.Debug().Bool("first == nil || isNotToday()", isNotToday(first)).Msg("Is it today?")
log.Debug().Bool("p.allowed", p.allowed(message)).Msg("Allowed?")
if (first == nil || isNotToday(first)) && p.allowed(message) {
2019-03-07 16:35:42 +00:00
log.Debug().
Str("body", message.Body).
Interface("t0", first).
Time("t1", time.Now()).
Msg("Recording first")
2019-05-27 23:21:53 +00:00
p.recordFirst(c, message)
return false
}
r := strings.NewReplacer("", "", "'", "", "\"", "", ",", "", ".", "", ":", "",
"?", "", "!", "")
m := strings.ToLower(message.Body)
2019-10-26 15:26:31 +00:00
if r.Replace(m) == "whos on first the most" && first != nil {
p.leaderboard(c, message.Channel)
return true
}
if r.Replace(m) == "whos on first" && first != nil {
p.announceFirst(c, first)
2013-02-01 13:41:18 +00:00
return true
}
return false
}
func (p *FirstPlugin) allowed(message msg.Message) bool {
for _, m := range p.Bot.Config().GetArray("Bad.Msgs", []string{}) {
match, err := regexp.MatchString(m, strings.ToLower(message.Body))
if err != nil {
2019-03-07 16:35:42 +00:00
log.Error().Err(err).Msg("Bad regexp")
}
if match {
2019-03-07 16:35:42 +00:00
log.Info().
Str("user", message.User.Name).
Str("body", message.Body).
Msg("Disallowing first")
return false
}
}
2019-01-22 00:16:57 +00:00
for _, host := range p.Bot.Config().GetArray("Bad.Hosts", []string{}) {
2013-04-21 20:49:00 +00:00
if host == message.Host {
2019-03-07 16:35:42 +00:00
log.Info().
Str("user", message.User.Name).
Str("body", message.Body).
Msg("Disallowing first")
2013-04-21 20:49:00 +00:00
return false
}
}
2019-01-22 00:16:57 +00:00
for _, nick := range p.Bot.Config().GetArray("Bad.Nicks", []string{}) {
2013-04-21 20:49:00 +00:00
if nick == message.User.Name {
2019-03-07 16:35:42 +00:00
log.Info().
Str("user", message.User.Name).
Str("body", message.Body).
Msg("Disallowing first")
2013-04-21 20:49:00 +00:00
return false
}
}
return true
}
2019-05-27 23:21:53 +00:00
func (p *FirstPlugin) recordFirst(c bot.Connector, message msg.Message) {
2019-03-07 16:35:42 +00:00
log.Info().
Str("channel", message.Channel).
2019-03-07 16:35:42 +00:00
Str("user", message.User.Name).
Str("body", message.Body).
Msg("Recording first")
first := &FirstEntry{
day: midnight(time.Now()),
time: message.Time,
channel: message.Channel,
body: message.Body,
nick: message.User.Name,
2016-01-15 16:54:09 +00:00
}
log.Info().Msgf("recordFirst: %+v", first.day)
err := first.save(p.db)
2016-01-15 16:54:09 +00:00
if err != nil {
2019-03-07 16:35:42 +00:00
log.Error().Err(err).Msg("Error saving first entry")
2016-01-15 16:54:09 +00:00
return
}
p.announceFirst(c, first)
}
2019-10-26 15:26:31 +00:00
func (p *FirstPlugin) leaderboard(c bot.Connector, ch string) error {
q := `select max(channel) channel, max(nick) nick, count(id) count
from first
group by channel, nick
having channel = ?
2019-10-26 15:44:01 +00:00
order by count desc
2019-10-26 15:26:31 +00:00
limit 3`
res := []struct {
Channel string
Nick string
Count int
}{}
err := p.db.Select(&res, q, ch)
if err != nil {
return err
}
talismans := []string{":gold-trophy:", ":silver-trophy:", ":bronze-trophy:"}
msg := "First leaderboard:\n"
for i, e := range res {
msg += fmt.Sprintf("%s %d %s\n", talismans[i], e.Count, e.Nick)
}
p.Bot.Send(c, bot.Message, ch, msg)
return nil
}
func (p *FirstPlugin) announceFirst(c bot.Connector, first *FirstEntry) {
ch := first.channel
p.Bot.Send(c, bot.Message, ch, fmt.Sprintf("%s had first at %s with the message: \"%s\"",
first.nick, first.time.Format("15:04"), first.body))
}
// Help responds to help requests. Every plugin must implement a help function.
2019-05-27 23:21:53 +00:00
func (p *FirstPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
p.Bot.Send(c, bot.Message, message.Channel, "You can ask 'who's on first?' to find out.")
return true
}