From f2f1326a190f1ea1cd85b6ac8cceddfa027c0cf1 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Fri, 1 Apr 2016 10:45:45 -0400 Subject: [PATCH] Config-ize factoid --- config/config.go | 9 ++++++--- example_config.json | 10 ++++++---- plugins/fact/factoid.go | 9 +++++---- plugins/fact/remember.go | 4 ++-- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/config/config.go b/config/config.go index 919fa70..df4ee2b 100644 --- a/config/config.go +++ b/config/config.go @@ -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 diff --git a/example_config.json b/example_config.json index bb76074..08eb01b 100644 --- a/example_config.json +++ b/example_config.json @@ -18,8 +18,6 @@ "FullName": "CatBase", "CommandChar": "!", "RatePerSec": 10.0, - "QuoteChance": 0.99, - "QuoteTime": 1, "LogLength": 50, "Admins": [""], "HttpAddr": "127.0.0.1:1337", @@ -35,8 +33,6 @@ "TwitterUserKey": "", "TwitterUserSecret": "", - "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 } } diff --git a/plugins/fact/factoid.go b/plugins/fact/factoid.go index 60c9730..35c528b 100644 --- a/plugins/fact/factoid.go +++ b/plugins/fact/factoid.go @@ -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() diff --git a/plugins/fact/remember.go b/plugins/fact/remember.go index 8039972..74f6024 100644 --- a/plugins/fact/remember.go +++ b/plugins/fact/remember.go @@ -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)