2017-06-17 20:20:06 +00:00
|
|
|
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
|
|
|
|
|
|
|
|
package reaction
|
|
|
|
|
|
|
|
import (
|
2019-11-11 23:09:42 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
2017-06-17 20:20:06 +00:00
|
|
|
|
2019-09-27 14:39:43 +00:00
|
|
|
"github.com/chrissexton/sentiment"
|
2020-07-24 16:31:57 +00:00
|
|
|
|
2017-06-17 20:20:06 +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"
|
2017-06-17 20:20:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ReactionPlugin struct {
|
2019-05-27 23:21:53 +00:00
|
|
|
bot bot.Bot
|
|
|
|
config *config.Config
|
2019-09-27 14:39:43 +00:00
|
|
|
|
|
|
|
model sentiment.Models
|
2020-07-24 16:31:57 +00:00
|
|
|
br *bayesReactor
|
2017-06-17 20:20:06 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 19:41:38 +00:00
|
|
|
func New(b bot.Bot) *ReactionPlugin {
|
2019-09-27 14:39:43 +00:00
|
|
|
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")
|
2019-02-05 19:41:38 +00:00
|
|
|
rp := &ReactionPlugin{
|
2019-05-27 23:21:53 +00:00
|
|
|
bot: b,
|
2020-07-24 16:31:57 +00:00
|
|
|
config: c,
|
2019-09-27 14:39:43 +00:00
|
|
|
model: model,
|
2020-07-24 16:31:57 +00:00
|
|
|
br: newBayesReactor(path),
|
2017-06-17 20:20:06 +00:00
|
|
|
}
|
2019-02-05 19:41:38 +00:00
|
|
|
b.Register(rp, bot.Message, rp.message)
|
|
|
|
return rp
|
2017-06-17 20:20:06 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
func (p *ReactionPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
2020-07-24 16:31:57 +00:00
|
|
|
emojy, prob := p.br.React(message.Body)
|
|
|
|
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 {
|
|
|
|
p.bot.Send(c, bot.Reaction, message.Channel, emojy, message)
|
2017-08-30 17:41:58 +00:00
|
|
|
}
|
|
|
|
|
2019-11-11 23:09:42 +00:00
|
|
|
p.checkReactions(c, message)
|
|
|
|
|
2017-06-17 20:20:06 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|