catbase/plugins/leftpad/leftpad.go

57 lines
1.4 KiB
Go
Raw Permalink Normal View History

2016-03-25 16:25:00 +00:00
// © 2016 the CatBase Authors under the WTFPL license. See AUTHORS for the list of authors.
// Leftpad contains the plugin that allows the bot to pad messages
package leftpad
import (
2016-04-01 14:37:44 +00:00
"fmt"
2021-02-07 19:20:54 +00:00
"regexp"
"strconv"
2016-03-25 16:25:00 +00:00
2018-12-07 13:34:25 +00:00
"github.com/chrissexton/leftpad"
2016-03-25 16:25:00 +00:00
"github.com/velour/catbase/bot"
2016-04-01 14:37:44 +00:00
"github.com/velour/catbase/config"
2016-03-25 16:25:00 +00:00
)
type LeftpadPlugin struct {
2016-04-01 14:37:44 +00:00
bot bot.Bot
config *config.Config
2016-03-25 16:25:00 +00:00
}
// New creates a new LeftpadPlugin with the Plugin interface
func New(b bot.Bot) *LeftpadPlugin {
p := &LeftpadPlugin{
bot: b,
config: b.Config(),
2016-03-25 16:25:00 +00:00
}
2021-02-07 19:20:54 +00:00
b.RegisterRegexCmd(p, bot.Message, leftpadRegex, p.leftpadCmd)
return p
2016-03-25 16:25:00 +00:00
}
type leftpadResp struct {
Str string
}
2021-02-07 19:20:54 +00:00
var leftpadRegex = regexp.MustCompile(`(?i)^leftpad (?P<padstr>\S+) (?P<padding>\d+) (?P<text>.+)$`)
2021-02-07 19:20:54 +00:00
func (p *LeftpadPlugin) leftpadCmd(r bot.Request) bool {
padchar := r.Values["padstr"]
length, err := strconv.Atoi(r.Values["padding"])
if err != nil {
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "Invalid padding number")
2016-03-25 16:25:00 +00:00
return true
}
2021-02-07 19:20:54 +00:00
maxLen, who := p.config.GetInt("LeftPad.MaxLen", 50), p.config.Get("LeftPad.Who", "Putin")
if length > maxLen && maxLen > 0 {
msg := fmt.Sprintf("%s would kill me if I did that.", who)
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
return true
}
text := r.Values["text"]
res := leftpad.LeftPad(text, length, padchar)
2016-03-25 16:25:00 +00:00
2021-02-07 19:20:54 +00:00
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, res)
return true
2016-03-25 16:25:00 +00:00
}