catbase/plugins/leftpad/leftpad.go

70 lines
1.3 KiB
Go
Raw 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 (
"strconv"
2016-03-25 16:25:00 +00:00
"strings"
"github.com/jamescun/leftpad"
2016-03-25 16:25:00 +00:00
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
2016-03-25 16:25:00 +00:00
)
type LeftpadPlugin struct {
bot bot.Bot
2016-03-25 16:25:00 +00:00
}
// New creates a new LeftpadPlugin with the Plugin interface
func New(bot bot.Bot) *LeftpadPlugin {
2016-03-25 16:25:00 +00:00
p := LeftpadPlugin{
bot: bot,
}
return &p
}
type leftpadResp struct {
Str string
}
func (p *LeftpadPlugin) Message(message msg.Message) bool {
2016-03-25 16:25:00 +00:00
if !message.Command {
return false
}
parts := strings.Split(message.Body, " ")
if len(parts) > 3 && parts[0] == "leftpad" {
padchar := parts[1]
length, err := strconv.Atoi(parts[2])
2016-03-25 16:25:00 +00:00
if err != nil {
p.bot.SendMessage(message.Channel, "Invalid padding number")
2016-03-25 16:25:00 +00:00
return true
}
text := strings.Join(parts[3:], " ")
res := leftpad.LeftPad(text, length, padchar)
p.bot.SendMessage(message.Channel, res)
2016-03-25 16:25:00 +00:00
return true
}
return false
}
func (p *LeftpadPlugin) Event(e string, message msg.Message) bool {
2016-03-25 16:25:00 +00:00
return false
}
func (p *LeftpadPlugin) BotMessage(message msg.Message) bool {
2016-03-25 16:25:00 +00:00
return false
}
func (p *LeftpadPlugin) Help(e string, m []string) {
}
func (p *LeftpadPlugin) RegisterWeb() *string {
// nothing to register
return nil
}