Config-ize factoid

This commit is contained in:
Chris Sexton 2016-04-01 10:45:45 -04:00
parent a63c22c00e
commit f2f1326a19
4 changed files with 19 additions and 13 deletions

View File

@ -29,8 +29,6 @@ type Config struct {
Version string
CommandChar string
RatePerSec float64
QuoteChance float64
QuoteTime int
LogLength int
Admins []string
HttpAddr string
@ -45,7 +43,6 @@ type Config struct {
TwitterConsumerSecret string
TwitterUserKey string
TwitterUserSecret string
StartupFact string
BadMsgs []string
Bad struct {
Msgs []string
@ -61,6 +58,12 @@ type Config struct {
MaxLen int
Who string
}
Factoid struct {
MinLen int
QuoteChance float64
QuoteTime int
StartupFact string
}
}
// Readconfig loads the config data out of a JSON file located in cfile

View File

@ -18,8 +18,6 @@
"FullName": "CatBase",
"CommandChar": "!",
"RatePerSec": 10.0,
"QuoteChance": 0.99,
"QuoteTime": 1,
"LogLength": 50,
"Admins": ["<Admin Nick>"],
"HttpAddr": "127.0.0.1:1337",
@ -35,8 +33,6 @@
"TwitterUserKey": "<User Key>",
"TwitterUserSecret": "<User Secret>",
"StartupFact": "speed test",
"WelcomeMsgs": [
"Real men use screen, %s.",
"Joins upset the hivemind's OCD, %s.",
@ -56,5 +52,11 @@
"LeftPad": {
"MaxLen": 50,
"Who": "person"
},
"Factoid": {
"QuoteChance": 0.99,
"QuoteTime": 1,
"StartupFact": "speed test",
"MinLen": 5
}
}

View File

@ -238,7 +238,7 @@ func New(botInst bot.Bot) *FactoidPlugin {
go func(ch string) {
// Some random time to start up
time.Sleep(time.Duration(15) * time.Second)
if ok, fact := p.findTrigger(p.Bot.Config().StartupFact); ok {
if ok, fact := p.findTrigger(p.Bot.Config().Factoid.StartupFact); ok {
p.sayFact(msg.Message{
Channel: ch,
Body: "speed test", // BUG: This is defined in the config too
@ -355,7 +355,8 @@ func (p *FactoidPlugin) sayFact(message msg.Message, fact factoid) {
// trigger checks the message for its fitness to be a factoid and then hauls
// the message off to sayFact for processing if it is in fact a trigger
func (p *FactoidPlugin) trigger(message msg.Message) bool {
if len(message.Body) > 4 || message.Command || message.Body == "..." {
minLen := p.Bot.Config().Factoid.MinLen
if len(message.Body) > minLen || message.Command || message.Body == "..." {
if ok, fact := p.findTrigger(message.Body); ok {
p.sayFact(message, *fact)
return true
@ -597,7 +598,7 @@ func (p *FactoidPlugin) randomFact() *factoid {
// factTimer spits out a fact at a given interval and with given probability
func (p *FactoidPlugin) factTimer(channel string) {
duration := time.Duration(p.Bot.Config().QuoteTime) * time.Minute
duration := time.Duration(p.Bot.Config().Factoid.QuoteTime) * time.Minute
myLastMsg := time.Now()
for {
time.Sleep(time.Duration(5) * time.Second)
@ -610,7 +611,7 @@ func (p *FactoidPlugin) factTimer(channel string) {
tdelta := time.Since(lastmsg.Time)
earlier := time.Since(myLastMsg) > tdelta
chance := rand.Float64()
success := chance < p.Bot.Config().QuoteChance
success := chance < p.Bot.Config().Factoid.QuoteChance
if success && tdelta > duration && earlier {
fact := p.randomFact()

View File

@ -168,8 +168,8 @@ func (p *RememberPlugin) quoteTimer(channel string) {
for {
// this pisses me off: You can't multiply int * time.Duration so it
// has to look ugly as shit.
time.Sleep(time.Duration(p.Bot.Config().QuoteTime) * time.Minute)
chance := 1.0 / p.Bot.Config().QuoteChance
time.Sleep(time.Duration(p.Bot.Config().Factoid.QuoteTime) * time.Minute)
chance := 1.0 / p.Bot.Config().Factoid.QuoteChance
if rand.Intn(int(chance)) == 0 {
msg := p.randQuote()
p.Bot.SendMessage(channel, msg)