-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
614 lines (568 loc) · 18.1 KB
/
main.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
package main
import (
"context"
"db"
"encoding/json"
"errors"
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"time"
"github.com/andersfylling/disgord"
"github.com/andersfylling/disgord/std"
)
var BotID string // loaded on init
var BotPFP string
var BigTypeLetters map[string]map[string]string // this is way easier than the alternative
var ThesaurusLookup map[string][]string
var GRand = rand.New(rand.NewSource(time.Now().UnixNano()))
var DBConn *db.Connection
var slashCommands = []*disgord.CreateApplicationCommand{
{
Name: "help",
Description: "Alternative to @TRAS help, view help command without DMs.",
},
{
Name: "about",
Description: "Alternative to @TRAS about. view about command without a DM.",
},
}
type MessagePassthrough struct {
Message *disgord.Message
Interaction *disgord.InteractionCreate
}
func main() {
// load bigtype letters
bigtypejson, err := os.ReadFile("src/bigtype.json")
if err != nil {
panic(err)
}
err = json.Unmarshal(bigtypejson, &BigTypeLetters)
if err != nil {
panic(err)
}
// load thesaurus
thesarusjson, err := os.ReadFile("src/thesaurus.json")
if err != nil {
panic(err)
}
err = json.Unmarshal(thesarusjson, &ThesaurusLookup)
if err != nil {
panic(err)
}
// connect to DB
DBConn = &db.Connection{
Host: DB_HOST,
Port: DB_PORT,
Password: os.Getenv("DB_PASSWORD"),
DBName: DB_NAME,
}
err = DBConn.Connect()
if err != nil {
panic(err)
}
DBConn.CloseOnInterrupt()
//load client
client := disgord.New(disgord.Config{
BotToken: os.Getenv("Token"),
Intents: disgord.IntentGuildMessages | disgord.IntentDirectMessages |
disgord.IntentGuildMessageReactions | disgord.IntentDirectMessageReactions,
})
defer client.Gateway().StayConnectedUntilInterrupted()
var BotUser *disgord.User
//startup message
client.Gateway().BotReady(func() {
botUser, err := client.CurrentUser().Get()
if err != nil {
panic(err) // Bot shouldn't start
}
BotID = botUser.ID.String()
BotPFP, err = botUser.AvatarURL(256, false)
if err != nil {
panic(err) // Bot shouldn't start
}
for i := range slashCommands {
err = client.ApplicationCommand(0).Global().Create(slashCommands[i])
if err != nil {
panic(err)
}
}
fmt.Println("Bot started @ " + time.Now().Local().Format(time.RFC1123))
client.UpdateStatusString("@me help")
})
//filter out unwanted messages
content, err := std.NewMsgFilter(context.Background(), client)
if err != nil {
panic(err)
}
content.NotByBot(client)
content.ContainsBotMention(client)
//slash command handling
client.Gateway().
InteractionCreate(func(s disgord.Session, h *disgord.InteractionCreate) {
user := h.User
if h.Member != nil {
user = h.Member.User
}
msgConvert := &disgord.Message{
ID: 0, // Tag with no original message
ChannelID: h.ChannelID,
GuildID: h.GuildID,
Author: user,
Member: h.Member,
Content: "<@" + BotID + "> " + h.Data.Name,
Timestamp: disgord.Time{Time: time.Now()},
EditedTimestamp: disgord.Time{Time: time.Now()},
Mentions: []*disgord.User{BotUser},
Type: disgord.MessageTypeApplicationCommand,
}
parseCommand(MessagePassthrough{
Message: msgConvert,
Interaction: h,
}, &s)
if err != nil {
msgerr(err, h.Message, &s)
}
})
//on message with mention
client.Gateway().
WithMiddleware(content.NotByBot, content.NotByWebhook, content.ContainsBotMention, content.HasBotMentionPrefix). // filter
MessageCreate(func(s disgord.Session, evt *disgord.MessageCreate) { // on message
// used for standard message parsing
go parseCommand(MessagePassthrough{Message: evt.Message}, &s)
})
client.Gateway().
WithMiddleware(content.NotByBot, content.NotByWebhook).
MessageCreate(func(s disgord.Session, evt *disgord.MessageCreate) { // on message (any)
if content.ContainsBotMention(evt) != nil { // middleware !content.ContainsBotMention
return
}
// used for ranking and randomspeak
updateMemberProgress(evt.Message)
executeRandSpeakRoll(evt.Message, &s)
})
}
func parseCommand(msgPt MessagePassthrough, s *disgord.Session) {
msg := msgPt.Message
procTimeStart := time.Now() // timer for ping info
cstr := msg.Content
rsplitstr := argumentSplitRegex.ReplaceAllString(cstr, "$1\n") // separate arguments with "\\" and "\"
rfixspacestr := strings.ReplaceAll(rsplitstr, "\\ ", " ") // fix "\ " to " " (this should only happen when that should happen)
rfixslashstr := strings.ReplaceAll(rfixspacestr, "\\\\", "\\") // fix "\\" to "\" (this is somewhat wonky behavior because "\" doesn't do
carr := strings.Split(rfixslashstr, "\n") // anything outside of the spaces so it can still work without disappearing but oh well)
args := []string{}
argsl := []string{}
for i := 1; i < len(carr); i++ { // first argument should always be bot mention
args = append(args, carr[i])
argsl = append(argsl, strings.ToLower(carr[i]))
}
if len(args) < 1 { // prevent error in switch case
args = append(args, "")
argsl = append(argsl, "")
}
// custom commands never override standard
// commands to prevent deadlock
successful_cc := parseCustomCommand(msg, s, argsl[0])
switch argsl[0] {
case "help":
helpResponse(msg, s, msgPt.Interaction)
case "about":
if len(argsl) > 1 && argsl[1] == "nocb" { // remove code blocks so iOS can click the links
aboutResponse(msg, s, true, msgPt.Interaction)
} else {
aboutResponse(msg, s, false, msgPt.Interaction)
}
case "oof":
// big OOF
baseReply(msg, s, "oof oof oof oof oof oof oof oof oof\noof oof oof oof oof\noof oof oof oof oof oof oof\noof oof oof oof oof\noof oof oof oof oof oof oof")
case "f":
// big F
baseReply(msg, s, "F F F F F F\nF F \nF F F F F F\nF F\nF F")
case "pi":
piResponse(msg, s)
case "big":
if len(argsl) > 1 && (argsl[1] == "-t" || argsl[1] == "--thin") {
if len(argsl) > 3 {
text := strings.Join(argsl[3:], " ") // case insensitive
bigTypeRespones(args[2], text, true, msg, s)
} else if len(argsl) == 3 {
bigTypeRespones(args[2], argsl[2], true, msg, s) // word is case sensitive but text is not
} else {
baseReply(msg, s, "I need to know the [word] to enlarge, OR the [word] and [text] to enlarge with it.")
}
} else {
if len(argsl) > 2 {
text := strings.Join(argsl[2:], " ") // case insensitive
bigTypeRespones(args[1], text, false, msg, s)
} else if len(argsl) == 2 {
bigTypeRespones(args[1], argsl[1], false, msg, s) // word is case sensitive but text is not
} else {
baseReply(msg, s, "I need to know the [word] to enlarge, OR the [word] and [text] to enlarge with it.")
}
}
case "jumble":
if len(argsl) > 1 {
jumbleResponse(args[1:], msg, s) // case sensitive, presplit
} else {
baseReply(msg, s, "What should I jumble?")
}
case "emojify":
if len(argsl) > 1 {
text := strings.Join(argsl[1:], " ") // case insensitive
emojifyResponse(text, msg, s)
} else {
baseReply(msg, s, "What would you like me to change to emojis?")
}
case "flagify":
if len(argsl) > 1 {
text := strings.Join(argsl[1:], " ") // case insensitive
flagifyResponse(text, msg, s)
} else {
baseReply(msg, s, "Ya gotta tell me what to flagify!")
}
case "superscript":
if len(argsl) > 1 {
text := strings.Join(args[1:], " ") // case sensitive
superScriptResponse(text, msg, s)
} else {
baseReply(msg, s, "What do you need to be superscripts?")
}
case "unicodify":
if len(argsl) > 1 {
text := strings.Join(args[1:], " ") // case sensitive
unicodifyResponse(text, msg, s)
} else {
baseReply(msg, s, "You need to tell me what to unicodify.")
}
case "bold":
if len(argsl) > 1 {
text := strings.Join(args[1:], " ") // case sensitive
boldResponse(text, msg, s)
} else {
baseReply(msg, s, "What needs bolding?")
}
case "replace", "rep":
if len(argsl) > 3 {
text := strings.Join(args[3:], " ") // case sensitive
replaceResponse(args[1], args[2], text, msg, s)
} else {
baseReply(msg, s, "Tell me the [what to replace], the [replacement], and then provide the [body of text].")
}
case "overcomplicate", "overcomp":
if len(argsl) > 1 {
overcompResponse(args[1:], msg, s) // case sensitive
} else {
baseReply(msg, s, "Whichever lexical constructions shall I reform?")
}
case "word":
if len(argsl) > 1 && argsl[1] == "info" {
if len(argsl) > 2 { // type
if len(argsl) > 3 { // word
word := strings.Join(args[3:], " ")
switch argsl[2] {
case "definition", "definitions", "define", "def", "defs":
wordInfoReply("def", word, msg, s)
case "categories", "category", "cat", "cats", "partofspeech", "pos":
wordInfoReply("cat", word, msg, s)
default:
baseReply(msg, s, "That's not an info type I can provide.")
}
} else {
baseReply(msg, s, "What word do you want info on?\nIf you said a word, you may not have specified definition/pos BEFORE The word.")
}
} else {
baseReply(msg, s, "What type of info do you want? Defintion, or categories?")
}
} else {
defaultResponse(msg, s, successful_cc)
}
case "ascii":
if len(argsl) > 1 && argsl[1] == "art" {
if len(argsl) > 2 {
if argsl[2] == "getfonts" {
asciiGetFonts(msg, s)
} else {
if len(argsl) > 3 {
text := strings.Join(args[3:], " ")
asciiResponse(msg, s, args[2], text, 100) // max width 100, maybe add option in future
} else {
baseReply(msg, s, "What text do you want to be generated?")
}
}
} else {
baseReply(msg, s, "I need to know the [font] and the [text] you want me to use.")
}
} else {
defaultResponse(msg, s, successful_cc)
}
case "commands", "cmds":
if len(argsl) > 1 && (argsl[1] == "view" || argsl[1] == "list") {
handleViewCustomCommands(msg, s)
} else if len(argsl) > 1 && argsl[1] == "manage" {
// check for permissions
perms, err := getPerms(msg, s)
if err != nil {
msgerr(err, msg, s)
return
}
if !hasPerm(perms, disgord.PermissionAdministrator) {
baseReply(msg, s, "You don't have administrator permission. Sorry!")
return
}
// restricted cases
word := ""
if len(argsl) > 2 {
word = argsl[2]
}
switch word {
case "set":
if len(argsl) > 4 {
text := strings.Join(args[4:], " ")
handleSetCustomCommand(msg, s, argsl[3], text)
} else {
baseReply(msg, s, "You need to tell me the [trigger] and [what I should respond with].")
}
case "delete", "del", "remove", "rem", "reset":
if len(argsl) > 3 {
handleDeleteCustomCommand(msg, s, argsl[3])
} else {
baseReply(msg, s, "You need to tell me the [trigger] to delete.")
}
case "schedule":
defaultTODOResponse(msg, s) // TODO: schedule feature
default:
baseReply(msg, s, "The format for manage is `@TRAS commands manage [set/delete/schedule] [(set/delete)trigger//(schedule)time of day (hh:mm:ss)] [(set/schedule)reply]`")
}
} else {
baseReply(msg, s, "Would you like to [view] the commands, or [manage] them as the admin?")
}
case "rank":
if len(argsl) > 1 {
switch argsl[1] {
case "info":
baseReply(msg, s, "TRAS' \"progress\" meter takes various elements of your messages' metadata into account when valuing them.\n"+
"This is a differnet approach than TRAS 2 due to the API changes between the sunset of v2 and the creation of v3.\n"+
"Levels are the logarithm of your \"progress\" to base 2, meaning you require 2 times the \"progress\" per level."+
"\nI included the \"dice roll\" feature as a fun gimmick to portray my thoughts about levels in general - pointless and silly.")
case "checkdice":
statusStr := "OFF"
status, err := getDiceStatus(msg)
if err != nil {
msgerr(err, msg, s)
return
}
if status {
statusStr = "ON"
}
baseReply(msg, s, "Dice rolls are currently "+statusStr)
case "dice":
diceEnabled, err := getDiceStatus(msg)
if err != nil {
msgerr(err, msg, s)
return
}
if diceEnabled {
diceRollResponse(msg, s)
} else {
baseReply(msg, s, "Sorry, dice rolls are currently disabled.\nAsk an admin to `@TRAS rank toggledice` if you want to play dice on this server.")
}
case "set", "reset", "toggledice":
// check for permissions
perms, err := getPerms(msg, s)
if err != nil {
msgerr(err, msg, s)
return
}
if !hasPerm(perms, disgord.PermissionAdministrator) {
baseReply(msg, s, "You don't have administrator permission. Sorry!")
return
}
switch argsl[1] {
case "set":
if len(argsl) > 3 {
user, validMention := extractSnowflake(argsl[2])
if !validMention {
baseReply(msg, s, "That was not a valid user mention.")
return
}
num, err := strconv.Atoi(argsl[3])
if err != nil {
baseReply(msg, s, "Invalid number!")
return
}
err = forceSetUserRank(msg, user, int64(num))
if err != nil {
msgerr(err, msg, s)
return
}
} else {
baseReply(msg, s, "You need to tell me the [user] and the [value] to set the progress to.")
}
case "reset":
if len(argsl) > 2 {
user, validMention := extractSnowflake(argsl[2])
if !validMention {
baseReply(msg, s, "That was not a valid user mention.")
return
}
err := forceSetUserRank(msg, user, 0)
if err != nil {
msgerr(err, msg, s)
return
}
} else {
baseReply(msg, s, "You need to tell me the [user] to reset the progress of.")
}
case "toggledice":
toggleDiceResponse(msg, s)
}
default:
user, validMention := extractSnowflake(argsl[1])
if validMention {
getUserRankInfo(msg, s, user)
return
}
// check for permissions
perms, err := getPerms(msg, s)
if err != nil {
msgerr(err, msg, s)
return
}
helpContent := "The format is `@TRAS rank [info/checkDice/dice] [(info)-real]`"
if hasPerm(perms, disgord.PermissionAdministrator) {
helpContent += "\nThe format for admin controls is `@TRAS rank [set/reset/toggleDice] [(set/reset)user] [value]`"
}
baseReply(msg, s, helpContent)
}
} else {
getUserRankInfo(msg, s, msg.Author.ID)
}
case "set":
if len(argsl) > 1 && (argsl[1] == "nickname" || argsl[1] == "nick") {
text := strings.Join(args[2:], " ") // case sensitive
if len(argsl) > 2 {
setNickResponse(text, msg, s) // checks permissions internally
} else {
baseReply(msg, s, "What should by nickname be?")
}
} else {
defaultResponse(msg, s, successful_cc)
}
case "reset":
if len(argsl) > 1 && (argsl[1] == "nickname" || argsl[1] == "nick") {
// A more natural way of resetting nickname
text := "{RESET}" // case sensitive
setNickResponse(text, msg, s) // checks permissions internally
} else {
defaultResponse(msg, s, successful_cc)
}
case "speak":
if len(argsl) > 1 && argsl[1] == "generate" {
if len(argsl) > 2 {
text := strings.Join(args[2:], " ") // case sensitive
randSpeakGenerateResponse(msg, s, text)
} else {
randSpeakGenerateResponse(msg, s, "")
}
} else if len(argsl) > 1 && argsl[1] == "randomspeak" {
if len(argsl) > 2 && (argsl[2] == "on" || argsl[2] == "off") {
perms, err := getPerms(msg, s)
if err != nil {
msgerr(err, msg, s)
return
}
if !hasPerm(perms, disgord.PermissionManageMessages) {
baseReply(msg, s, "You don't have Manage Messages permissions. Sorry!")
return
}
if argsl[2] == "on" {
err := DBConn.SetRandomSpeakAvailability(getDivision(msg), true)
if err != nil {
msgerr(err, msg, s)
return
}
baseReply(msg, s, "Randomspeak has been enabled.")
} else if argsl[2] == "off" {
err := DBConn.SetRandomSpeakAvailability(getDivision(msg), false)
if err != nil {
msgerr(err, msg, s)
return
}
baseReply(msg, s, "Randomspeak has been disabled.")
} else {
err := errors.New("cosmic bit flip (speak randomspeak on/off)")
msgerr(err, msg, s)
return
}
} else if len(argsl) > 2 && args[2] == "status" {
statusStr := "OFF"
data, err := getRandSpeakInfo(msg)
if err != nil {
msgerr(err, msg, s)
return
}
if data.status {
statusStr = "ON"
}
baseReply(msg, s, "Randspeak is currently "+statusStr)
} else {
baseReply(msg, s, "Would you like to check the [status] or turn it [on] or [off]?")
}
} else {
baseReply(msg, s, "Would you like to [generate] a phrase or manage [randomspeak]?")
}
case "combinations", "combos", "powerset":
if len(argsl) > 1 { // option
if len(argsl) > 2 { // text
switch argsl[1] {
case "words", "w":
combosResponse(args[2:], msg, s)
case "characters", "chars", "c":
text := strings.Join(args[2:], " ")
ltrs := strings.Split(text, "")
combosResponse(ltrs, msg, s)
default:
baseReply(msg, s, "That's not an option.")
}
} else {
baseReply(msg, s, "What do you want the combinations for?")
}
} else {
baseReply(msg, s, "Which combinations do you want? Words, or characters?")
}
case "ping":
if len(argsl) > 1 && (argsl[1] == "info" || argsl[1] == "information") {
pingResponse(true, msg, s, procTimeStart)
} else {
pingResponse(false, msg, s, procTimeStart)
}
case "mydata":
defaultTODOResponse(msg, s)
return // feature incomplete
if len(argsl) > 1 && (argsl[1] == "user" || argsl[1] == "me") {
if len(argsl) > 2 && argsl[2] == "download" {
} else if len(argsl) > 2 && argsl[2] == "delete" {
}
} else if len(argsl) > 1 && (argsl[1] == "server" || argsl[1] == "guild") {
// check for permissions
perms, err := getPerms(msg, s)
if err != nil {
msgerr(err, msg, s)
return
}
if !hasPerm(perms, disgord.PermissionAdministrator) {
baseReply(msg, s, "You don't have administrator permission. Sorry!")
return
}
if len(argsl) > 2 && argsl[2] == "download" {
} else if len(argsl) > 2 && argsl[2] == "delete" {
}
} else {
baseReply(msg, s, "Would you like to manage your [user] data or this [server]'s data?")
}
default:
defaultResponse(msg, s, successful_cc)
}
}