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 Hosts []string
} }
Your struct { Your struct {
YourChance float64
FuckingChance float64
DuckingChance float64
MaxLength int MaxLength int
Replacements []Replacement
} }
LeftPad struct { LeftPad struct {
MaxLen int 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 // Readconfig loads the config data out of a JSON file located in cfile
func Readconfig(version, cfile string) *Config { func Readconfig(version, cfile string) *Config {
fmt.Printf("Using %s as config file.\n", cfile) fmt.Printf("Using %s as config file.\n", cfile)

View File

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

View File

@ -3,17 +3,18 @@
package your package your
import ( import (
"log"
"math/rand" "math/rand"
"strings" "strings"
"time" "time"
"github.com/velour/catbase/bot" "github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg" "github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/config"
) )
type YourPlugin struct { type YourPlugin struct {
bot bot.Bot bot bot.Bot
config *config.Config
} }
// NewYourPlugin creates a new YourPlugin with the Plugin interface // NewYourPlugin creates a new YourPlugin with the Plugin interface
@ -21,6 +22,7 @@ func New(bot bot.Bot) *YourPlugin {
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
return &YourPlugin{ return &YourPlugin{
bot: bot, bot: bot,
config: bot.Config(),
} }
} }
@ -28,40 +30,19 @@ func New(bot bot.Bot) *YourPlugin {
// This function returns true if the plugin responds in a meaningful way to the users message. // 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. // Otherwise, the function returns false and the bot continues execution of other plugins.
func (p *YourPlugin) Message(message msg.Message) bool { func (p *YourPlugin) Message(message msg.Message) bool {
lower := strings.ToLower(message.Body) if len(message.Body) > p.config.Your.MaxLength {
config := p.bot.Config().Your
if len(message.Body) > config.MaxLength {
return false return false
} }
msg := message.Body
if strings.Contains(message.Body, "the fucking") { // let's not mess with case for _, replacement := range p.config.Your.Replacements {
log.Println("Found a fucking") if rand.Float64() < replacement.Frequency {
if rand.Float64() < config.FuckingChance { r := strings.NewReplacer(replacement.This, replacement.That)
log.Println("Replacing a fucking") msg = r.Replace(msg)
r := strings.NewReplacer("the fucking", "fucking the")
msg := r.Replace(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 if msg != message.Body {
log.Println("Found a fucking|ducking") p.bot.SendMessage(message.Channel, msg)
if rand.Float64() < config.DuckingChance { return true
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 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)
}