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 (
|
|
|
|
"math/rand"
|
|
|
|
|
|
|
|
"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 {
|
2018-05-02 11:02:04 +00:00
|
|
|
Bot bot.Bot
|
2017-08-30 17:41:58 +00:00
|
|
|
Config *config.Config
|
2017-06-17 20:20:06 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 19:41:38 +00:00
|
|
|
func New(b bot.Bot) *ReactionPlugin {
|
|
|
|
rp := &ReactionPlugin{
|
|
|
|
Bot: b,
|
|
|
|
Config: b.Config(),
|
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-02-05 19:41:38 +00:00
|
|
|
func (p *ReactionPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
2017-08-30 18:44:04 +00:00
|
|
|
harrass := false
|
2019-01-22 00:16:57 +00:00
|
|
|
for _, nick := range p.Config.GetArray("Reaction.HarrassList", []string{}) {
|
2017-08-30 17:41:58 +00:00
|
|
|
if message.User.Name == nick {
|
2017-08-30 18:44:04 +00:00
|
|
|
harrass = true
|
2017-08-30 17:41:58 +00:00
|
|
|
break
|
|
|
|
}
|
2017-06-17 20:20:06 +00:00
|
|
|
}
|
2017-08-30 17:41:58 +00:00
|
|
|
|
2019-01-22 00:16:57 +00:00
|
|
|
chance := p.Config.GetFloat64("Reaction.GeneralChance", 0.01)
|
2017-08-30 18:44:04 +00:00
|
|
|
negativeWeight := 1
|
|
|
|
if harrass {
|
2019-01-22 00:16:57 +00:00
|
|
|
chance = p.Config.GetFloat64("Reaction.HarrassChance", 0.05)
|
|
|
|
negativeWeight = p.Config.GetInt("Reaction.NegativeHarrassmentMultiplier", 2)
|
2017-08-30 18:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if rand.Float64() < chance {
|
2019-01-22 00:16:57 +00:00
|
|
|
numPositiveReactions := len(p.Config.GetArray("Reaction.PositiveReactions", []string{}))
|
|
|
|
numNegativeReactions := len(p.Config.GetArray("Reaction.NegativeReactions", []string{}))
|
2017-08-30 18:44:04 +00:00
|
|
|
|
2018-05-02 11:02:04 +00:00
|
|
|
maxIndex := numPositiveReactions + numNegativeReactions*negativeWeight
|
2017-08-30 18:44:04 +00:00
|
|
|
|
|
|
|
index := rand.Intn(maxIndex)
|
|
|
|
|
|
|
|
reaction := ""
|
|
|
|
|
|
|
|
if index < numPositiveReactions {
|
2019-01-22 00:16:57 +00:00
|
|
|
reaction = p.Config.GetArray("Reaction.PositiveReactions", []string{})[index]
|
2017-08-30 18:44:04 +00:00
|
|
|
} else {
|
|
|
|
index -= numPositiveReactions
|
|
|
|
index %= numNegativeReactions
|
2019-01-22 00:16:57 +00:00
|
|
|
reaction = p.Config.GetArray("Reaction.NegativeReactions", []string{})[index]
|
2017-08-30 17:41:58 +00:00
|
|
|
}
|
2017-08-30 18:44:04 +00:00
|
|
|
|
2019-02-05 15:54:13 +00:00
|
|
|
p.Bot.Send(bot.Reaction, message.Channel, reaction, message)
|
2017-08-30 17:41:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-17 20:20:06 +00:00
|
|
|
return false
|
|
|
|
}
|