mirror of https://github.com/velour/catbase.git
62 lines
1.8 KiB
Go
62 lines
1.8 KiB
Go
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
|
|
|
|
package your
|
|
|
|
import (
|
|
"math/rand"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/velour/catbase/bot"
|
|
"github.com/velour/catbase/bot/msg"
|
|
"github.com/velour/catbase/config"
|
|
)
|
|
|
|
type YourPlugin struct {
|
|
bot bot.Bot
|
|
config *config.Config
|
|
}
|
|
|
|
// NewYourPlugin creates a new YourPlugin with the Plugin interface
|
|
func New(b bot.Bot) *YourPlugin {
|
|
yp := &YourPlugin{
|
|
bot: b,
|
|
config: b.Config(),
|
|
}
|
|
b.RegisterRegex(yp, bot.Message, regexp.MustCompile(`.*`), yp.message)
|
|
b.Register(yp, bot.Help, yp.help)
|
|
return yp
|
|
}
|
|
|
|
// 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.
|
|
func (p *YourPlugin) message(r bot.Request) bool {
|
|
message := r.Msg
|
|
maxLen := p.config.GetInt("your.maxlength", 140)
|
|
if len(message.Body) > maxLen {
|
|
return false
|
|
}
|
|
msg := message.Body
|
|
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)
|
|
}
|
|
}
|
|
if msg != message.Body {
|
|
p.bot.Send(r.Conn, bot.Message, message.Channel, msg)
|
|
return true
|
|
}
|
|
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 {
|
|
p.bot.Send(c, bot.Message, message.Channel, "Your corrects people's grammar.")
|
|
return true
|
|
}
|