Merge pull request #43 from velour/your_refactor

Your refactor
This commit is contained in:
Chris Sexton 2017-08-30 14:43:26 -04:00 committed by GitHub
commit c157adb3c8
4 changed files with 94 additions and 38 deletions

View File

@ -64,10 +64,8 @@ type Config struct {
Hosts []string
}
Your struct {
YourChance float64
FuckingChance float64
DuckingChance float64
MaxLength int
Replacements []Replacement
}
LeftPad struct {
MaxLen int
@ -113,6 +111,12 @@ func init() {
})
}
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)

View File

@ -45,10 +45,10 @@
"Hosts": []
},
"Your": {
"YourChance": 0.4,
"FuckingChance": 0.15,
"DuckingChance": 0.5,
"MaxLength": 140
"MaxLength": 140,
"Replacements": [
{ "This": "this", "That": "that", "Frequency": 1.0 },
]
},
"LeftPad": {
"MaxLen": 50,

View File

@ -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,41 +30,20 @@ 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)
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 msg != message.Body {
p.bot.SendMessage(message.Channel, msg)
return true
}
}
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
}
}
return false
}

71
plugins/your/your_test.go Normal file
View File

@ -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)
}