summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bot/.dicts/adjectives1
-rw-r--r--bot/.testconfig.json1
-rw-r--r--bot/papiez.go23
-rw-r--r--bot/plugins_test.go20
4 files changed, 40 insertions, 5 deletions
diff --git a/bot/.dicts/adjectives b/bot/.dicts/adjectives
new file mode 100644
index 0000000..3c1d819
--- /dev/null
+++ b/bot/.dicts/adjectives
@@ -0,0 +1 @@
+adjective
diff --git a/bot/.testconfig.json b/bot/.testconfig.json
index 84df936..1fdd524 100644
--- a/bot/.testconfig.json
+++ b/bot/.testconfig.json
@@ -11,5 +11,6 @@
"NotSeenMessage":"nope, never",
"DictionaryObjects":".dicts/objects",
"DictionaryVerbs":".dicts/verbs",
+ "DictionaryAdjectives":".dicts/adjectives",
"Nick":"gorepost"
}
diff --git a/bot/papiez.go b/bot/papiez.go
index 1f028b8..ca573f0 100644
--- a/bot/papiez.go
+++ b/bot/papiez.go
@@ -8,12 +8,15 @@ import (
"log"
"math/rand"
"strings"
+ "sync"
"time"
+ cfg "github.com/arachnist/gorepost/config"
"github.com/arachnist/gorepost/irc"
)
-var adverbs []string
+var adjectives []string
+var papiezLock sync.RWMutex
func papiez(output func(irc.Message), msg irc.Message) {
args := strings.Split(msg.Trailing, " ")
@@ -21,18 +24,28 @@ func papiez(output func(irc.Message), msg irc.Message) {
return
}
- choice := "Papież " + adverbs[rand.Intn(len(adverbs))]
+ papiezLock.RLock()
+ defer papiezLock.RUnlock()
+
+ choice := "Papież " + adjectives[rand.Intn(len(adjectives))]
output(reply(msg, choice))
}
-func init() {
+func lazyPapiezInit() {
+ defer papiezLock.Unlock()
var err error
rand.Seed(time.Now().UnixNano())
- adverbs, err = readLines("/home/repost/przymiotniki")
+ adjectives, err = readLines(cfg.LookupString(nil, "DictionaryAdjectives"))
if err != nil {
- log.Println("failed to read adverbs", err)
+ log.Println("failed to read adjectives", err)
return
}
addCallback("PRIVMSG", "papiez", papiez)
}
+
+func init() {
+ papiezLock.Lock()
+ log.Println("Defering \"papiez\" initialization")
+ go lazyPapiezInit()
+}
diff --git a/bot/plugins_test.go b/bot/plugins_test.go
index 04129c9..a9a0eff 100644
--- a/bot/plugins_test.go
+++ b/bot/plugins_test.go
@@ -258,6 +258,26 @@ var eventTests = []struct {
},
},
{
+ desc: "papież",
+ in: irc.Message{
+ Command: "PRIVMSG",
+ Trailing: ":papież",
+ Params: []string{"#testchan-1"},
+ Prefix: &irc.Prefix{
+ Name: "idontexist",
+ User: "test",
+ Host: "framework",
+ },
+ },
+ expectedOut: []irc.Message{
+ {
+ Command: "PRIVMSG",
+ Params: []string{"#testchan-1"},
+ Trailing: "Papież adjective",
+ },
+ },
+ },
+ {
desc: "jan without args",
in: irc.Message{
Command: "PRIVMSG",