-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.go
336 lines (306 loc) · 8.63 KB
/
commands.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package main
import (
"bufio"
"fmt"
"log"
"math/rand"
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"
)
var (
botCommandRe = regexp.MustCompile(`^\.(\w+)\s*(.*)$`)
botCommands = map[string]*botCommand{}
botGlobalCooldown = map[string]*botCommandGlobalCooldown{}
botGlobalCooldownMu sync.Mutex
botCooldown = map[string]*botCommandCooldown{}
botCooldownMu sync.Mutex
textfiles = map[string][]string{}
textfilesMu sync.Mutex
)
type botCommand struct {
admin bool
fn func(who, arg, nick string)
}
type botCommandGlobalCooldown struct {
amount time.Duration
last time.Time
}
type botCommandCooldown struct {
amount time.Duration
users map[string]time.Time
}
func setGlobalCooldown(command string, amt time.Duration) {
botGlobalCooldownMu.Lock()
defer botGlobalCooldownMu.Unlock()
botGlobalCooldown[command] = &botCommandGlobalCooldown{
amount: amt,
}
}
func setCooldown(command string, amt time.Duration) {
botCooldownMu.Lock()
defer botCooldownMu.Unlock()
botCooldown[command] = &botCommandCooldown{
amount: amt,
users: map[string]time.Time{},
}
}
func globalCooldown(command string) bool {
botGlobalCooldownMu.Lock()
defer botGlobalCooldownMu.Unlock()
cd, ok := botGlobalCooldown[command]
if !ok {
log.Println("warning:", command, "has no cooldown time defined but globalCooldown() was called")
return false
}
// fmt.Println("///////////////////////////////\n\n\n")
// fmt.Printf("amt: %d, last: %s\nnow: %s\nsince: %s", cd.amount, cd.last, time.Now(), time.Since(cd.last))
// fmt.Println("///////////////////////////////\n\n\n")
if time.Since(cd.last) >= cd.amount {
botGlobalCooldown[command].last = time.Now()
return false
}
return true
}
func cooldown(command, nick string) bool {
botCooldownMu.Lock()
defer botCooldownMu.Unlock()
cd, ok := botCooldown[command]
if !ok {
log.Println("warning:", command, "has no cooldown time defined but cooldown() was called")
return false
}
last, ok := cd.users[nick]
if !ok || time.Since(last) >= cd.amount {
botCooldown[command].users[nick] = time.Now()
return false
}
return true
}
func init() {
botCommands["bots"] = &botCommand{
false,
func(who, arg, nick string) {
sendMessage(who, "Reporting in! [ "+colorString("Go!", Cyan)+" ] https://github.com/bheru27/tsobot")
},
}
botCommands["join"] = &botCommand{
true,
func(who, arg, nick string) {
for _, ch := range strings.Split(arg, " ") {
irc.Join(ch)
}
},
}
botCommands["part"] = &botCommand{
true,
func(who, arg, nick string) {
irc.Part(who, arg)
},
}
botCommands["trans"] = &botCommand{
false,
func(who, arg, nick string) {
arg = strings.Replace(arg, "/", "", -1)
sendMessage(who, translate(arg))
},
}
botCommands["addquote"] = &botCommand{
false,
func(who, arg, nick string) {
if !strings.HasPrefix(who, "#") {
return
}
sendMessage(who, addQuote(who, arg, nick))
},
}
botCommands["quote"] = &botCommand{
false,
func(who, arg, nick string) {
if !strings.HasPrefix(who, "#") {
return
}
user := strings.SplitN(arg, " ", 1)[0]
user = strings.TrimSpace(user)
if user == "" {
sendMessage(who, getRandQuote(who))
} else {
sendMessage(who, getQuote(who, user))
}
},
}
setCooldown("addpoint", time.Second*30)
botCommands["addpoint"] = &botCommand{
false,
func(who, arg, nick string) {
if cooldown("addpoint", nick) {
return
}
if !strings.HasPrefix(who, "#") {
return
}
user := strings.SplitN(arg, " ", 2)[0]
user = strings.TrimSpace(user)
if user == "" || strings.ToLower(user) == strings.ToLower(nick) {
return
}
sb.AddPoint(user)
sendMessage(who, sb.Score(user).String())
},
}
setCooldown("rmpoint", time.Second*30)
botCommands["rmpoint"] = &botCommand{
false,
func(who, arg, nick string) {
if cooldown("rmpoint", nick) {
return
}
if !strings.HasPrefix(who, "#") {
return
}
user := strings.SplitN(arg, " ", 1)[0]
user = strings.TrimSpace(user)
if user == "" || strings.ToLower(user) == strings.ToLower(nick) {
return
}
sb.RmPoint(user)
sendMessage(who, sb.Score(user).String())
},
}
setCooldown("score", time.Second*15)
botCommands["score"] = &botCommand{
false,
func(who, arg, nick string) {
if cooldown("score", nick) {
return
}
s := sb.Score(nick)
msg := s.String()
if s.Rank == 1 {
msg = msg + rainbowText(" ~ H I G H S C O R E ~")
}
sendMessage(who, msg)
},
}
setGlobalCooldown("scores", time.Second*30)
botCommands["scores"] = &botCommand{
false,
func(who, arg, nick string) {
if globalCooldown("scores") {
return
}
highscores := sb.HighScores()
msg := []string{}
for _, s := range highscores {
if s == nil {
break
}
msg = append(msg, "_"+s.String())
}
if len(msg) == 0 {
sendMessage(who, "(scoreboard is empty)")
return
}
sendMessage(who, strings.Join(msg, " "))
},
}
botCommands["askhn"] = &botCommand{false, func(who, arg, nick string) { sendMessage(who, hn("ask")) }}
botCommands["showhn"] = &botCommand{false, func(who, arg, nick string) { sendMessage(who, hn("show")) }}
botCommands["tophn"] = &botCommand{false, func(who, arg, nick string) { sendMessage(who, hn("top")) }}
botCommands["besthn"] = &botCommand{false, func(who, arg, nick string) { sendMessage(who, hn("best")) }}
botCommands["newhn"] = &botCommand{false, func(who, arg, nick string) { sendMessage(who, hn("new")) }}
botCommands["goodpost"] = &botCommand{false, func(who, arg, nick string) { sendMessage(who, lobsters()) }}
botCommands["w"] = &botCommand{
false,
func(who, arg, nick string) {
args := strings.Split(arg, " ")
freedom := false
if len(args) > 1 {
switch args[0] {
case "-f", "-F", "--fahrenheit", "--freedom-units":
freedom = true
args = args[1:]
}
}
sendMessage(who, wttr(strings.Join(args, " "), freedom))
},
}
botCommands["ngate"] = &botCommand{
false,
func(who, arg, nick string) {
snark, isNew := ngate()
sendMessage(who, snark)
if isNew {
<-time.After(time.Second * 5)
sendMessage(who, rainbowText("new n-gate available")+": http://n-gate.com/")
}
},
}
botCommands["todo"] = &botCommand{true, func(who, arg, nick string) {
arg = strings.TrimSpace(arg)
if arg == "" {
for i, item := range listTodo() {
sendMessage(nick, fmt.Sprintf("%d: %s", i, item))
}
} else {
sendMessage(who, addTodo(arg))
}
}}
botCommands["done"] = &botCommand{true, func(who, arg, nick string) {
arg = strings.TrimSpace(arg)
idx, _ := strconv.Atoi(arg)
sendMessage(who, removeTodo(idx))
}}
botCommands["shrug"] = &botCommand{false, func(who, arg, nick string) { sendMessage(who, `¯\_(ツ)_/¯`) }}
botCommands["smug"] = &botCommand{false, func(who, arg, nick string) { sendMessage(who, `( ≖‿≖)`) }}
botCommands["denko"] = &botCommand{false, func(who, arg, nick string) { sendMessage(who, "(´・ω・`)") }}
botCommands["noided"] = &botCommand{false, func(who, arg, nick string) { sendMessage(who, randomChoice("grips")) }}
botCommands["spooky"] = &botCommand{false, func(who, arg, nick string) { sendMessage(who, randomChoice("spooks")) }}
botCommands["zerowing"] = &botCommand{false, func(who, arg, nick string) { sendMessage(who, randomChoice("zerowing")) }}
botCommands["schizopost"] = &botCommand{false, func(who, arg, nick string) { sendMessage(who, randomChoice("pawel")) }}
botCommands["cite"] = &botCommand{false, func(who, arg, nick string) { sendMessage(who, "\x1d\x0312[citation needed]") }}
botCommands["wake"] = &botCommand{false, func(who, arg, nick string) {
if arg == "" {
sendMessage(who, "me up inside")
} else if arg == "me up inside" {
sendMessage(who, "(can't wake up)")
go func() {
<-time.After(time.Second * 30)
sendMessage(who, "call my name and save me from the dark")
<-time.After(time.Second * 10)
sendMessage(who, "bid my blood to run")
<-time.After(time.Second * 5)
sendMessage(who, "before I come undone")
<-time.After(time.Second)
sendMessage(who, colorString("SAVE ME", Red, Black))
<-time.After(time.Second * 3)
sendMessage(who, colorString("Save me from the nothing I've become~", Red, Black))
}()
}
}}
}
func randomChoice(filename string) string {
textfilesMu.Lock()
defer textfilesMu.Unlock()
lines, ok := textfiles[filename]
if !ok {
f, err := os.Open("texts/" + filename + ".txt")
if err != nil {
return err.Error()
}
lines = []string{}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
ln := strings.TrimSpace(scanner.Text())
if ln != "" {
lines = append(lines, ln)
}
}
textfiles[filename] = lines
}
rand.Seed(time.Now().UnixNano())
return lines[rand.Intn(len(lines))]
}