package tell import ( "fmt" bh "github.com/timshannon/bolthold" "strings" "github.com/rs/zerolog/log" "github.com/velour/catbase/bot" "github.com/velour/catbase/bot/msg" ) type delayedMsg string type TellPlugin struct { b bot.Bot store *bh.Store } func New(b bot.Bot) *TellPlugin { tp := &TellPlugin{b, b.Store()} b.Register(tp, bot.Message, tp.message) return tp } type Tell struct { ID int `boltholdIndex:"ID"` Who string What string } func (t *TellPlugin) getTells() []Tell { result := []Tell{} t.store.Find(&result, &bh.Query{}) return result } 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") } } func (t *TellPlugin) addTell(who, what string) error { tell := Tell{Who: who, What: what} err := t.store.Insert(bh.NextSequence(), tell) if err != nil { log.Error().Err(err).Msg("could not add Tell") } return err } func (t *TellPlugin) check(who string) []Tell { result := []Tell{} 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 } func (t *TellPlugin) troll(who string) bool { targets := t.b.Config().GetArray("Tell.troll", []string{}) for _, target := range targets { if who == target { return true } } return false } func (t *TellPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool { if strings.HasPrefix(strings.ToLower(message.Body), "Tell ") || strings.HasPrefix(strings.ToLower(message.Body), "tellah ") { parts := strings.Split(message.Body, " ") target := strings.ToLower(parts[1]) if !t.checkValidTarget(message.Channel, target) { if t.troll(message.User.Name) { t.b.Send(c, bot.Message, message.Channel, fmt.Sprintf("Okay. I'll Tell %s.", target)) return true } return false } newMessage := strings.Join(parts[2:], " ") newMessage = fmt.Sprintf("Hey, %s. %s said: %s", target, message.User.Name, newMessage) t.addTell(target, newMessage) t.b.Send(c, bot.Message, message.Channel, fmt.Sprintf("Okay. I'll Tell %s.", target)) return true } uname := strings.ToLower(message.User.Name) if tells := t.check(uname); len(tells) > 0 { for _, m := range tells { t.b.Send(c, bot.Message, message.Channel, m.What) } return true } return false }