2016-01-17 18:00:44 +00:00
|
|
|
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
|
2013-12-10 23:37:07 +00:00
|
|
|
|
2016-03-24 17:32:40 +00:00
|
|
|
package your
|
2013-03-18 21:21:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"strings"
|
|
|
|
|
2016-01-17 18:00:44 +00:00
|
|
|
"github.com/velour/catbase/bot"
|
2016-04-01 14:20:03 +00:00
|
|
|
"github.com/velour/catbase/bot/msg"
|
2017-03-13 17:44:44 +00:00
|
|
|
"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
|
2017-03-13 17:44:44 +00:00
|
|
|
config *config.Config
|
2013-03-18 21:21:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewYourPlugin creates a new YourPlugin with the Plugin interface
|
2016-03-30 14:00:20 +00:00
|
|
|
func New(bot bot.Bot) *YourPlugin {
|
2013-03-18 21:21:37 +00:00
|
|
|
return &YourPlugin{
|
2018-05-02 11:02:04 +00:00
|
|
|
bot: bot,
|
2017-03-13 17:44:44 +00:00
|
|
|
config: bot.Config(),
|
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.
|
2016-04-01 14:20:03 +00:00
|
|
|
func (p *YourPlugin) Message(message msg.Message) bool {
|
2019-01-22 00:16:57 +00:00
|
|
|
maxLen := p.config.GetInt("your.maxlength", 140)
|
2019-01-21 19:24:03 +00:00
|
|
|
if len(message.Body) > maxLen {
|
2016-03-24 17:49:44 +00:00
|
|
|
return false
|
|
|
|
}
|
2017-03-13 17:44:44 +00:00
|
|
|
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", "")
|
2019-01-20 20:21:26 +00:00
|
|
|
if rand.Float64() < freq {
|
|
|
|
r := strings.NewReplacer(this, that)
|
2017-03-13 17:44:44 +00:00
|
|
|
msg = r.Replace(msg)
|
2017-03-13 16:41:17 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-13 17:44:44 +00:00
|
|
|
if msg != message.Body {
|
|
|
|
p.bot.SendMessage(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(channel string, parts []string) {
|
2016-03-24 17:49:44 +00:00
|
|
|
p.bot.SendMessage(channel, "Your corrects people's grammar.")
|
2013-03-18 21:21:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Empty event handler because this plugin does not do anything on event recv
|
2016-04-01 14:20:03 +00:00
|
|
|
func (p *YourPlugin) Event(kind string, message msg.Message) bool {
|
2013-03-18 21:21:37 +00:00
|
|
|
return false
|
|
|
|
}
|
2013-05-08 00:08:18 +00:00
|
|
|
|
|
|
|
// Handler for bot's own messages
|
2016-04-01 14:20:03 +00:00
|
|
|
func (p *YourPlugin) BotMessage(message msg.Message) bool {
|
2013-05-08 00:08:18 +00:00
|
|
|
return false
|
|
|
|
}
|
2013-06-01 17:10:15 +00:00
|
|
|
|
|
|
|
// Register any web URLs desired
|
|
|
|
func (p *YourPlugin) RegisterWeb() *string {
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-31 18:14:45 +00:00
|
|
|
|
|
|
|
func (p *YourPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false }
|