-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdiscord_main.go
1456 lines (1369 loc) · 41.4 KB
/
discord_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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/google/uuid"
"github.com/grokify/html-strip-tags-go"
"html"
"log"
"math"
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"
)
var (
//server-invariant
discord_bot_user_id string //for permchecks
discord_bot_token string
Discord_command_character string
discord_superuser_id string
local_users map[string]string //user id -> ckey
local_moderators []string //ckeys
discord_player_roles map[string]string //guild id -> role
known_channels_id_t map[string]channel //channel id -> channel
discord_spam_prot_bans map[string]bool
discord_spam_prot_checks map[string]int //user id -> spammability
discord_spam_prot_limit int //if checks amount exceed limit, autoban dat spammer scum
discord_spam_prot_tick int //each tick all entries are nullified, in seconds
//server-specific
Known_admins map[string][]string //server -> ckeys
known_channels_s_t_id_m map[string]map[string][]string //server -> type -> channel ids
discord_subscriber_roles map[string]map[string]string //guild id -> server -> role
discord_admin_roles map[string]map[string]string //guild id -> server -> role
discord_onetime_subscriptions map[string]map[string]string //guild id -> server -> users slap string
discord_ahelp_locks map[string]map[string]string //server -> adminid -> ckey
//discord-specific
discord_guild_permission_errors_flap map[string]bool
)
type channel struct {
generic_type string //ooc, admin, debug etc
server string //which server it belongs to
}
const (
PERMISSIONS_NONE = iota - 1
PERMISSIONS_REGISTERED
PERMISSIONS_ADMIN
PERMISSIONS_MODERATOR
PERMISSIONS_SUPERUSER
)
const PERMISSIONS_SPECIAL = -1
const (
MODERATOR_BAN_PERMISSION = PERMISSIONS_SUPERUSER
)
const (
ROLE_PLAYER = "player"
ROLE_ADMIN = "admin"
ROLE_SUBSCRIBER = "subscriber"
)
const (
BANTYPE_OOC = 1 << iota
BANTYPE_COMMANDS
BANTYPE_SPAM
)
const (
BANSTRING_OOC = "OOC"
BANSTRING_COMMANDS = "COMMANDS"
)
//DEL_FIXED_MOD is not spamproof, so should rarely be used
const (
DEL_FIXED_MOD = -2
DEL_NEVER = -1
DEL_DEFAULT = 0
DEL_LONG = 3
DEL_EXTRA_LONG = 10
)
const max_message_size = 2000
const message_mod_delay = 1
const message_fixed_delay = 30
type dban struct {
reason string
admin string
bantype int
permlevel int
}
var known_bans_summary map[string]map[int]int
var known_ban_overrides map[string]map[string]map[int]int //server->ckey->permission->type
var dsession, _ = discordgo.New()
var last_ahelp map[string]string
var emoji_stripper *regexp.Regexp
var url_embedder *regexp.Regexp
var channel_message_send_loops_online map[string]bool
var channel_buffers map[string][]string
func discord_init() {
populate_configs()
discord_bot_token = get_config_must("discord_bot_token")
dsession.Token = discord_bot_token
Discord_command_character = get_config_must("discord_command_character")
discord_superuser_id = get_config_must("discord_superuser_id")
discord_spam_prot_limit_str := get_config_must("discord_spam_prot_limit")
var err error
discord_spam_prot_limit, err = strconv.Atoi(discord_spam_prot_limit_str)
if err != nil {
log.Fatalln("Failed to parse 'discord_spam_prot_limit'")
}
discord_spam_prot_tick_str := get_config_must("discord_spam_prot_tick")
discord_spam_prot_tick, err = strconv.Atoi(discord_spam_prot_tick_str)
if err != nil {
log.Fatalln("Failed to parse 'discord_spam_prot_tick'")
}
emoji_stripper = regexp.MustCompile("<a?:.+?:[0-9]{18}?>")
url_embedder = regexp.MustCompile("https?://[\\w.]+/?[^ ]+")
local_users = make(map[string]string)
local_moderators = make([]string, 0)
discord_player_roles = make(map[string]string)
known_channels_id_t = make(map[string]channel)
Known_admins = make(map[string][]string)
known_channels_s_t_id_m = make(map[string]map[string][]string)
discord_subscriber_roles = make(map[string]map[string]string)
discord_admin_roles = make(map[string]map[string]string)
discord_onetime_subscriptions = make(map[string]map[string]string)
known_bans_summary = make(map[string]map[int]int)
known_ban_overrides = make(map[string]map[string]map[int]int)
last_ahelp = make(map[string]string)
discord_spam_prot_checks = make(map[string]int)
discord_spam_prot_bans = make(map[string]bool)
discord_ahelp_locks = make(map[string]map[string]string)
channel_message_send_loops_online = make(map[string]bool)
channel_buffers = make(map[string][]string)
discord_guild_permission_errors_flap = make(map[string]bool)
}
func reply(session *discordgo.Session, message *discordgo.MessageCreate, msg string, temporary int) []*discordgo.Message {
if temporary == DEL_FIXED_MOD {
rep, err := session.ChannelMessageSend(message.ChannelID, "<@!"+message.Author.ID+">, here will be result of your command")
if err != nil {
log.Println("NON-PANIC ERROR: failed to delete reply message in discord: ", err)
}
time.Sleep(time.Duration(message_mod_delay) * time.Second)
session.ChannelMessageEdit(rep.ChannelID, rep.ID, "<@!"+message.Author.ID+">, "+msg)
if !is_in_private_channel(session, message) {
delete_in(session, rep, message_fixed_delay)
}
return []*discordgo.Message{rep}
}
rep := send_message_big(message.ChannelID, "<@!"+message.Author.ID+">, "+msg)
if temporary == DEL_NEVER {
return rep
}
if temporary == DEL_DEFAULT {
temporary = 1
}
temporary = temporary * int(math.Ceil(math.Sqrt(2+float64(len(msg))/10)))
if !is_in_private_channel(session, message) {
batch_delete_in(session, rep, temporary)
}
return rep
}
func is_in_private_channel(session *discordgo.Session, message *discordgo.MessageCreate) bool {
channel, err := session.State.Channel(message.ChannelID)
if err != nil {
log.Println("ERROR: failed to retrieve channel: ", err)
return false
}
return channel.Type == discordgo.ChannelTypeDM || channel.Type == discordgo.ChannelTypeGroupDM
}
func delete_in(session *discordgo.Session, message *discordgo.Message, seconds int) {
time.Sleep(time.Duration(seconds) * time.Second)
err := session.ChannelMessageDelete(message.ChannelID, message.ID)
if err != nil {
log.Println("NON-PANIC ERROR: failed to delete reply message in discord: ", err)
}
}
func batch_delete_in(session *discordgo.Session, messages []*discordgo.Message, seconds int) {
time.Sleep(time.Duration(seconds) * time.Second)
for _, message := range messages {
err := session.ChannelMessageDelete(message.ChannelID, message.ID)
if err != nil {
log.Println("NON-PANIC ERROR: failed to delete reply message in discord: ", err)
}
}
}
func delcommand(session *discordgo.Session, message *discordgo.MessageCreate) {
if is_in_private_channel(session, message) {
return
}
err := session.ChannelMessageDelete(message.ChannelID, message.ID)
if err != nil {
log.Println("NON-PANIC ERROR: failed to delete command message in discord: ", err)
}
}
func delmessage(session *discordgo.Session, message *discordgo.Message) {
err := session.ChannelMessageDelete(message.ChannelID, message.ID)
if err != nil {
log.Println("NON-PANIC ERROR: failed to delete message in discord: ", err)
}
}
func Get_guild(session *discordgo.Session, message *discordgo.MessageCreate) string {
channel, err := session.Channel(message.ChannelID)
if err != nil {
log.Println("Shiet: ", err)
return ""
}
return channel.GuildID
}
func ckey_simplifier(s string) string {
return strings.ToLower(strings.Replace(strings.Replace(s, ".", "", -1), "_", "", -1))
}
func get_permission_level_ckey(ckey, server string) int {
if check_moderator(ckey) {
return PERMISSIONS_MODERATOR
}
if server != "" {
asl, ok := Known_admins[server]
if !ok {
return PERMISSIONS_REGISTERED
}
for _, ackey := range asl {
if ckey == ackey {
return PERMISSIONS_ADMIN //this server admin
}
}
return PERMISSIONS_REGISTERED
}
//no server, wide check
for _, adminsl := range Known_admins {
for _, ackey := range adminsl {
if ckey == ackey {
return PERMISSIONS_ADMIN //generic admin
}
}
}
return PERMISSIONS_REGISTERED
}
func get_permission_level_ckey_sp(ckey, server string) int {
su, ok := local_users[discord_superuser_id]
if ok && ckey_simplifier(su) == ckey {
return PERMISSIONS_SUPERUSER
}
return get_permission_level_ckey(ckey, server)
}
func get_permission_level(user *discordgo.User, server string) int {
if user.ID == discord_superuser_id || user.ID == discord_bot_user_id {
return PERMISSIONS_SUPERUSER //bot admin
}
ckey := local_users[user.ID]
if ckey == "" {
return PERMISSIONS_NONE //not registered
}
ckey = ckey_simplifier(ckey)
return get_permission_level_ckey(ckey, server)
}
func Permissions_check(user *discordgo.User, permission_level int, server string) bool {
return get_permission_level(user, server) >= permission_level
}
func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate) {
if message.Author.ID == session.State.User.ID {
return
}
mcontent := message.ContentWithMentionsReplaced()
if is_in_private_channel(session, message) && !Permissions_check(message.Author, PERMISSIONS_SUPERUSER, "") {
reply(session, message, "FORBIDDEN, won't execute commands in private channels", DEL_NEVER)
return
}
if len(mcontent) < 1 { //wut?
return
}
if mcontent[:1] == Discord_command_character {
if !spam_check(message.Author.ID) {
delete_in(session, message.Message, 1)
return
}
if len(mcontent) < 2 { //one for command char and at least one for command
return
}
//it's command
defer delcommand(session, message)
var server string
srvstr, ok := known_channels_id_t[message.ChannelID]
if ok {
server = srvstr.server
}
args := strings.Fields(mcontent[1:])
command := strings.ToLower(args[0])
if check_bans(message.Author, server, BANTYPE_COMMANDS) && command != "baninfo" {
reply(session, message, "you're banned from this action. Try !baninfo.\nHead here -> "+config_entries["ban_unban_place"]+" <- if you feel you should be unbanned and **can prove it**", DEL_DEFAULT)
return
}
if len(args) > 1 {
args = args[1:]
} else {
args = make([]string, 0) //empty slice
}
dcomm, ok := known_commands[command]
if !ok {
reply(session, message, "unknown command: `"+Dweaksanitize(command)+"`", DEL_DEFAULT)
return
}
log_line(fmt.Sprintf("<@%v>(%v)[%v]->`%v`", message.Author.ID, local_users[message.Author.ID], server, message.Content), "commands")
if server == "" && dcomm.Server_specific {
reply(session, message, "this command requires channel to be bound to server", DEL_DEFAULT)
return
}
if !Permissions_check(message.Author, dcomm.Permlevel, server) {
reply(session, message, "missing permissions required to run this command: `"+Dweaksanitize(command)+"`", DEL_DEFAULT)
return
}
if len(args) < dcomm.Minargs {
reply(session, message, "usage: "+dcomm.Usagestr(), DEL_LONG)
return
}
ret := dcomm.Exec(session, message, args, server)
if ret == "" {
return
}
reply(session, message, ret, dcomm.Temporary)
return
} else if dsch := get_config("discord_shell_character"); dsch != "" && mcontent[:1] == dsch {
if !spam_check(message.Author.ID) {
delete_in(session, message.Message, 1)
return
}
if len(mcontent) < 2 { //one for command char and at least one for command
return
}
defer delete_in(session, message.Message, 1)
udata := userdata{key: local_users[message.Author.ID], message: message, session: session}
srvstr, ok := known_channels_id_t[message.ChannelID]
if ok {
udata.server = srvstr.server
}
if check_bans(message.Author, udata.server, BANTYPE_COMMANDS) {
reply(session, message, "you're banned from this action. Try !baninfo.\nHead here -> "+config_entries["ban_unban_place"]+" <- if you feel you should be unbanned and **can prove it**", DEL_DEFAULT)
return
}
log_line(fmt.Sprintf("<@%v>(%v)[%v]->`%v`", message.Author.ID, udata.key, udata.server, message.Content), "shell_commands")
ret := shell_handler(udata, mcontent[1:])
//TODO: make it temporary
if udata.key == "" {
udata.key = "nokey"
}
if udata.server == "" {
udata.server = "noserver"
}
inpsplit := strings.Split(mcontent, "\n")
inpjoined := strings.Join(inpsplit, "\n> ")
send_message(message.ChannelID, fmt.Sprintf("```%s[%s]@%s:%s```\n%s", udata.key, message.Author.Username, udata.server, inpjoined, ret))
}
if known_channels_id_t[message.ChannelID].generic_type != "ooc" && known_channels_id_t[message.ChannelID].generic_type != "admin" {
return
}
if !spam_check(message.Author.ID) {
delete_in(session, message.Message, 0)
return
}
shown_nick := local_users[message.Author.ID]
defer delcommand(session, message)
if shown_nick == "" {
channel, err := session.Channel(message.ChannelID)
if err != nil {
log.Println("Shiet: ", err)
reply(session, message, "failed to retrieve channel", DEL_DEFAULT)
}
if logoff_user(channel.GuildID, message.Author.ID) {
reply(session, message, "you were logged off because of missing registration entry (try !register)", DEL_NEVER)
return
} else {
reply(session, message, "no registration entry found + automatic logoff failed (probably because broken permissions); your message weren't delivered, register (!register) to be able to use OOC; also ask guild admins to fix bot's permissions", DEL_NEVER)
return
}
}
addstr := ""
srv := known_channels_id_t[message.ChannelID]
mcontent = emoji_stripper.ReplaceAllString(mcontent, "")
var byondmcontent string //sent to byond
isadminhere := Permissions_check(message.Author, PERMISSIONS_ADMIN, srv.server)
if isadminhere {
byondmcontent = "<font color='#39034f'>" + mcontent + "</font>"
addstr = "&isadmin=1"
} else {
byondmcontent = strings.Replace(mcontent, "\n", "#", -1)
byondmcontent = html.EscapeString(byondmcontent)
}
switch srv.generic_type {
case "ooc":
if check_bans(message.Author, srv.server, BANTYPE_OOC) {
reply(session, message, "you're banned from this action. Try !baninfo", DEL_DEFAULT)
return
}
br := Byond_query(srv.server, "ckey="+Bquery_convert(shown_nick, srv.server)+"&ooc="+Bquery_convert(byondmcontent, srv.server)+addstr, true)
if br.String() == "muted" {
reply(session, message, "your ckey is muted from OOC", DEL_DEFAULT)
return
}
if br.String() == "globally muted" {
reply(session, message, "OOC is globally muted", DEL_DEFAULT)
return
}
Discord_message_send(srv.server, "ooc", "DOOC: "+shown_nick, strip.StripTags(mcontent))
case "admin":
if !isadminhere {
reply(session, message, "You have no privilegies to write here", DEL_DEFAULT)
return //not today
}
Byond_query(srv.server, "admin="+Bquery_convert(shown_nick, srv.server)+"&asay="+Bquery_convert(byondmcontent, srv.server), true)
Discord_message_send(srv.server, "admin", "DASAY: "+shown_nick, strip.StripTags(mcontent))
default:
}
}
//this one uses loops to try to handle spam
//it hangs on attempting to send message if rate limit is exceeded, so it queues more chunks
var send_message_mutex sync.Mutex
func send_message(channel, message string) {
message = fmt.Sprintf("`[%v]` %v", get_time(), message)
send_message_mutex.Lock()
if channel_buffers[channel] == nil {
channel_buffers[channel] = make([]string, 1)
channel_buffers[channel][0] = message
} else {
channel_buffers[channel] = append(channel_buffers[channel], message)
}
if channel_message_send_loops_online[channel] {
send_message_mutex.Unlock() //doublelol
return
}
channel_message_send_loops_online[channel] = true
send_message_mutex.Unlock() //lol
go func() {
for {
send_message_mutex.Lock()
if len(channel_buffers[channel]) <= 0 {
send_message_mutex.Unlock()
break
}
condensed := strings.Join(channel_buffers[channel], "\n")
var chunk string
if len(condensed) <= max_message_size {
chunk = condensed
channel_buffers[channel] = nil
} else {
li := strings.LastIndexByte(condensed[:max_message_size], '\n')
if li < 0 {
//channel_message_send_loops_online[channel] = false
//panic("send message WTF li<0: `" + condensed[:max_message_size] + "`")
//some shitter sent VERY long line from server, force cut it
li = max_message_size - 5
chunk = condensed[:li] + "**#**"
channel_buffers[channel] = []string{"**#**" + condensed[li:]}
} else {
chunk = condensed[:li]
channel_buffers[channel] = []string{condensed[li+1:]}
}
}
send_message_mutex.Unlock()
_, err := dsession.ChannelMessageSend(channel, chunk)
if err != nil {
log.Println("DERR: failed to send message to discord: ", err)
}
}
send_message_mutex.Lock()
channel_message_send_loops_online[channel] = false
send_message_mutex.Unlock()
}()
}
func send_message_big(channel, message string) []*discordgo.Message {
ret := make([]*discordgo.Message, 0)
for len(message) > 0 {
var tosend string
if len(message) > max_message_size {
li := strings.LastIndexByte(message[:max_message_size], '\n')
if li < 0 {
li = max_message_size - 5
tosend = message[:li] + "**#**"
message = "**#**" + message[li:]
} else {
tosend = message[:li]
message = message[li:]
}
} else {
tosend = message
message = ""
}
rep, err := dsession.ChannelMessageSend(channel, tosend)
if err != nil {
log.Println("DERR: failed to send message to discord: ", err)
break
} else {
ret = append(ret, rep)
}
}
return ret
}
func Discord_subsriber_message_send(servername, channel, message string) {
defer logging_recover("Dsms")
srvchans, ok := known_channels_s_t_id_m[servername]
if !ok {
panic("unknown server, " + servername)
}
channels, ok := srvchans[channel]
if !ok || len(channels) < 1 {
return
}
flush_onetime_subscriptions(servername)
for _, id := range channels {
chann, cerr := dsession.Channel(id)
if cerr != nil {
log.Println("ERROR: CHAN<-ID FAIL: ", cerr)
continue
}
guild, gerr := dsession.Guild(chann.GuildID)
if gerr != nil {
log.Println("ERROR: GUILD<-ID FAIL: ", gerr)
continue
}
var rid string
guildsubrole, ok := discord_subscriber_roles[guild.ID]
if ok {
rid, ok = guildsubrole[servername]
if !ok {
rid = ""
} else {
rid = "<@&" + rid + ">, "
}
}
var subs string
guildoncesubs, ok := discord_onetime_subscriptions[guild.ID]
if ok {
subs, ok = guildoncesubs[servername]
if !ok {
subs = ""
} else {
subs += ", "
}
}
send_message(id, rid+subs+Dsanitize(message))
}
}
func Discord_message_send(servername, channel, boldprefix, message string) {
defer logging_recover("Dms")
srvchans, ok := known_channels_s_t_id_m[servername]
if !ok {
panic("unknown server, " + servername)
}
channels, ok := srvchans[channel]
if !ok || len(channels) < 1 {
return //no bound channels
}
for _, id := range channels {
send_message(id, "**"+Dsanitize(boldprefix)+":** "+Dsanitize(message))
}
}
func Discord_message_send_raw(servername, channel, message string) {
defer logging_recover("Dmsr")
srvchans, ok := known_channels_s_t_id_m[servername]
if !ok {
panic("unknown server, " + servername)
}
channels, ok := srvchans[channel]
if !ok || len(channels) < 1 {
return //no bound channels
}
for _, id := range channels {
send_message(id, message)
}
}
func Discord_send_embed(servername, channel string, embed *discordgo.MessageEmbed) {
defer logging_recover("Dse")
srvchans, ok := known_channels_s_t_id_m[servername]
if !ok {
panic("unknown server, " + servername)
}
channels, ok := srvchans[channel]
if !ok || len(channels) < 1 {
return //no bound channels
}
for _, id := range channels {
_, err := dsession.ChannelMessageSendEmbed(id, embed)
if err != nil {
log.Println("DERR: failed to send embed to discord: ", err)
}
}
}
func Discord_replace_embed(channelid, messageid string, embed *discordgo.MessageEmbed) (ret bool) {
defer logging_recover("Dre")
_, err := dsession.ChannelMessageEditComplex(discordgo.NewMessageEdit(channelid, messageid).SetContent(fmt.Sprintf("Last status update: %v UTC+3(Moscow)", get_time())).SetEmbed(embed))
if fmt.Sprint(err) == "HTTP 404 Not Found, {\"message\": \"Unknown Channel\", \"code\": 10003}" {
return false
}
ret = true
noerror(err)
return
}
func Discord_message_propagate(servername, channel, prefix, ckey, message, chanid string) {
//given channel id and other params sends message to all channels except specified one
defer logging_recover("Dmp")
srvchans, ok := known_channels_s_t_id_m[servername]
if !ok {
panic("unknown server, " + servername)
}
channels, ok := srvchans[channel]
if !ok || len(channels) < 1 {
return //no bound channels
}
var delim string
if prefix != "" && ckey != "" {
delim = " "
}
for _, id := range channels {
if id == chanid {
continue
}
send_message(id, "**"+Dsanitize(prefix+delim+ckey)+":** "+Dsanitize(message))
}
}
func Discord_private_message_send(user *discordgo.User, message string) bool {
channel, err := dsession.UserChannelCreate(user.ID)
if err != nil {
log.Println("Failed to create private channel: ", err)
return false
}
send_message(channel.ID, message)
return true
}
func Dsanitize(m string) string {
out := strings.Replace(m, "\\", "\\\\", -1)
out = strings.Replace(out, "*", "\\*", -1)
out = strings.Replace(out, "`", "\\`", -1)
out = strings.Replace(out, "_", "\\_", -1)
out = strings.Replace(out, "~", "\\~", -1)
out = strings.Replace(out, "@everyone", "[я долбоеб]", -1)
out = strings.Replace(out, "@here", "[я долбоеб]", -1)
out = strings.Replace(out, "@", "\\@", -1)
out = url_embedder.ReplaceAllString(out, "<$0>")
return out
}
func Dweaksanitize(m string) string {
out := strings.Replace(m, "`", "\\`", -1)
return out
}
func trim(str string) string {
return strings.Trim(str, " ")
}
func populate_known_channels() {
defer logging_recover("pkc")
for k := range known_channels_id_t {
delete(known_channels_id_t, k)
} //clear id->type pairs
for k, v := range known_channels_s_t_id_m {
for sk := range v {
delete(v, sk)
}
delete(known_channels_s_t_id_m, k)
} //clear server->type->ids and server->type
var ch, id, srv string
clcllb := func() {
known_channels_id_t[id] = channel{ch, srv}
if srv == "" {
log.Println("DB: setting `" + id + "` to '" + srv + "@" + ch + "';")
return
}
srvchans, ok := known_channels_s_t_id_m[srv]
if !ok {
srvchans = make(map[string][]string)
}
chsl, ok := srvchans[ch]
if !ok {
chsl = make([]string, 0)
}
srvchans[ch] = append(chsl, id)
known_channels_s_t_id_m[srv] = srvchans // not needed since maps are references, but it's nice for readability
log.Println("DB: setting `" + id + "` to '" + srv + "@" + ch + "';")
}
db_template("select_known_channels").query().parse(clcllb, &ch, &id, &srv)
}
func add_known_channel(srv, t, id, gid string) bool {
defer logging_recover("akc")
if db_template("add_known_channel").exec(t, id, gid, srv).count() > 0 {
populate_known_channels()
return true
}
return false
}
func Remove_known_channels(srv, t, gid string) bool {
defer logging_recover("rkc")
var res *db_query_result
if gid == "" {
res = db_template("remove_known_channels").exec(t, srv)
} else {
res = db_template("remove_known_channels_guild").exec(t, gid, srv)
}
if res.count() > 0 {
populate_known_channels()
return true
}
return false
}
func List_known_channels() string {
ret := "known channels:\n"
for id, t := range known_channels_id_t {
ret += fmt.Sprintf("<#%s> <-> `%s@%s`\n", id, t.server, t.generic_type)
}
return ret
}
func Update_known_channel(srv, t, id, gid string) bool {
defer logging_recover("ukc")
if db_template("update_known_channel").exec(t, id, gid, srv).count() > 0 {
populate_known_channels() //update everything
return true
} else {
return add_known_channel(srv, t, id, gid)
}
}
func Remove_token(ttype, data string) bool {
defer logging_recover("rt")
if db_template("remove_token").exec(ttype, data).count() > 0 {
return true
}
return false
}
func remove_token_by_id(id string) bool {
defer logging_recover("rtbi")
if db_template("remove_token_by_id").exec(id).count() > 0 {
return true
}
return false
}
func Create_token(ttype, data string) string {
defer logging_recover("ct")
id := uuid.New().String()
if db_template("create_token").exec(id, ttype, data).count() > 0 {
return id
}
return ""
}
func expend_token(id string) (ttype, data string) {
defer logging_recover("et")
db_template("select_token").row(id).parse(&ttype, &data)
remove_token_by_id(id)
return
}
func Discord_process_token(id, ckey string) {
ttype, data := expend_token(id)
if ttype == "" {
return
}
switch ttype {
case "register":
register_user(data, ckey)
default:
}
}
func deregister_user(login string) string {
defer logging_recover("dru")
if db_template("delete_user_did").exec(login).count() > 0 {
delete(local_users, login)
return "OK"
}
return "FAIL"
}
func register_user(login, ckey string) {
defer logging_recover("ru")
db_template("delete_user_did").exec(login)
db_template("delete_user_ckey").exec(ckey)
db_template("register_user").exec(login, ckey)
update_local_user(login)
user, err := dsession.User(login)
if err != nil {
panic("failed to get user")
}
Discord_private_message_send(user, "Registered as `"+ckey+"`")
}
func update_local_users() {
defer logging_recover("ulus")
for k := range local_users {
delete(local_users, k)
}
var login, ckey string
db_template("select_users").query().parse(func() {
local_users[login] = ckey
}, &login, &ckey)
}
func update_local_user(login string) (ckey string) {
defer logging_recover("ulu")
db_template("select_user").row(login).parse(&ckey)
for l, c := range local_users {
if l == login || c == ckey {
delete(local_users, l)
}
}
local_users[login] = ckey
return
}
func populate_known_roles() {
logging_recover("pkr")
for k := range discord_player_roles {
delete(discord_player_roles, k)
}
for k := range discord_admin_roles {
delete(discord_admin_roles, k)
}
var gid, rid, tp, srv string
closure_callback := func() {
switch tp {
case ROLE_PLAYER:
discord_player_roles[gid] = rid
case ROLE_ADMIN:
if srv != "" {
m, ok := discord_admin_roles[gid]
if !ok {
m = make(map[string]string)
discord_admin_roles[gid] = m
}
m[srv] = rid
}
case ROLE_SUBSCRIBER:
m, ok := discord_subscriber_roles[gid]
if !ok {
m = make(map[string]string)
discord_subscriber_roles[gid] = m
}
m[srv] = rid
}
}
db_template("select_known_roles").query().parse(closure_callback, &gid, &rid, &tp, &srv)
}
func update_known_role(gid, tp, rid, srv string) bool {
defer logging_recover("ukr")
if db_template("update_known_role").exec(rid, gid, tp, srv).count() > 0 {
populate_known_roles()
return true
}
return create_known_role(gid, tp, rid, srv)
}
func create_known_role(gid, tp, rid, srv string) bool {
defer logging_recover("ckr")
if db_template("create_known_role").exec(gid, rid, tp, srv).count() > 0 {
populate_known_roles()
return true
}
return false
}
func remove_known_role(gid, tp, srv string) bool {
defer logging_recover("rkr")
if db_template("remove_known_role").exec(gid, tp, srv).count() > 0 {
populate_known_roles()
return true
}
return false
}
func populate_bans() {
defer logging_recover("pb")
//clean known
for k := range known_bans_summary {
delete(known_bans_summary, k)
}
var ckey string
var bantype, permission int
closure_callback := func() {
_, ok := known_bans_summary[ckey]
if !ok {
known_bans_summary[ckey] = make(map[int]int)
}
for i := permission - 1; i >= 0; i-- {
known_bans_summary[ckey][i] |= bantype
}
}
db_template("fetch_bans").query().parse(closure_callback, &ckey, &bantype, &permission)
}
func populate_ban_overrides() {
defer logging_recover("pbo")
for k := range known_ban_overrides {
delete(known_ban_overrides, k)
}
var server, ckey string
var bantype, permission int
closure_callback := func() {
srv, ok := known_ban_overrides[server]
if !ok {
srv = make(map[string]map[int]int)
known_ban_overrides[server] = srv
}
usr, ok := known_ban_overrides[server][ckey]
if !ok {
usr = make(map[int]int)
known_ban_overrides[server][ckey] = usr
}
for i := permission - 1; i >= 0; i-- {
usr[i] |= bantype
}
}
db_template("fetch_ban_overrides").query().parse(closure_callback, &server, &ckey, &bantype, &permission)
}
func update_ban(ckey, reason string, user *discordgo.User, tp int) (succ bool, msg string) {
defer logging_recover("ub")
ckey = ckey_simplifier(ckey)
permissions := get_permission_level(user, "")
var admin string
if user.ID == dsession.State.User.ID {
admin = dsession.State.User.Username
} else {
admin = local_users[user.ID]
}
if permissions < PERMISSIONS_ADMIN {
return false, "missing permissions (how the fuck did you get there?)"
}
msg = "lr"
if db_template("lookup_ban").exec(ckey, tp, admin).count() > 0 {
msg = "ur"
if db_template("update_ban").exec(reason, ckey, admin, tp, permissions).count() > 0 {
populate_bans()
return true, "updated"
}
return false, "some strange shit happened"
} else {
msg = "cr"
if db_template("create_ban").exec(ckey, admin, reason, tp, permissions).count() > 0 {
populate_bans()
return true, "created"
}
return false, "some neat shit happened"
}
}
func update_ban_override(server, ckey string, user *discordgo.User, tp int) (succ bool, msg string) {
defer logging_recover("ubo")
ckey = ckey_simplifier(ckey)
permissions := get_permission_level(user, server)
if permissions < PERMISSIONS_ADMIN {
return false, "403 forbidden"
}
msg = "database failure"
if db_template("check_ban_override").exec(server, ckey, tp, permissions).count() > 0 {
populate_ban_overrides()
return true, "stronger override is in effect"
}
if db_template("promote_ban_override").exec(server, ckey, tp, permissions).count() > 0 {
populate_ban_overrides()