catbase/plugins/reaction/reaction.go

76 lines
1.8 KiB
Go
Raw Normal View History

// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
package reaction
import (
2021-02-19 17:15:20 +00:00
"regexp"
2019-11-11 23:09:42 +00:00
"strings"
"github.com/rs/zerolog/log"
"github.com/chrissexton/sentiment"
2020-07-24 16:31:57 +00:00
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
2017-08-30 17:41:58 +00:00
"github.com/velour/catbase/config"
)
type ReactionPlugin struct {
2019-05-27 23:21:53 +00:00
bot bot.Bot
config *config.Config
model sentiment.Models
2020-07-24 16:31:57 +00:00
br *bayesReactor
}
func New(b bot.Bot) *ReactionPlugin {
model, err := sentiment.Restore()
if err != nil {
log.Fatal().Err(err).Msg("Couldn't restore sentiment model")
}
2020-07-24 16:31:57 +00:00
c := b.Config()
path := c.GetString("reaction.modelpath", "emojy.model.json")
rp := &ReactionPlugin{
2019-05-27 23:21:53 +00:00
bot: b,
2020-07-24 16:31:57 +00:00
config: c,
model: model,
2020-07-24 16:31:57 +00:00
br: newBayesReactor(path),
}
2021-02-19 17:15:20 +00:00
b.RegisterRegex(rp, bot.Message, regexp.MustCompile(`.*`), rp.message)
return rp
}
2021-02-19 17:15:20 +00:00
func (p *ReactionPlugin) message(r bot.Request) bool {
emojy, prob := p.br.React(r.Msg.Body)
2020-07-24 16:31:57 +00:00
target := p.config.GetFloat64("reaction.confidence", 0.5)
log.Debug().
Float64("prob", prob).
Float64("target", target).
Bool("accept", prob > target).
Str("emojy", emojy).
Msgf("Reaction check")
if prob > target {
2021-02-19 17:15:20 +00:00
p.bot.Send(r.Conn, bot.Reaction, r.Msg.Channel, emojy, r.Msg)
2017-08-30 17:41:58 +00:00
}
2021-02-19 17:15:20 +00:00
p.checkReactions(r.Conn, r.Msg)
2019-11-11 23:09:42 +00:00
return false
}
2019-11-11 23:09:42 +00:00
2020-05-25 18:42:56 +00:00
// b will always react if a message contains a check word
2019-11-11 23:09:42 +00:00
// Note that reactions must not be enclosed in :
func (p *ReactionPlugin) checkReactions(c bot.Connector, m msg.Message) {
checkWords := p.config.GetArray("reaction.checkwords", []string{})
reactions := p.config.GetArray("reaction.checkedreactions", []string{})
for i, w := range checkWords {
if strings.Contains(strings.ToLower(m.Body), w) {
react := strings.Trim(reactions[i], ":")
p.bot.Send(c, bot.Reaction, m.Channel, react, m)
}
}
}