mirror of https://github.com/velour/catbase.git
commit
e42440fa61
|
@ -4,6 +4,10 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
|
"github.com/jmoiron/sqlx"
|
||||||
|
|
||||||
"github.com/velour/catbase/bot"
|
"github.com/velour/catbase/bot"
|
||||||
"github.com/velour/catbase/bot/msg"
|
"github.com/velour/catbase/bot/msg"
|
||||||
)
|
)
|
||||||
|
@ -12,31 +16,99 @@ type delayedMsg string
|
||||||
|
|
||||||
type TellPlugin struct {
|
type TellPlugin struct {
|
||||||
b bot.Bot
|
b bot.Bot
|
||||||
users map[string][]string
|
db *sqlx.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(b bot.Bot) *TellPlugin {
|
func New(b bot.Bot) *TellPlugin {
|
||||||
tp := &TellPlugin{b, make(map[string][]string)}
|
tp := &TellPlugin{b, b.DB()}
|
||||||
b.Register(tp, bot.Message, tp.message)
|
b.Register(tp, bot.Message, tp.message)
|
||||||
|
tp.createDB()
|
||||||
return tp
|
return tp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type tell struct {
|
||||||
|
ID int
|
||||||
|
Who string
|
||||||
|
What string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TellPlugin) createDB() {
|
||||||
|
q := `create table if not exists tell (
|
||||||
|
id integer primary key autoincrement,
|
||||||
|
who string,
|
||||||
|
what string
|
||||||
|
)`
|
||||||
|
t.db.MustExec(q)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TellPlugin) getTells() []tell {
|
||||||
|
result := []tell{}
|
||||||
|
q := `select * from tell`
|
||||||
|
t.db.Select(&result, q)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TellPlugin) rmTell(entry tell) {
|
||||||
|
q := `delete from tell where id=?`
|
||||||
|
if _, err := t.db.Exec(q, entry.ID); err != nil {
|
||||||
|
log.Error().Err(err).Msg("could not remove tell")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TellPlugin) addTell(who, what string) error {
|
||||||
|
q := `insert into tell (who, what) values (?, ?)`
|
||||||
|
_, err := t.db.Exec(q, who, what)
|
||||||
|
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) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
func (t *TellPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
||||||
if strings.HasPrefix(strings.ToLower(message.Body), "tell ") {
|
if strings.HasPrefix(strings.ToLower(message.Body), "tell ") {
|
||||||
parts := strings.Split(message.Body, " ")
|
parts := strings.Split(message.Body, " ")
|
||||||
target := strings.ToLower(parts[1])
|
target := strings.ToLower(parts[1])
|
||||||
|
if !t.checkValidTarget(message.Channel, target) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
newMessage := strings.Join(parts[2:], " ")
|
newMessage := strings.Join(parts[2:], " ")
|
||||||
newMessage = fmt.Sprintf("Hey, %s. %s said: %s", target, message.User.Name, newMessage)
|
newMessage = fmt.Sprintf("Hey, %s. %s said: %s", target, message.User.Name, newMessage)
|
||||||
t.users[target] = append(t.users[target], newMessage)
|
t.addTell(target, newMessage)
|
||||||
t.b.Send(c, bot.Message, message.Channel, fmt.Sprintf("Okay. I'll tell %s.", target))
|
t.b.Send(c, bot.Message, message.Channel, fmt.Sprintf("Okay. I'll tell %s.", target))
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
uname := strings.ToLower(message.User.Name)
|
uname := strings.ToLower(message.User.Name)
|
||||||
if msg, ok := t.users[uname]; ok && len(msg) > 0 {
|
if tells := t.check(uname); len(tells) > 0 {
|
||||||
for _, m := range msg {
|
for _, m := range tells {
|
||||||
t.b.Send(c, bot.Message, message.Channel, string(m))
|
t.b.Send(c, bot.Message, message.Channel, m.What)
|
||||||
}
|
}
|
||||||
t.users[uname] = []string{}
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
|
Loading…
Reference in New Issue