summaryrefslogtreecommitdiffstats
path: root/bot/papiez.go
blob: ca573f006c6160b80de9f0851b145a2984acf639 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2015 Robert S. Gerus. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package bot

import (
	"log"
	"math/rand"
	"strings"
	"sync"
	"time"

	cfg "github.com/arachnist/gorepost/config"
	"github.com/arachnist/gorepost/irc"
)

var adjectives []string
var papiezLock sync.RWMutex

func papiez(output func(irc.Message), msg irc.Message) {
	args := strings.Split(msg.Trailing, " ")
	if args[0] != ":papiez" && args[0] != ":papież" {
		return
	}

	papiezLock.RLock()
	defer papiezLock.RUnlock()

	choice := "Papież " + adjectives[rand.Intn(len(adjectives))]

	output(reply(msg, choice))
}

func lazyPapiezInit() {
	defer papiezLock.Unlock()
	var err error
	rand.Seed(time.Now().UnixNano())
	adjectives, err = readLines(cfg.LookupString(nil, "DictionaryAdjectives"))
	if err != nil {
		log.Println("failed to read adjectives", err)
		return
	}
	addCallback("PRIVMSG", "papiez", papiez)
}

func init() {
	papiezLock.Lock()
	log.Println("Defering \"papiez\" initialization")
	go lazyPapiezInit()
}