catbase/plugins/achievements/achievements.go

232 lines
6.6 KiB
Go
Raw Normal View History

2020-04-20 09:56:49 +00:00
package achievements
import (
"fmt"
2021-12-20 17:40:10 +00:00
bh "github.com/timshannon/bolthold"
2020-04-24 20:24:29 +00:00
"regexp"
"strings"
"time"
2020-04-20 09:56:49 +00:00
2020-04-24 20:24:29 +00:00
"github.com/rs/zerolog/log"
2020-04-20 09:56:49 +00:00
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/config"
)
// A plugin to track our misdeeds
type AchievementsPlugin struct {
2021-12-20 17:40:10 +00:00
bot bot.Bot
cfg *config.Config
store *bh.Store
2020-04-20 09:56:49 +00:00
}
func New(b bot.Bot) *AchievementsPlugin {
2020-04-24 20:24:29 +00:00
ap := &AchievementsPlugin{
2021-12-20 17:40:10 +00:00
bot: b,
cfg: b.Config(),
store: b.Store(),
2020-04-20 09:56:49 +00:00
}
2021-02-01 03:14:55 +00:00
2021-06-17 20:21:30 +00:00
ap.register()
2021-02-01 03:14:55 +00:00
2020-04-24 20:24:29 +00:00
return ap
2020-04-20 09:56:49 +00:00
}
2021-06-17 20:21:30 +00:00
var grantRegex = regexp.MustCompile(`(?i)grant (?P<emojy>(?::[[:word:][:punct:]]+:\s?)+) to :?(?P<nick>[[:word:]]+):?`)
var createRegex = regexp.MustCompile(`(?i)create trophy (?P<emojy>(?::[[:word:][:punct:]]+:\s?)+) (?P<description>.+)`)
var greatRegex = regexp.MustCompile(`(?i)how great (?:am i|is :?(?P<who>[[:word:]]+))[[:punct:]]*`)
var listRegex = regexp.MustCompile(`(?i)^list (?P<whos>.+)?\s?trophies$`)
func (p *AchievementsPlugin) register() {
p.bot.RegisterRegexCmd(p, bot.Message, grantRegex, p.grantCmd)
p.bot.RegisterRegexCmd(p, bot.Message, createRegex, p.createCmd)
p.bot.RegisterRegexCmd(p, bot.Message, greatRegex, p.greatCmd)
p.bot.RegisterRegexCmd(p, bot.Message, listRegex, p.listCmd)
p.bot.Register(p, bot.Help, p.help)
}
2020-04-24 20:24:29 +00:00
func (p *AchievementsPlugin) GetAwards(nick string) []Award {
var awards []Award
2021-12-20 17:40:10 +00:00
err := p.store.Find(&awards, bh.Where("holder").Eq(nick))
if err != nil {
2020-04-24 20:24:29 +00:00
log.Error().Err(err).Msg("could not select awards")
}
return awards
}
2021-06-17 20:21:30 +00:00
func (p *AchievementsPlugin) listCmd(r bot.Request) bool {
log.Debug().Msgf("Values: %+v", r.Values)
whos := strings.TrimSpace(r.Values["whos"])
if whos == "my" {
whos = r.Msg.User.Name
}
log.Debug().Msgf("whos = %s", whos)
ts, err := p.AllTrophies()
if err != nil {
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "Some problem happened.")
log.Error().Err(err).Msg("could not retrieve trophies")
return true
}
if len(ts) == 0 {
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "There are no trophies.")
return true
}
msg := "Current trophies:\n"
log.Debug().Msgf("Trophies: %s", ts)
for _, t := range ts {
log.Debug().Msgf("t.Creator = %s", t.Creator)
if strings.TrimSpace(t.Creator) == whos {
log.Debug().Msgf("adding %s", t)
msg += fmt.Sprintf("%s - %s\n", t.Emojy, t.Description)
} else if whos == "" {
msg += fmt.Sprintf("%s - %s (by %s)\n", t.Emojy, t.Description, t.Creator)
}
}
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, strings.TrimSpace(msg))
return true
}
2020-04-24 20:24:29 +00:00
2021-02-01 03:14:55 +00:00
func (p *AchievementsPlugin) grantCmd(r bot.Request) bool {
nick := r.Msg.User.Name
emojy := r.Values["emojy"]
receiver := r.Values["nick"]
trophy, err := p.FindTrophy(emojy)
if err != nil {
log.Error().Err(err).Msg("could not find trophy")
msg := fmt.Sprintf("The %s award doesn't exist.", emojy)
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
2020-04-24 20:24:29 +00:00
return true
}
2021-02-01 03:14:55 +00:00
if nick == trophy.Creator {
a, err := p.Grant(receiver, emojy)
2020-04-24 20:24:29 +00:00
if err != nil {
2021-02-01 03:14:55 +00:00
log.Error().Err(err).Msg("could not award trophy")
2020-04-24 20:24:29 +00:00
}
2021-02-01 03:14:55 +00:00
msg := fmt.Sprintf("Congrats %s. You just got the %s award for %s.",
receiver, emojy, a.Description)
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
} else {
msg := fmt.Sprintf("Sorry, %s. %s owns that trophy.", nick, trophy.Creator)
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
2020-04-24 20:24:29 +00:00
}
2021-02-01 03:14:55 +00:00
return true
}
func (p *AchievementsPlugin) createCmd(r bot.Request) bool {
nick := r.Msg.User.Name
emojy := r.Values["emojy"]
description := r.Values["description"]
t, err := p.Create(emojy, description, nick)
if err != nil {
log.Error().Err(err).Msg("could not create trophy")
if strings.Contains(err.Error(), "exists") {
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, err.Error())
2020-04-24 20:24:29 +00:00
return true
}
2021-02-01 03:14:55 +00:00
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "I'm too humble to ever award that trophy")
2020-04-24 20:24:29 +00:00
return true
}
2021-02-01 03:14:55 +00:00
resp := fmt.Sprintf("Okay %s. I have crafted a one-of-a-kind %s trophy to give for %s",
nick, t.Emojy, t.Description)
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, resp)
return true
}
func (p *AchievementsPlugin) greatCmd(r bot.Request) bool {
nick := r.Msg.User.Name
who := r.Values["who"]
if who == "" {
who = nick
}
awards := p.GetAwards(who)
if len(awards) == 0 {
m := fmt.Sprintf("%s has no achievements to their name. They really suck.", who)
if who == nick {
m = fmt.Sprintf("You have no achievements to your name. "+
"You are a sad and terrible specimen of the human condition, %s.", who)
}
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, m)
} else {
m := fmt.Sprintf("Wow, let's all clap for %s. Look at these awards:", who)
for _, a := range awards {
m += fmt.Sprintf("\n%s - %s", a.Emojy, a.Description)
}
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, m)
}
return true
2020-04-20 09:56:49 +00:00
}
func (p *AchievementsPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
ch := message.Channel
me := p.bot.WhoAmI()
2020-04-24 20:24:29 +00:00
msg := "The achievements plugins awards trophies."
msg += fmt.Sprintf("\nYou can create a trophy with `%s, create trophy <emojy> <description>`", me)
msg += fmt.Sprintf("\nYou can award a trophy with `%s, grant <emojy> to <nick>`", me)
msg += "\nYou can see others awards with `How great is <nick>`"
msg += "\nYou can see your awards with `How great am I?`"
2020-04-20 09:56:49 +00:00
p.bot.Send(c, bot.Message, ch, msg)
return true
}
// Award is used by other plugins to register a particular award for a user
2020-04-24 20:24:29 +00:00
func (p *AchievementsPlugin) Grant(nick, emojy string) (Award, error) {
2021-12-20 17:40:10 +00:00
trophy, err := p.FindTrophy(emojy)
2020-04-24 20:24:29 +00:00
if err != nil {
2021-12-20 17:40:10 +00:00
return Award{}, err
2020-04-24 20:24:29 +00:00
}
2021-12-20 17:40:10 +00:00
award := Award{
Holder: nick,
Emojy: emojy,
Description: trophy.Description,
Granted: time.Now(),
2020-04-24 20:24:29 +00:00
}
2021-12-20 17:40:10 +00:00
if err = p.store.Insert(bh.NextSequence(), &award); err != nil {
return Award{}, err
2020-04-24 20:24:29 +00:00
}
2021-12-20 17:40:10 +00:00
return award, err
2020-04-24 20:24:29 +00:00
}
func (p *AchievementsPlugin) Create(emojy, description, creator string) (Trophy, error) {
t, err := p.FindTrophy(emojy)
if err == nil {
return t, fmt.Errorf("the trophy %s already exists", emojy)
}
2021-12-20 17:40:10 +00:00
t = Trophy{
2020-04-24 20:24:29 +00:00
Emojy: emojy,
Description: description,
Creator: creator,
2021-12-20 17:40:10 +00:00
}
err = p.store.Insert(emojy, t)
return t, err
2020-04-24 20:24:29 +00:00
}
type Trophy struct {
Emojy string
Description string
Creator string
}
type Award struct {
2021-12-20 17:40:10 +00:00
ID int64 `boltholderid:"ID"`
Holder string
Emojy string
Description string
Granted time.Time
2020-04-20 09:56:49 +00:00
}
2020-04-24 20:24:29 +00:00
2021-06-17 20:21:30 +00:00
func (p *AchievementsPlugin) AllTrophies() ([]Trophy, error) {
var t []Trophy
2021-12-20 17:40:10 +00:00
err := p.store.Find(&t, &bh.Query{})
2021-06-17 20:21:30 +00:00
return t, err
}
2020-04-24 20:24:29 +00:00
func (p *AchievementsPlugin) FindTrophy(emojy string) (Trophy, error) {
var t Trophy
2021-12-20 17:40:10 +00:00
err := p.store.Find(&t, bh.Where("emojy").Eq(emojy))
2020-04-24 20:24:29 +00:00
return t, err
}