diff options
author | Robert Gerus <rgerus@dpd.com.pl> | 2015-12-09 09:01:22 +0100 |
---|---|---|
committer | Robert Gerus <rgerus@dpd.com.pl> | 2015-12-09 09:01:22 +0100 |
commit | dbe204ba5cd80466808101e2ae95ed84813453da (patch) | |
tree | a9dbaa1ade5b395aeaa3dc9d6da990f944a859a1 | |
parent | 3d21ac9c7bd33a37089d7ee7b18c8f5caf26e5c1 (diff) | |
download | gorepost-dbe204ba5cd80466808101e2ae95ed84813453da.tar.gz gorepost-dbe204ba5cd80466808101e2ae95ed84813453da.tar.bz2 gorepost-dbe204ba5cd80466808101e2ae95ed84813453da.tar.xz gorepost-dbe204ba5cd80466808101e2ae95ed84813453da.zip |
Dictionary for "papież" is now configurable.configurable-file-paths
-rw-r--r-- | bot/.dicts/adjectives | 1 | ||||
-rw-r--r-- | bot/.testconfig.json | 1 | ||||
-rw-r--r-- | bot/papiez.go | 23 | ||||
-rw-r--r-- | bot/plugins_test.go | 20 |
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", |