achievements: refactor

Refs #346
This commit is contained in:
Chris Sexton 2021-01-31 22:14:55 -05:00 committed by Chris Sexton
parent e7ec092c64
commit 00dae33e8a
2 changed files with 67 additions and 67 deletions

View File

@ -31,7 +31,11 @@ func New(b bot.Bot) *AchievementsPlugin {
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("unable to create achievements tables") log.Fatal().Err(err).Msg("unable to create achievements tables")
} }
b.Register(ap, bot.Message, ap.message)
b.RegisterRegexCmd(ap, bot.Message, grantRegex, ap.grantCmd)
b.RegisterRegexCmd(ap, bot.Message, createRegex, ap.createCmd)
b.RegisterRegexCmd(ap, bot.Message, greatRegex, ap.greatCmd)
b.Register(ap, bot.Help, ap.help) b.Register(ap, bot.Help, ap.help)
return ap return ap
} }
@ -80,77 +84,74 @@ var grantRegex = regexp.MustCompile(`(?i)grant (?P<emojy>(?::[[:word:][:punct:]]
var createRegex = regexp.MustCompile(`(?i)create trophy (?P<emojy>(?::[[:word:][:punct:]]+:\s?)+) (?P<description>.+)`) 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 greatRegex = regexp.MustCompile(`(?i)how great (?:am i|is :?(?P<who>[[:word:]]+))[[:punct:]]*`)
func (p *AchievementsPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool { func (p *AchievementsPlugin) grantCmd(r bot.Request) bool {
nick := message.User.Name 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)
return true
}
if nick == trophy.Creator {
a, err := p.Grant(receiver, emojy)
if err != nil {
log.Error().Err(err).Msg("could not award trophy")
}
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)
}
return true
}
if greatRegex.MatchString(message.Body) { func (p *AchievementsPlugin) createCmd(r bot.Request) bool {
submatches := greatRegex.FindAllStringSubmatch(message.Body, -1) nick := r.Msg.User.Name
who := submatches[0][1] emojy := r.Values["emojy"]
if who == "" { description := r.Values["description"]
who = nick t, err := p.Create(emojy, description, nick)
} if err != nil {
awards := p.GetAwards(who) log.Error().Err(err).Msg("could not create trophy")
if len(awards) == 0 { if strings.Contains(err.Error(), "exists") {
m := fmt.Sprintf("%s has no achievements to their name. They really suck.", who) p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, err.Error())
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(c, bot.Message, message.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(c, bot.Message, message.Channel, m)
}
return true
}
if message.Command && grantRegex.MatchString(message.Body) {
submatches := grantRegex.FindAllStringSubmatch(message.Body, -1)
emojy := submatches[0][1]
receiver := submatches[0][2]
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(c, bot.Message, message.Channel, msg)
return true return true
} }
if nick == trophy.Creator { p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "I'm too humble to ever award that trophy")
a, err := p.Grant(receiver, emojy)
if err != nil {
log.Error().Err(err).Msg("could not award trophy")
}
msg := fmt.Sprintf("Congrats %s. You just got the %s award for %s.",
receiver, emojy, a.Description)
p.bot.Send(c, bot.Message, message.Channel, msg)
} else {
msg := fmt.Sprintf("Sorry, %s. %s owns that trophy.", nick, trophy.Creator)
p.bot.Send(c, bot.Message, message.Channel, msg)
}
return true return true
} }
if message.Command && createRegex.MatchString(message.Body) { resp := fmt.Sprintf("Okay %s. I have crafted a one-of-a-kind %s trophy to give for %s",
submatches := createRegex.FindAllStringSubmatch(message.Body, -1) nick, t.Emojy, t.Description)
emojy := submatches[0][1] p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, resp)
description := submatches[0][2] return true
t, err := p.Create(emojy, description, nick) }
if err != nil {
log.Error().Err(err).Msg("could not create trophy") func (p *AchievementsPlugin) greatCmd(r bot.Request) bool {
if strings.Contains(err.Error(), "exists") { nick := r.Msg.User.Name
p.bot.Send(c, bot.Message, message.Channel, err.Error()) who := r.Values["who"]
return true if who == "" {
} who = nick
p.bot.Send(c, bot.Message, message.Channel, "I'm too humble to ever award that trophy")
return true
}
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(c, bot.Message, message.Channel, resp)
return true
} }
return false 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
} }
func (p *AchievementsPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool { func (p *AchievementsPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {

View File

@ -1 +0,0 @@
package achievements