catbase/plugins/reaction/reaction.go

66 lines
1.6 KiB
Go
Raw Normal View History

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