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"
|
2016-03-31 03:09:35 +00:00
|
|
|
"strconv"
|
2016-03-25 16:25:00 +00:00
|
|
|
"strings"
|
|
|
|
|
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:20:03 +00:00
|
|
|
"github.com/velour/catbase/bot/msg"
|
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
|
2019-02-05 19:41:38 +00:00
|
|
|
func New(b bot.Bot) *LeftpadPlugin {
|
|
|
|
p := &LeftpadPlugin{
|
|
|
|
bot: b,
|
|
|
|
config: b.Config(),
|
2016-03-25 16:25:00 +00:00
|
|
|
}
|
2019-02-05 19:41:38 +00:00
|
|
|
b.Register(p, bot.Message, p.message)
|
|
|
|
return p
|
2016-03-25 16:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type leftpadResp struct {
|
|
|
|
Str string
|
|
|
|
}
|
|
|
|
|
2019-02-05 19:41:38 +00:00
|
|
|
func (p *LeftpadPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
2016-03-25 16:25:00 +00:00
|
|
|
if !message.Command {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-05-11 16:10:15 +00:00
|
|
|
parts := strings.Fields(message.Body)
|
2016-03-25 16:25:00 +00:00
|
|
|
if len(parts) > 3 && parts[0] == "leftpad" {
|
|
|
|
padchar := parts[1]
|
2016-03-31 03:09:35 +00:00
|
|
|
length, err := strconv.Atoi(parts[2])
|
2016-03-25 16:25:00 +00:00
|
|
|
if err != nil {
|
2019-02-05 15:54:13 +00:00
|
|
|
p.bot.Send(bot.Message, message.Channel, "Invalid padding number")
|
2016-03-25 16:25:00 +00:00
|
|
|
return true
|
|
|
|
}
|
2019-01-22 00:16:57 +00:00
|
|
|
maxLen, who := p.config.GetInt("LeftPad.MaxLen", 50), p.config.Get("LeftPad.Who", "Putin")
|
2019-01-21 19:24:03 +00:00
|
|
|
if length > maxLen && maxLen > 0 {
|
2019-01-22 00:16:57 +00:00
|
|
|
msg := fmt.Sprintf("%s would kill me if I did that.", who)
|
2019-02-05 15:54:13 +00:00
|
|
|
p.bot.Send(bot.Message, message.Channel, msg)
|
2016-04-01 14:37:44 +00:00
|
|
|
return true
|
|
|
|
}
|
2016-03-31 03:09:35 +00:00
|
|
|
text := strings.Join(parts[3:], " ")
|
|
|
|
|
|
|
|
res := leftpad.LeftPad(text, length, padchar)
|
|
|
|
|
2019-02-05 15:54:13 +00:00
|
|
|
p.bot.Send(bot.Message, message.Channel, res)
|
2016-03-25 16:25:00 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LeftpadPlugin) RegisterWeb() *string {
|
|
|
|
// nothing to register
|
|
|
|
return nil
|
|
|
|
}
|