mirror of https://github.com/velour/catbase.git
move the specification of replacements into the config file
This commit is contained in:
parent
f2309b9090
commit
2b9cdda019
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue