2022-05-30 20:34:42 +00:00
|
|
|
package emojy
|
|
|
|
|
|
|
|
import (
|
2022-06-06 14:20:50 +00:00
|
|
|
"github.com/forPelevin/gomoji"
|
2022-05-30 20:34:42 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/config"
|
2022-06-06 14:20:50 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2022-05-30 20:34:42 +00:00
|
|
|
"regexp"
|
2022-06-06 14:20:50 +00:00
|
|
|
"strings"
|
2022-06-02 15:00:48 +00:00
|
|
|
"time"
|
2022-05-30 20:34:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type EmojyPlugin struct {
|
|
|
|
b bot.Bot
|
|
|
|
c *config.Config
|
|
|
|
db *sqlx.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(b bot.Bot) *EmojyPlugin {
|
|
|
|
log.Debug().Msgf("emojy.New")
|
|
|
|
p := &EmojyPlugin{
|
|
|
|
b: b,
|
|
|
|
c: b.Config(),
|
|
|
|
db: b.DB(),
|
|
|
|
}
|
|
|
|
p.setupDB()
|
|
|
|
p.register()
|
|
|
|
p.registerWeb()
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *EmojyPlugin) setupDB() {
|
2022-06-02 15:00:48 +00:00
|
|
|
p.db.MustExec(`create table if not exists emojyLog (
|
|
|
|
id integer primary key autoincrement,
|
|
|
|
emojy text,
|
|
|
|
observed datetime
|
2022-05-30 20:34:42 +00:00
|
|
|
)`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *EmojyPlugin) register() {
|
|
|
|
ht := bot.HandlerTable{
|
2022-06-06 14:20:50 +00:00
|
|
|
{
|
|
|
|
Kind: bot.Message, IsCmd: false,
|
|
|
|
Regex: regexp.MustCompile(`.*`),
|
|
|
|
Handler: func(request bot.Request) bool {
|
|
|
|
r := regexp.MustCompile(`:[a-zA-Z0-9_-]+:`)
|
|
|
|
for _, match := range r.FindAllString(request.Msg.Body, -1) {
|
|
|
|
log.Debug().Msgf("Emojy detected: %s", match)
|
|
|
|
p.recordReaction(match)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
},
|
2022-05-30 20:34:42 +00:00
|
|
|
{
|
|
|
|
Kind: bot.Reaction, IsCmd: false,
|
|
|
|
Regex: regexp.MustCompile(`.*`),
|
|
|
|
Handler: func(request bot.Request) bool {
|
|
|
|
log.Debug().Msgf("Emojy detected: %s", request.Msg.Body)
|
|
|
|
p.recordReaction(request.Msg.Body)
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
p.b.RegisterTable(p, ht)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *EmojyPlugin) recordReaction(emojy string) error {
|
2022-06-02 15:00:48 +00:00
|
|
|
q := `insert into emojyLog (emojy, observed) values (?, ?)`
|
|
|
|
_, err := p.db.Exec(q, emojy, time.Now())
|
2022-05-30 20:34:42 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("recordReaction")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type EmojyEntry struct {
|
2022-06-02 15:00:48 +00:00
|
|
|
Emojy string `db:"emojy"`
|
|
|
|
Observed time.Time `db:"observed"`
|
2022-05-30 20:34:42 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 15:00:48 +00:00
|
|
|
type EmojyCount struct {
|
2022-06-06 14:47:11 +00:00
|
|
|
Emojy string `json:"emojy"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Count int `json:"count"`
|
|
|
|
OnServer bool `json:"onServer"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func invertEmojyList(emojy map[string]string) map[string]string {
|
|
|
|
out := map[string]string{}
|
|
|
|
for k, v := range emojy {
|
|
|
|
out[v] = k
|
|
|
|
}
|
|
|
|
return out
|
2022-06-02 15:00:48 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 14:20:50 +00:00
|
|
|
func (p *EmojyPlugin) allCounts() (map[string][]EmojyCount, error) {
|
|
|
|
out := map[string][]EmojyCount{}
|
2022-06-06 14:47:11 +00:00
|
|
|
onServerList := invertEmojyList(p.b.DefaultConnector().GetEmojiList())
|
2022-06-02 15:00:48 +00:00
|
|
|
q := `select emojy, count(observed) as count from emojyLog group by emojy order by count desc`
|
|
|
|
result := []EmojyCount{}
|
2022-05-30 20:34:42 +00:00
|
|
|
err := p.db.Select(&result, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-06 14:20:50 +00:00
|
|
|
for _, e := range result {
|
2022-06-06 14:47:11 +00:00
|
|
|
_, e.OnServer = onServerList[e.Emojy]
|
2022-06-06 14:20:50 +00:00
|
|
|
if isEmoji(e.Emojy) {
|
|
|
|
out["emoji"] = append(out["emoji"], e)
|
|
|
|
} else if ok, fname, _ := p.isKnownEmojy(e.Emojy); ok {
|
|
|
|
e.URL = fname
|
|
|
|
out["emojy"] = append(out["emojy"], e)
|
|
|
|
} else {
|
|
|
|
out["unknown"] = append(out["unknown"], e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *EmojyPlugin) isKnownEmojy(name string) (bool, string, error) {
|
|
|
|
emojyPath := p.c.Get("emojy.path", "emojy")
|
|
|
|
baseURL := p.c.Get("emojy.baseURL", "/emojy/file")
|
|
|
|
entries, err := os.ReadDir(emojyPath)
|
|
|
|
if err != nil {
|
|
|
|
return false, "", err
|
|
|
|
}
|
|
|
|
for _, e := range entries {
|
|
|
|
if !e.IsDir() && strings.HasPrefix(e.Name(), name) {
|
|
|
|
url := path.Join(baseURL, e.Name())
|
|
|
|
return true, url, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isEmoji(in string) bool {
|
|
|
|
return gomoji.ContainsEmoji(in)
|
2022-05-30 20:34:42 +00:00
|
|
|
}
|