catbase/plugins/your/your.go

62 lines
1.8 KiB
Go
Raw Permalink Normal View History

2016-01-17 18:00:44 +00:00
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
package your
2013-03-18 21:21:37 +00:00
import (
"math/rand"
2021-03-23 17:40:18 +00:00
"regexp"
2013-03-18 21:21:37 +00:00
"strings"
2016-01-17 18:00:44 +00:00
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/config"
2013-03-18 21:21:37 +00:00
)
type YourPlugin struct {
2018-05-02 11:02:04 +00:00
bot bot.Bot
config *config.Config
2013-03-18 21:21:37 +00:00
}
// NewYourPlugin creates a new YourPlugin with the Plugin interface
func New(b bot.Bot) *YourPlugin {
yp := &YourPlugin{
bot: b,
config: b.Config(),
2013-03-18 21:21:37 +00:00
}
2021-03-23 17:40:18 +00:00
b.RegisterRegex(yp, bot.Message, regexp.MustCompile(`.*`), yp.message)
b.Register(yp, bot.Help, yp.help)
return yp
2013-03-18 21:21:37 +00:00
}
// Message responds to the bot hook on recieving messages.
// This function returns true if the plugin responds in a meaningful way to the users message.
// Otherwise, the function returns false and the bot continues execution of other plugins.
2021-03-23 17:40:18 +00:00
func (p *YourPlugin) message(r bot.Request) bool {
message := r.Msg
2019-01-22 00:16:57 +00:00
maxLen := p.config.GetInt("your.maxlength", 140)
if len(message.Body) > maxLen {
2016-03-24 17:49:44 +00:00
return false
}
msg := message.Body
2019-01-22 00:16:57 +00:00
for _, replacement := range p.config.GetArray("Your.Replacements", []string{}) {
freq := p.config.GetFloat64("your.replacements."+replacement+".freq", 0.0)
this := p.config.Get("your.replacements."+replacement+".this", "")
that := p.config.Get("your.replacements."+replacement+".that", "")
if rand.Float64() < freq {
r := strings.NewReplacer(this, that)
msg = r.Replace(msg)
2017-03-13 16:41:17 +00:00
}
}
if msg != message.Body {
2021-03-23 17:40:18 +00:00
p.bot.Send(r.Conn, bot.Message, message.Channel, msg)
return true
2017-03-13 16:41:17 +00:00
}
2013-03-18 21:21:37 +00:00
return false
}
// Help responds to help requests. Every plugin must implement a help function.
func (p *YourPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...any) bool {
2019-05-27 23:21:53 +00:00
p.bot.Send(c, bot.Message, message.Channel, "Your corrects people's grammar.")
return true
}