From 2b9cdda0191b994f467a4bcc0adb6551706a6f96 Mon Sep 17 00:00:00 2001 From: skkiesel Date: Mon, 13 Mar 2017 13:44:44 -0400 Subject: [PATCH] move the specification of replacements into the config file --- config/config.go | 11 +++--- example_config.json | 9 +++-- plugins/your/your.go | 63 +++++++--------------------------- plugins/your/your_test.go | 71 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 60 deletions(-) create mode 100644 plugins/your/your_test.go diff --git a/config/config.go b/config/config.go index 7c98730..bb13f44 100644 --- a/config/config.go +++ b/config/config.go @@ -54,11 +54,8 @@ type Config struct { Hosts []string } Your struct { - YourChance float64 - FuckingChance float64 - DuckingChance float64 - NegativeChance float64 MaxLength int + Replacements []Replacement } LeftPad struct { MaxLen int @@ -75,6 +72,12 @@ type Config struct { } } +type Replacement struct { + This string + That string + Frequency float64 +} + // Readconfig loads the config data out of a JSON file located in cfile func Readconfig(version, cfile string) *Config { fmt.Printf("Using %s as config file.\n", cfile) diff --git a/example_config.json b/example_config.json index 4774f6c..b7e373a 100644 --- a/example_config.json +++ b/example_config.json @@ -45,11 +45,10 @@ "Hosts": [] }, "Your": { - "YourChance": 0.4, - "FuckingChance": 0.15, - "DuckingChance": 0.5, - "NegativeChance": 0.5, - "MaxLength": 140 + "MaxLength": 140, + "Replacements": [ + { "This": "this", "That": "that", "Frequency": 1.0 }, + ] }, "LeftPad": { "MaxLen": 50, diff --git a/plugins/your/your.go b/plugins/your/your.go index 50a36a2..9100d37 100644 --- a/plugins/your/your.go +++ b/plugins/your/your.go @@ -3,17 +3,18 @@ package your import ( - "log" "math/rand" "strings" "time" "github.com/velour/catbase/bot" "github.com/velour/catbase/bot/msg" + "github.com/velour/catbase/config" ) type YourPlugin struct { bot bot.Bot + config *config.Config } // NewYourPlugin creates a new YourPlugin with the Plugin interface @@ -21,6 +22,7 @@ func New(bot bot.Bot) *YourPlugin { rand.Seed(time.Now().Unix()) return &YourPlugin{ bot: bot, + config: bot.Config(), } } @@ -28,60 +30,19 @@ func New(bot bot.Bot) *YourPlugin { // This function returns true if the plugin responds in a meaningful way to the users message. // Otherwise, the function returns false and the bot continues execution of other plugins. func (p *YourPlugin) Message(message msg.Message) bool { - lower := strings.ToLower(message.Body) - config := p.bot.Config().Your - if len(message.Body) > config.MaxLength { + if len(message.Body) > p.config.Your.MaxLength { return false } - - if strings.Contains(message.Body, "the fucking") { // let's not mess with case - log.Println("Found a fucking") - if rand.Float64() < config.FuckingChance { - log.Println("Replacing a fucking") - r := strings.NewReplacer("the fucking", "fucking the") - msg := r.Replace(message.Body) - p.bot.SendMessage(message.Channel, msg) - return true + msg := message.Body + for _, replacement := range p.config.Your.Replacements { + if rand.Float64() < replacement.Frequency { + r := strings.NewReplacer(replacement.This, replacement.That) + msg = r.Replace(msg) } } - if strings.Contains(message.Body, "ducking") || strings.Contains(message.Body, "fucking") { // let's not mess with case - log.Println("Found a fucking|ducking") - if rand.Float64() < config.DuckingChance { - log.Println("Replacing a ducking") - r := strings.NewReplacer("fucking", "ducking", "ducking", "fucking") - msg := r.Replace(message.Body) - p.bot.SendMessage(message.Channel, msg) - return true - } - } - if strings.Contains(lower, "your") || strings.Contains(lower, "you're") { - if rand.Float64() < config.YourChance { - r := strings.NewReplacer("Your", "You're", "your", "you're", "You're", - "Your", "you're", "your", "Youre", "Your", "youre", "your") - msg := r.Replace(message.Body) - p.bot.SendMessage(message.Channel, msg) - return true - } - } - if strings.Contains(message.Body, " is ") { - log.Println("Found an is") - if rand.Float64() < config.NegativeChance { - log.Println("Replacing an is") - r := strings.NewReplacer(" is ", " is not ") - msg := r.Replace(message.Body) - p.bot.SendMessage(message.Channel, msg) - return true - } - } - if strings.Contains(message.Body, " are ") { - log.Println("Found an are") - if rand.Float64() < config.NegativeChance { - log.Println("Replacing an are") - r := strings.NewReplacer(" are ", " are not ") - msg := r.Replace(message.Body) - p.bot.SendMessage(message.Channel, msg) - return true - } + if msg != message.Body { + p.bot.SendMessage(message.Channel, msg) + return true } return false } diff --git a/plugins/your/your_test.go b/plugins/your/your_test.go new file mode 100644 index 0000000..01d3f4f --- /dev/null +++ b/plugins/your/your_test.go @@ -0,0 +1,71 @@ +// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors. + +package your + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/velour/catbase/bot" + "github.com/velour/catbase/bot/msg" + "github.com/velour/catbase/bot/user" + "github.com/velour/catbase/config" +) + +func makeMessage(payload string) msg.Message { + isCmd := strings.HasPrefix(payload, "!") + if isCmd { + payload = payload[1:] + } + return msg.Message{ + User: &user.User{Name: "tester"}, + Channel: "test", + Body: payload, + Command: isCmd, + } +} + +func TestReplacement(t *testing.T) { + mb := bot.NewMockBot() + c := New(mb) + assert.NotNil(t, c) + c.config.Your.MaxLength = 1000 + c.config.Your.Replacements = []config.Replacement{ + config.Replacement{ + This: "fuck", + That: "duck", + Frequency: 1.0, + }, + } + res := c.Message(makeMessage("fuck a duck")) + assert.Len(t, mb.Messages, 1) + assert.True(t, res) + assert.Contains(t, mb.Messages[0], "duck a duck") +} + +func TestNoReplacement(t *testing.T) { + mb := bot.NewMockBot() + c := New(mb) + assert.NotNil(t, c) + c.config.Your.MaxLength = 1000 + c.config.Your.Replacements = []config.Replacement{ + config.Replacement{ + This: "nope", + That: "duck", + Frequency: 1.0, + }, + config.Replacement{ + This: " fuck", + That: "duck", + Frequency: 1.0, + }, + config.Replacement{ + This: "Fuck", + That: "duck", + Frequency: 1.0, + }, + } + c.Message(makeMessage("fuck a duck")) + assert.Len(t, mb.Messages, 0) +}