From a63c22c00e055350dbe88a66d1c0ed466b85f3ea Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Fri, 1 Apr 2016 10:37:44 -0400 Subject: [PATCH] Add leftpad limit --- config/config.go | 4 ++++ example_config.json | 4 ++++ plugins/leftpad/leftpad.go | 13 +++++++++++-- plugins/leftpad/leftpad_test.go | 23 +++++++++++++++++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index e4c68b3..919fa70 100644 --- a/config/config.go +++ b/config/config.go @@ -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 diff --git a/example_config.json b/example_config.json index a7e4a60..bb76074 100644 --- a/example_config.json +++ b/example_config.json @@ -52,5 +52,9 @@ "YourChance": 0.4, "FuckingChance": 0.15, "MaxLength": 140 + }, + "LeftPad": { + "MaxLen": 50, + "Who": "person" } } diff --git a/plugins/leftpad/leftpad.go b/plugins/leftpad/leftpad.go index 975454a..338d704 100644 --- a/plugins/leftpad/leftpad.go +++ b/plugins/leftpad/leftpad.go @@ -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) diff --git a/plugins/leftpad/leftpad_test.go b/plugins/leftpad/leftpad_test.go index efef7a6..805f8f1 100644 --- a/plugins/leftpad/leftpad_test.go +++ b/plugins/leftpad/leftpad_test.go @@ -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"))