2017-07-24 19:09:27 +00:00
|
|
|
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
|
|
|
|
|
|
|
|
package emojifyme
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-04-19 17:33:41 +00:00
|
|
|
"io"
|
2017-07-24 19:09:27 +00:00
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
2021-02-02 02:25:19 +00:00
|
|
|
"regexp"
|
2017-07-24 19:09:27 +00:00
|
|
|
"strings"
|
|
|
|
|
2019-03-07 16:35:42 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2017-07-24 19:09:27 +00:00
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
)
|
|
|
|
|
|
|
|
type EmojifyMePlugin struct {
|
|
|
|
Bot bot.Bot
|
|
|
|
GotBotEmoji bool
|
|
|
|
Emoji map[string]string
|
|
|
|
}
|
|
|
|
|
2019-02-05 19:41:38 +00:00
|
|
|
func New(b bot.Bot) *EmojifyMePlugin {
|
2017-07-24 19:09:27 +00:00
|
|
|
resp, err := http.Get("https://raw.githubusercontent.com/github/gemoji/master/db/emoji.json")
|
|
|
|
if err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Fatal().Err(err).Msg("Error generic emoji list")
|
2017-07-24 19:09:27 +00:00
|
|
|
}
|
2024-04-19 17:33:41 +00:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2017-07-24 19:09:27 +00:00
|
|
|
resp.Body.Close()
|
|
|
|
if err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Fatal().Err(err).Msg("Error generic emoji list body")
|
2017-07-24 19:09:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Emoji struct {
|
2024-04-19 17:33:41 +00:00
|
|
|
Emoji string `json:"emoji"`
|
2019-01-19 14:59:53 +00:00
|
|
|
Aliases []string `json:"aliases"`
|
2017-07-24 19:09:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var emoji []Emoji
|
|
|
|
err = json.Unmarshal(body, &emoji)
|
|
|
|
if err != nil {
|
2020-04-21 15:47:30 +00:00
|
|
|
log.Error().Err(err).Msg("Error parsing emoji list")
|
2017-07-24 19:09:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
emojiMap := map[string]string{}
|
|
|
|
for _, e := range emoji {
|
|
|
|
for _, alias := range e.Aliases {
|
2024-04-19 17:33:41 +00:00
|
|
|
emojiMap[alias] = e.Emoji
|
2017-07-24 19:09:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-19 17:33:41 +00:00
|
|
|
emojy := b.GetEmojiList(true)
|
|
|
|
log.Debug().Interface("emojyList", emojy).Msg("startup")
|
|
|
|
for _, v := range emojy {
|
|
|
|
emojiMap[v] = v
|
|
|
|
}
|
|
|
|
|
2019-02-05 19:41:38 +00:00
|
|
|
ep := &EmojifyMePlugin{
|
|
|
|
Bot: b,
|
2017-07-24 19:09:27 +00:00
|
|
|
GotBotEmoji: false,
|
|
|
|
Emoji: emojiMap,
|
|
|
|
}
|
2024-04-19 17:33:41 +00:00
|
|
|
b.RegisterRegex(ep, bot.Any, regexp.MustCompile(`.*`), ep.message)
|
2019-02-05 19:41:38 +00:00
|
|
|
return ep
|
2017-07-24 19:09:27 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 02:25:19 +00:00
|
|
|
func (p *EmojifyMePlugin) message(r bot.Request) bool {
|
|
|
|
c := r.Conn
|
|
|
|
message := r.Msg
|
2017-07-24 19:09:27 +00:00
|
|
|
if !p.GotBotEmoji {
|
|
|
|
p.GotBotEmoji = true
|
2022-06-08 20:57:24 +00:00
|
|
|
emojiMap := p.Bot.GetEmojiList(false)
|
2017-07-24 19:09:27 +00:00
|
|
|
for e := range emojiMap {
|
|
|
|
p.Emoji[e] = e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-22 00:16:57 +00:00
|
|
|
inertTokens := p.Bot.Config().GetArray("Emojify.Scoreless", []string{})
|
2017-07-31 18:16:59 +00:00
|
|
|
emojied := 0.0
|
2019-01-21 23:00:51 +00:00
|
|
|
emojys := []string{}
|
|
|
|
msg := strings.Replace(strings.ToLower(message.Body), "_", " ", -1)
|
|
|
|
for k, v := range p.Emoji {
|
|
|
|
k = strings.Replace(k, "_", " ", -1)
|
2019-01-21 23:05:35 +00:00
|
|
|
candidates := []string{
|
2019-01-21 23:30:20 +00:00
|
|
|
k,
|
2019-01-21 23:05:35 +00:00
|
|
|
k + "es",
|
|
|
|
k + "s",
|
|
|
|
}
|
|
|
|
for _, c := range candidates {
|
|
|
|
if strings.Contains(msg, " "+c+" ") ||
|
2019-01-22 15:43:10 +00:00
|
|
|
strings.HasPrefix(msg, c+" ") ||
|
2019-01-22 15:59:08 +00:00
|
|
|
strings.HasSuffix(msg, " "+c) ||
|
|
|
|
msg == c {
|
2019-01-21 23:05:35 +00:00
|
|
|
emojys = append(emojys, v)
|
2019-01-22 15:52:28 +00:00
|
|
|
if !stringsContain(inertTokens, k) && len(v) > 2 {
|
2019-01-21 23:30:20 +00:00
|
|
|
emojied += 1
|
2019-01-21 23:05:35 +00:00
|
|
|
}
|
2017-11-28 03:07:59 +00:00
|
|
|
}
|
2017-07-24 19:09:27 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-21 23:00:51 +00:00
|
|
|
|
2019-01-22 00:16:57 +00:00
|
|
|
if emojied > 0 && rand.Float64() <= p.Bot.Config().GetFloat64("Emojify.Chance", 0.02)*emojied {
|
2019-01-21 23:00:51 +00:00
|
|
|
for _, e := range emojys {
|
2024-04-19 17:33:41 +00:00
|
|
|
log.Debug().
|
|
|
|
Str("emojy", e).
|
|
|
|
Msgf("trying to react")
|
|
|
|
_, err := p.Bot.Send(c, bot.Reaction, message.Channel, e, message)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Msgf("emojy react error: %s", err)
|
|
|
|
}
|
2019-01-21 22:15:46 +00:00
|
|
|
}
|
2019-02-05 21:01:12 +00:00
|
|
|
return false
|
2017-07-31 18:16:59 +00:00
|
|
|
}
|
2017-07-24 19:09:27 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-12-03 18:04:55 +00:00
|
|
|
func stringsContain(haystack []string, needle string) bool {
|
|
|
|
for _, s := range haystack {
|
|
|
|
if s == needle {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|