-
Notifications
You must be signed in to change notification settings - Fork 1
/
quote.go
145 lines (124 loc) · 2.96 KB
/
quote.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"encoding/json"
"math/rand"
"regexp"
"strings"
"sync"
)
var (
quotes = map[string]map[string][]string{}
quotesMu sync.Mutex
nobracketsRe = regexp.MustCompile(`[^a-z0-9-_]`)
)
func loadQuotes(filename string) {
quotesMu.Lock()
defer quotesMu.Unlock()
if fileExists(filename) {
data := fileGetContents(filename)
checkErr(json.Unmarshal(data, "es))
}
}
func saveQuotes(filename string) {
quotesMu.Lock()
defer quotesMu.Unlock()
data, err := json.Marshal(quotes)
checkErr(err)
filePutContents(filename, data)
}
func addQuote(channel, arg, nick string) string {
args := strings.SplitN(strings.TrimSpace(arg), " ", 2)
src := args[0]
src = strings.TrimSpace(nobracketsRe.ReplaceAllString(strings.ToLower(src), ""))
if src == "" {
return "usage: .addquote [nick] [message (optional)]"
}
q := ""
if src == strings.ToLower(nick) {
return "don't quote yourself you narcissistic twat"
}
// disabling this but not removing completely
//if src == "tso" {
// return "tso never said that"
//}
if len(args) > 1 {
quote := args[1]
chatlogMu.Lock()
defer chatlogMu.Unlock()
if logs, ok := chatlogs[channel]; ok {
for _, ln := range logs {
if ln == nil {
break
}
if ln[0] == src {
if strings.TrimSpace(ln[1]) == quote {
q = quote
goto here
}
}
}
}
// shitlistMu.Lock()
// shitlist = append(shitlist, strings.ToLower(nick))
// shitlistMu.Unlock()
return src + " never said that" //, " + nick + " is commiting slanderous libel and treason and has been permanently prevented from interacting with this bot EVER AGAIN."
} else {
chatlogMu.Lock()
defer chatlogMu.Unlock()
if logs, ok := chatlogs[channel]; ok {
for _, ln := range logs {
if ln == nil {
break
}
if ln[0] == src {
q = ln[1]
goto here
}
}
return "(no quotes from " + src + " in chatlog)"
}
return "(no quotes from " + channel + " in chatlog)"
}
here:
quotesMu.Lock()
defer quotesMu.Unlock()
// I LOVE MAPS SO MUCH
_, ok := quotes[channel]
if !ok {
quotes[channel] = map[string][]string{}
}
// I LOVE MAPS SO MUCH
if _, ok := quotes[channel][src]; !ok {
quotes[channel][src] = []string{}
}
quotes[channel][src] = append(quotes[channel][src], q)
// FUCK
return "oic 📝"
}
func getQuote(channel, src string) string {
src = strings.TrimSpace(nobracketsRe.ReplaceAllString(strings.ToLower(src), ""))
quotesMu.Lock()
defer quotesMu.Unlock()
if _, ok := quotes[channel]; ok {
if q, ok := quotes[channel][src]; ok {
return "<" + src + "> " + q[rand.Intn(len(q))]
}
}
return "i got nothing, sorry"
}
func getRandQuote(channel string) string {
quotesMu.Lock()
defer quotesMu.Unlock()
if _, ok := quotes[channel]; ok {
i := 0
r := rand.Intn(len(quotes[channel]))
for src, q := range quotes[channel] {
if i == r {
return "<" + src + "> " + q[rand.Intn(len(q))]
}
i++
}
return "Something happened. :("
}
return "no really, i got nothing"
}