Add leftpad limit

This commit is contained in:
Chris Sexton 2016-04-01 10:37:44 -04:00
parent e449a82001
commit a63c22c00e
4 changed files with 42 additions and 2 deletions

View File

@ -57,6 +57,10 @@ type Config struct {
FuckingChance float64
MaxLength int
}
LeftPad struct {
MaxLen int
Who string
}
}
// Readconfig loads the config data out of a JSON file located in cfile

View File

@ -52,5 +52,9 @@
"YourChance": 0.4,
"FuckingChance": 0.15,
"MaxLength": 140
},
"LeftPad": {
"MaxLen": 50,
"Who": "person"
}
}

View File

@ -4,22 +4,26 @@
package leftpad
import (
"fmt"
"strconv"
"strings"
"github.com/jamescun/leftpad"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/config"
)
type LeftpadPlugin struct {
bot bot.Bot
bot bot.Bot
config *config.Config
}
// New creates a new LeftpadPlugin with the Plugin interface
func New(bot bot.Bot) *LeftpadPlugin {
p := LeftpadPlugin{
bot: bot,
bot: bot,
config: bot.Config(),
}
return &p
}
@ -41,6 +45,11 @@ func (p *LeftpadPlugin) Message(message msg.Message) bool {
p.bot.SendMessage(message.Channel, "Invalid padding number")
return true
}
if length > p.config.LeftPad.MaxLen && p.config.LeftPad.MaxLen > 0 {
msg := fmt.Sprintf("%s would kill me if I did that.", p.config.LeftPad.Who)
p.bot.SendMessage(message.Channel, msg)
return true
}
text := strings.Join(parts[3:], " ")
res := leftpad.LeftPad(text, length, padchar)

View File

@ -54,6 +54,29 @@ func TestNotCommand(t *testing.T) {
assert.Len(t, mb.Messages, 0)
}
func TestNoMaxLen(t *testing.T) {
p, mb := makePlugin(t)
p.Message(makeMessage("!leftpad dicks 100 dicks"))
assert.Len(t, mb.Messages, 1)
assert.Contains(t, mb.Messages[0], "dicks")
}
func Test50Padding(t *testing.T) {
p, mb := makePlugin(t)
p.config.LeftPad.MaxLen = 50
p.Message(makeMessage("!leftpad dicks 100 dicks"))
assert.Len(t, mb.Messages, 1)
assert.Contains(t, mb.Messages[0], "kill me")
}
func TestUnder50Padding(t *testing.T) {
p, mb := makePlugin(t)
p.config.LeftPad.MaxLen = 50
p.Message(makeMessage("!leftpad dicks 49 dicks"))
assert.Len(t, mb.Messages, 1)
assert.Contains(t, mb.Messages[0], "dicks")
}
func TestNotPadding(t *testing.T) {
p, mb := makePlugin(t)
p.Message(makeMessage("!lololol"))