catbase/plugins/tell/tell.go

118 lines
2.6 KiB
Go
Raw Normal View History

2017-11-16 22:05:22 +00:00
package tell
import (
"fmt"
2021-12-20 17:40:10 +00:00
bh "github.com/timshannon/bolthold"
2017-11-16 22:05:22 +00:00
"strings"
2019-10-08 20:50:29 +00:00
"github.com/rs/zerolog/log"
2017-11-16 22:05:22 +00:00
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
)
type delayedMsg string
type TellPlugin struct {
2021-12-20 17:40:10 +00:00
b bot.Bot
store *bh.Store
2017-11-16 22:05:22 +00:00
}
func New(b bot.Bot) *TellPlugin {
2021-12-20 17:40:10 +00:00
tp := &TellPlugin{b, b.Store()}
b.Register(tp, bot.Message, tp.message)
return tp
2017-11-16 22:05:22 +00:00
}
2021-12-22 03:36:08 +00:00
type Tell struct {
2021-12-20 17:40:10 +00:00
ID int `boltholdIndex:"ID"`
2019-10-08 20:50:29 +00:00
Who string
What string
}
2021-12-22 03:36:08 +00:00
func (t *TellPlugin) getTells() []Tell {
result := []Tell{}
2021-12-20 17:40:10 +00:00
t.store.Find(&result, &bh.Query{})
2019-10-08 20:50:29 +00:00
return result
}
2021-12-22 03:36:08 +00:00
func (t *TellPlugin) rmTell(entry Tell) {
if err := t.store.Delete(entry.ID, Tell{}); err != nil {
log.Error().Err(err).Msg("could not remove Tell")
2019-10-08 20:50:29 +00:00
}
}
func (t *TellPlugin) addTell(who, what string) error {
2021-12-22 03:36:08 +00:00
tell := Tell{Who: who, What: what}
2021-12-20 17:40:10 +00:00
err := t.store.Insert(bh.NextSequence(), tell)
2019-10-08 20:50:29 +00:00
if err != nil {
2021-12-22 03:36:08 +00:00
log.Error().Err(err).Msg("could not add Tell")
2019-10-08 20:50:29 +00:00
}
return err
}
2021-12-22 03:36:08 +00:00
func (t *TellPlugin) check(who string) []Tell {
result := []Tell{}
2019-10-08 20:50:29 +00:00
tells := t.getTells()
for _, e := range tells {
if e.Who == who {
result = append(result, e)
t.rmTell(e)
}
}
return result
}
func (t *TellPlugin) checkValidTarget(ch, target string) bool {
users := t.b.Who(ch)
log.Debug().
Str("ch", ch).
Str("target", target).
Interface("users", users).
Msg("checking valid target")
for _, u := range users {
if u.Name == target {
return true
}
}
return false
}
2019-10-08 21:09:12 +00:00
func (t *TellPlugin) troll(who string) bool {
2021-12-22 03:36:08 +00:00
targets := t.b.Config().GetArray("Tell.troll", []string{})
2019-10-08 21:09:12 +00:00
for _, target := range targets {
if who == target {
return true
}
}
return false
}
2019-05-27 23:21:53 +00:00
func (t *TellPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
2021-12-22 03:36:08 +00:00
if strings.HasPrefix(strings.ToLower(message.Body), "Tell ") ||
2019-10-08 21:09:12 +00:00
strings.HasPrefix(strings.ToLower(message.Body), "tellah ") {
2017-11-16 22:05:22 +00:00
parts := strings.Split(message.Body, " ")
2017-11-17 13:39:35 +00:00
target := strings.ToLower(parts[1])
2019-10-08 20:50:29 +00:00
if !t.checkValidTarget(message.Channel, target) {
2019-10-08 21:09:12 +00:00
if t.troll(message.User.Name) {
2021-12-22 03:36:08 +00:00
t.b.Send(c, bot.Message, message.Channel, fmt.Sprintf("Okay. I'll Tell %s.", target))
2019-10-08 21:09:12 +00:00
return true
}
2019-10-08 20:50:29 +00:00
return false
}
2017-11-16 22:05:22 +00:00
newMessage := strings.Join(parts[2:], " ")
newMessage = fmt.Sprintf("Hey, %s. %s said: %s", target, message.User.Name, newMessage)
2019-10-08 20:50:29 +00:00
t.addTell(target, newMessage)
2021-12-22 03:36:08 +00:00
t.b.Send(c, bot.Message, message.Channel, fmt.Sprintf("Okay. I'll Tell %s.", target))
2017-11-16 22:05:22 +00:00
return true
}
2017-11-17 13:39:35 +00:00
uname := strings.ToLower(message.User.Name)
2019-10-08 20:50:29 +00:00
if tells := t.check(uname); len(tells) > 0 {
for _, m := range tells {
t.b.Send(c, bot.Message, message.Channel, m.What)
2017-11-16 22:05:22 +00:00
}
return true
}
return false
}