forked from gregzaal/Auto-Voice-Channels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-voice-channels.py
1169 lines (1027 loc) · 46.2 KB
/
auto-voice-channels.py
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
import asyncio
import concurrent.futures
import os
import logging
import traceback
import subprocess
import sys
from datetime import datetime
from time import time
import cfg
from commands import admin_commands
import commands
import discord
import psutil
import pytz
import translate
import utils
import functions as func
from functions import log, echo
from discord.ext.tasks import loop
logging.basicConfig(level=logging.INFO)
ADMIN_CHANNEL = None
ADMIN = None
DEV_BOT = cfg.CONFIG['DEV'] if 'DEV' in cfg.CONFIG else False
GOLD_BOT = False
NUM_SHARDS = cfg.CONFIG['num_shards'] if 'num_shards' in cfg.CONFIG else 0
if DEV_BOT:
print("DEV BOT")
TOKEN = cfg.CONFIG['token_dev']
else:
TOKEN = cfg.CONFIG['token']
try:
sid = int(sys.argv[1])
if str(sid) in cfg.CONFIG["sapphires"]:
cfg.SAPPHIRE_ID = sid
TOKEN = cfg.CONFIG["sapphires"][str(sid)]['token']
NUM_SHARDS = 1
elif 'gold_id' in cfg.CONFIG and sid == 6666:
GOLD_BOT = True
TOKEN = cfg.CONFIG["token_gold"]
NUM_SHARDS = 1
else:
print("NO SAPPHIRE WITH ID " + str(sid))
sys.exit()
except IndexError:
pass
except ValueError:
pass
LAST_COMMIT = "UNKNOWN"
try:
LAST_COMMIT = subprocess.check_output(['git', 'log', '-1']).decode('ascii').strip().split('\n\n ', 1)[1]
except:
print("Warning: Failed to get last commit log")
pass
utils.clean_permastore()
utils.update_server_location()
class LoopChecks:
def __init__(self, client, tick):
self._client = client
self._time = time()
self._loop = asyncio.get_event_loop()
self._tick = tick
async def waiting_loop(self):
print("Starting Waiting Loop")
while True:
tmp = {}
for c, v in cfg.CURRENT_REQUESTS.items():
if self._time - v < 5:
tmp[c] = v
cfg.CURRENT_REQUESTS = tmp
await asyncio.sleep(self._tick)
async def active_loop(self):
print("Starting Active Loop")
while True:
tmp = {}
for u, v in cfg.USER_REQUESTS.items():
if self._time - v < 15:
tmp[u] = v
else:
if u in cfg.USER_ABUSE_EVENTS:
del (cfg.USER_ABUSE_EVENTS[u])
cfg.USER_REQUESTS = tmp
await asyncio.sleep(self._tick)
async def other_loops(self):
print("Starting Other Loops")
while True:
tmp = {}
for e, v in cfg.ERROR_MESSAGES.items():
if self._time - v < (60 * 5): # Forget messages older than 5 minutes
tmp[e] = v
cfg.ERROR_MESSAGES = tmp
tmp = {}
for e, v in cfg.DM_ERROR_MESSAGES.items():
if self._time - v < 15:
tmp[e] = v
cfg.DM_ERROR_MESSAGES = tmp
await asyncio.sleep(self._tick)
async def timer(self):
print("Starting Timer")
while True:
self._time = time()
await asyncio.sleep(self._tick)
def start_loops(self):
self._loop.create_task(self.timer())
self._loop.create_task(self.waiting_loop())
self._loop.create_task(self.active_loop())
self._loop.create_task(self.other_loops())
def cleanup(client, tick_):
# TODO probably possible to run into race conditions
start_time = time()
LoopSystem = LoopChecks(client=client, tick=tick_)
LoopSystem.start_loops()
async def first_start(client):
global ADMIN_CHANNEL
global ADMIN
while not client.is_ready():
await asyncio.sleep(1)
if not cfg.FIRST_RUN_COMPLETE:
cfg.FIRST_RUN_COMPLETE = True
guilds = func.get_guilds(client)
if guilds:
text = 'vc/help'
if len(guilds) == 1 and guilds[0].id in cfg.PREFIXES:
text = cfg.PREFIXES[guilds[0].id] + 'help'
else:
text = "🚧No guilds🚧"
await client.change_presence(activity=discord.Activity(name=text, type=discord.ActivityType.watching))
if 'admin_channel' in cfg.CONFIG:
ADMIN_CHANNEL = client.get_channel(cfg.CONFIG['admin_channel'])
if ADMIN is None:
ADMIN = client.get_user(cfg.CONFIG['admin_id'])
asyncio.get_event_loop().create_task(first_start(client))
end_time = time()
fn_name = "cleanup"
cfg.TIMINGS[fn_name] = end_time - start_time
# @loop(seconds=cfg.CONFIG['loop_interval'])
@loop(minutes=10)
async def main_loop(client):
main_loop.last_run = datetime.now(pytz.utc)
start_time = time()
if client.is_ready():
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
thread_ = executor.submit(func.get_guilds, client)
while not thread_.done():
await asyncio.sleep(0.1)
guilds = thread_.result()
for guild in guilds:
settings = utils.get_serv_settings(guild)
if settings['enabled'] and settings['auto_channels']:
await check_all_channels(guild, settings)
end_time = time()
fn_name = "main_loop"
cfg.TIMINGS[fn_name] = end_time - start_time
if cfg.TIMINGS[fn_name] > 20:
await func.log_timings(client, fn_name)
# Check for new patrons using patron role in support server
if cfg.SAPPHIRE_ID is None:
try:
num_patrons = len(client.get_guild(601015720200896512).get_role(606482184043364373).members)
except AttributeError:
pass
else:
if cfg.NUM_PATRONS != num_patrons:
if cfg.NUM_PATRONS != -1: # Skip first run, since patrons are fetched on startup already.
await func.check_patreon(force_update=(not DEV_BOT), client=client)
cfg.NUM_PATRONS = num_patrons
@loop(seconds=cfg.CONFIG['loop_interval'])
async def creation_loop(client):
creation_loop.last_run = datetime.now(pytz.utc)
@utils.func_timer()
async def check_create(guild, settings):
for pid in settings['auto_channels']:
p = client.get_channel(int(pid))
if p is not None:
users_waiting = [m for m in p.members if not func.user_request_is_locked(m)]
for u in users_waiting:
await func.create_secondary(guild, p, u)
start_time = time()
if client.is_ready():
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
thread_ = executor.submit(func.get_guilds, client)
while not thread_.done():
await asyncio.sleep(0.1)
guilds = thread_.result()
for guild in guilds:
settings = utils.get_serv_settings(guild)
if settings['enabled'] and settings['auto_channels']:
await check_create(guild, settings)
end_time = time()
fn_name = "creation_loop"
cfg.TIMINGS[fn_name] = end_time - start_time
if cfg.TIMINGS[fn_name] > 20:
await func.log_timings(client, fn_name)
@loop(seconds=cfg.CONFIG['loop_interval'] * 2)
async def deletion_loop(client):
deletion_loop.last_run = datetime.now(pytz.utc)
@utils.func_timer()
async def check_empty(guild, settings):
# Delete empty secondaries, in case they didn't get caught somehow (e.g. errors, downtime)
secondaries = func.get_secondaries(guild, settings)
voice_channels = [x for x in guild.channels if isinstance(x, discord.VoiceChannel)]
for v in voice_channels:
if v.name != "⌛": # Ignore secondary channels that are currently being created
if v.id in secondaries:
if not v.members:
await func.delete_secondary(guild, v)
start_time = time()
if client.is_ready():
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
thread_ = executor.submit(func.get_guilds, client)
while not thread_.done():
await asyncio.sleep(0.1)
guilds = thread_.result()
for guild in guilds:
settings = utils.get_serv_settings(guild)
if settings['enabled'] and settings['auto_channels']:
await check_empty(guild, settings)
await func.remove_broken_channels(guild)
end_time = time()
fn_name = "deletion_loop"
cfg.TIMINGS[fn_name] = end_time - start_time
if cfg.TIMINGS[fn_name] > 20:
await func.log_timings(client, fn_name)
@loop(minutes=2)
async def check_dead(client):
check_dead.last_run = datetime.now(pytz.utc)
def for_looper(client):
for guild in func.get_guilds(client):
settings = utils.get_serv_settings(guild) # Need fresh in case some were deleted
dead_secondaries = []
for p in settings['auto_channels']:
for sid, sv in settings['auto_channels'][p]['secondaries'].items():
s = client.get_channel(sid)
if s is None:
dying = sv['dying'] + 1 if 'dying' in sv else 1
settings['auto_channels'][p]['secondaries'][sid]['dying'] = dying
cfg.GUILD_SETTINGS[guild.id] = settings # Temporarily settings, no need to write to disk.
log("{} is dead ({})".format(sid, dying), guild)
if dying >= 3:
dead_secondaries.append(sid)
else:
if 'dying' in sv:
settings['auto_channels'][p]['secondaries'][sid]['dying'] = 0
cfg.GUILD_SETTINGS[guild.id] = settings # Temporarily settings, no need to write to disk.
if dead_secondaries:
for p, pv in settings['auto_channels'].items():
tmp = {}
for s, sv in pv['secondaries'].items():
if s not in dead_secondaries:
tmp[s] = sv
settings['auto_channels'][p]['secondaries'] = tmp
utils.set_serv_settings(guild, settings)
for s in dead_secondaries:
if s in cfg.ATTEMPTED_CHANNEL_NAMES:
del cfg.ATTEMPTED_CHANNEL_NAMES[s]
start_time = time()
if client.is_ready():
with concurrent.futures.ThreadPoolExecutor() as pool:
await client.loop.run_in_executor(pool, for_looper, client)
end_time = time()
fn_name = "check_dead"
cfg.TIMINGS[fn_name] = end_time - start_time
if cfg.TIMINGS[fn_name] > 20:
await func.log_timings(client, fn_name)
@loop(seconds=2)
async def check_votekicks(client):
check_votekicks.last_run = datetime.now(pytz.utc)
start_time = time()
if client.is_ready():
to_remove = []
votekicks = list(cfg.VOTEKICKS.keys())
for mid in votekicks:
try:
vk = cfg.VOTEKICKS[mid]
except KeyError:
print("Ignoring error:")
traceback.print_exc()
continue
guild = vk['message'].guild
guilds = func.get_guilds(client)
if guild not in guilds:
return
in_favor = len(vk['in_favor'])
log("TESTVOTEKICK: {} ({}/{})".format(vk['offender'].display_name, in_favor, vk['required_votes']), guild)
if in_favor >= vk['required_votes']:
to_remove.append(mid)
log("Kicked {} from {} ({}/{})".format(vk['offender'].display_name,
vk['voice_channel'].name,
in_favor, len(vk['participants']) + 1), guild)
try:
await vk['offender'].move_to(None) # Kick
except Exception as e:
to_remove.append(mid)
await vk['message'].edit(
content="‼ **Votekick** failed - A `{}` error was encountered.".format(type(e).__name__))
continue
banned = True
try:
await vk['voice_channel'].set_permissions(vk['offender'], connect=False)
except discord.errors.Forbidden:
banned = False
await vk['message'].edit(content=(
"‼ **Votekick** ‼\n"
"{} was **kicked** from {}'s channel{}.{}".format(
vk['offender'].mention, vk['initiator'].mention,
(", but could not be banned from the channel as I don't have the *Manage Roles* permission."
if not banned else ""),
("\nReason: **{}**".format(vk['reason']) if vk['reason'] else ""))
))
await func.server_log(
guild,
"👢 {} (`{}`) has been **kicked** from {}'s channel.".format(
func.user_hash(vk['offender']), vk['offender'].id, vk['initiator']
), 1, utils.get_serv_settings(guild)
)
elif time() > vk['end_time'] + 5:
to_remove.append(mid)
log("VOTEKICK TIMED OUT: {} ({}/{}) {} {}".format(
vk['offender'].display_name, in_favor, vk['required_votes'], mid, type(mid)), guild)
await vk['message'].edit(content="‼ **Votekick** timed out: Insufficient votes received "
"({0}/{1}), required: {2}/{1}.".format(in_favor,
len(vk['participants']) + 1,
vk['required_votes']))
for mid in to_remove:
log("REMOVING VOTEKICK: {} {} len:{} keys:{}".format(
mid,
type(mid),
len(cfg.VOTEKICKS),
', '.join([str(k) + " " + str(type(k)) for k in cfg.VOTEKICKS.keys()])),
guild)
del cfg.VOTEKICKS[mid]
print("... len:{} keys:{}".format(
len(cfg.VOTEKICKS),
', '.join([str(k) + " " + str(type(k)) for k in cfg.VOTEKICKS.keys()])))
end_time = time()
fn_name = "check_votekicks"
cfg.TIMINGS[fn_name] = end_time - start_time
if cfg.TIMINGS[fn_name] > 20:
await func.log_timings(client, fn_name)
@loop(seconds=3)
async def create_join_channels(client):
create_join_channels.last_run = datetime.now(pytz.utc)
start_time = time()
if not client.is_ready():
return
to_remove = []
priv_channels = list(cfg.PRIV_CHANNELS.keys())
for pc in priv_channels:
try:
pcv = cfg.PRIV_CHANNELS[pc]
except KeyError:
print("Ignoring error:")
traceback.print_exc()
continue
if 'request_time' in pcv and time() - pcv['request_time'] > 120:
# Unable to create join channel for 120s
to_remove.append(pc)
await pcv['text_channel'].send(
":warning: {} For some reason I was unable to create your \"⇩ Join\" channel, please try again later. "
"Your channel is still private, but there's now no way for anyone to join you. "
"Use `{}public` to make it public again."
"".format(pcv['creator'].mention, pcv['prefix']))
log("Failed to create join-channel, timed out.")
continue
guild = client.get_guild(pcv['guild_id'])
if guild not in func.get_guilds(client):
continue
settings = utils.get_serv_settings(guild)
for p, pv in settings['auto_channels'].items():
for s, sv in pv['secondaries'].items():
if 'priv' in sv and 'jc' not in sv:
creator = pcv['creator'].display_name
vc = pcv['voice_channel']
c_position = vc.position
overwrites = vc.overwrites
k = guild.default_role
v = overwrites[k] if k in overwrites else discord.PermissionOverwrite()
v.update(connect=True)
overwrites[k] = v
try:
jc = await guild.create_voice_channel("⇩ Join {}".format(creator), # TODO creator can change
category=vc.category,
overwrites=overwrites)
except discord.errors.Forbidden:
to_remove.append(pc)
try:
await pcv['text_channel'].send(
":warning: {} I don't have permission to make the \"⇩ Join\" channel for you anymore."
"".format(pcv['creator'].mention))
except:
log("Failed to create join-channel, and failed to notify {}".format(creator))
break
utils.permastore_secondary(jc.id)
settings['auto_channels'][p]['secondaries'][s]['jc'] = jc.id
utils.set_serv_settings(guild, settings)
to_remove.append(pc)
try:
# Set position again, sometimes create_voice_channel gets it wrong.
await jc.edit(position=c_position)
except discord.errors.Forbidden:
# Harmless error, no idea why it sometimes throws this, seems like a bug.
pass
break
for i in to_remove:
try:
del cfg.PRIV_CHANNELS[i]
except KeyError:
# Already deleted somehow.
print("Ignoring error:")
traceback.print_exc()
pass
end_time = time()
fn_name = "create_join_channels"
cfg.TIMINGS[fn_name] = end_time - start_time
if cfg.TIMINGS[fn_name] > 10:
await func.log_timings(client, fn_name)
@loop(minutes=3)
async def update_seed(client):
update_seed.last_run = datetime.now(pytz.utc)
if client.is_ready():
cfg.SEED = int(time())
@loop(minutes=5)
async def dynamic_tickrate(client):
dynamic_tickrate.last_run = datetime.now(pytz.utc)
start_time = time()
if client.is_ready():
current_channels = utils.num_active_channels(func.get_guilds(client))
new_tickrate = current_channels / 7
new_tickrate = max(3, min(100, new_tickrate))
new_seed_interval = current_channels / 45
new_seed_interval = max(10, min(15, new_seed_interval))
if cfg.SAPPHIRE_ID is None:
print("New tickrate is {0:.1f}s, seed interval is {1:.2f}m".format(new_tickrate, new_seed_interval))
main_loop.change_interval(seconds=new_tickrate)
creation_loop.change_interval(seconds=new_tickrate)
deletion_loop.change_interval(seconds=new_tickrate * 2)
update_seed.change_interval(minutes=new_seed_interval)
cfg.TICK_RATE = new_tickrate
end_time = time()
fn_name = "dynamic_tickrate"
cfg.TIMINGS[fn_name] = end_time - start_time
if cfg.TIMINGS[fn_name] > 20:
await func.log_timings(client, fn_name)
def get_potentials():
with open(os.path.join(cfg.SCRIPT_DIR, "secondaries.txt"), 'r') as f:
potentials = f.read()
return potentials
@loop(minutes=5.22)
async def lingering_secondaries(client):
lingering_secondaries.last_run = datetime.now(pytz.utc)
start_time = time()
if client.is_ready():
potentials = None
with concurrent.futures.ThreadPoolExecutor() as pool:
potentials = await client.loop.run_in_executor(pool, get_potentials)
potentials = potentials.split('\n')
if potentials:
potentials = set(potentials[-10000:]) # Sets apparently give better performance. Discard all but last 10k.
for guild in func.get_guilds(client):
settings = utils.get_serv_settings(guild)
if not settings['enabled'] or not settings['auto_channels']:
continue
secondaries = func.get_secondaries(guild, settings=settings, include_jc=True)
voice_channels = [x for x in guild.channels if isinstance(x, discord.VoiceChannel)]
for v in voice_channels:
if v.id not in secondaries and str(v.id) in potentials and not func.channel_is_requested(v):
if v.name not in ['⌛', '⚠']:
try:
await v.edit(name='⚠')
log("Remembering channel {}".format(v.id), guild)
await func.admin_log(
"⚠ Remembering channel `{}` in guild **{}**".format(v.id, guild.name), client
)
except discord.errors.NotFound:
pass
except Exception:
traceback.print_exc()
end_time = time()
fn_name = "lingering_secondaries"
cfg.TIMINGS[fn_name] = end_time - start_time
if cfg.TIMINGS[fn_name] > 5:
await func.log_timings(client, fn_name)
@loop(hours=2.4)
async def analytics(client):
analytics.last_run = datetime.now(pytz.utc)
start_time = time()
if client.is_ready() and cfg.SAPPHIRE_ID is None:
fp = os.path.join(cfg.SCRIPT_DIR, "analytics.json")
guilds = func.get_guilds(client)
if not os.path.exists(fp):
data = {}
else:
data = utils.read_json(fp)
data[datetime.now(pytz.timezone(cfg.CONFIG['log_timezone'])).strftime("%Y-%m-%d %H:%M")] = {
'nc': utils.num_active_channels(guilds),
'tt': round(cfg.TICK_TIME, 2),
'tr': main_loop.seconds,
'ng': len(guilds),
'm': round(psutil.virtual_memory().used / 1024 / 1024 / 1024, 2),
}
with concurrent.futures.ThreadPoolExecutor() as pool:
await client.loop.run_in_executor(
pool, utils.write_json, fp, data)
end_time = time()
fn_name = "analytics"
cfg.TIMINGS[fn_name] = end_time - start_time
if cfg.TIMINGS[fn_name] > 10:
await func.log_timings(client, fn_name)
@loop(minutes=2)
async def update_status(client):
update_status.last_run = datetime.now(pytz.utc)
if client.is_ready():
guilds = func.get_guilds(client)
if guilds:
prefix = 'vc/'
if len(guilds) == 1 and guilds[0].id in cfg.PREFIXES:
prefix = cfg.PREFIXES[guilds[0].id]
nc = utils.num_active_channels(guilds)
text = "{}help | {} channel{}".format(prefix, nc, ("s" if nc != 1 else ""))
else:
text = "🚧No guilds🚧"
old_text = ""
try:
old_text = client.guilds[0].me.activity.name
except (IndexError, AttributeError, TypeError):
pass
if text != old_text:
try:
await client.change_presence(activity=discord.Activity(name=text, type=discord.ActivityType.watching))
log("Changing status to: {}".format(text.replace(' ', ' ')))
except Exception as e:
log("Failed to update status: {}".format(type(e).__name__))
@loop(hours=3)
async def check_patreon():
# Will run at startup too, since it doesn't have to wait for client.is_ready
await func.check_patreon(force_update=cfg.SAPPHIRE_ID in [None, 0] and not DEV_BOT)
loops = { # loops with client as only arg - passed to admin_commands's `loop` cmd
'main_loop': main_loop,
'deletion_loop': deletion_loop,
'check_dead': check_dead,
'check_votekicks': check_votekicks,
'create_join_channels': create_join_channels,
'update_seed': update_seed,
# 'dynamic_tickrate': dynamic_tickrate,
'lingering_secondaries': lingering_secondaries,
'analytics': analytics,
'update_status': update_status,
}
if 'disable_creation_loop' not in cfg.CONFIG or not cfg.CONFIG['disable_creation_loop']:
loops['creation_loop'] = creation_loop
async def check_all_channels(guild, settings):
@utils.func_timer()
async def check_rename(guild, settings):
# Update secondary channel names
settings = utils.get_serv_settings(guild) # Need fresh in case some were deleted
templates = {'0': 0} # Initialize with 0's to prevent checking again if empty
for p in settings['auto_channels']:
secondaries = []
for sid, sv in settings['auto_channels'][p]['secondaries'].items():
s = client.get_channel(sid)
if s is not None:
secondaries.append(s)
if "template" in settings['auto_channels'][p]:
templates[s.id] = settings['auto_channels'][p]['template']
if "name" in sv:
templates[s.id] = sv['name']
secondaries = sorted(secondaries, key=lambda x: discord.utils.snowflake_time(x.id))
for i, s in enumerate(secondaries):
await func.rename_channel(guild=guild,
channel=s,
settings=settings,
primary_id=None,
templates=templates,
i=i)
if guild is None or guild.name is None:
# Weird ghostly disconnect where things that shouldn't be possible happen.
if not cfg.DISCONNECTED:
cfg.DISCONNECTED = True
print("There's something strange in the neighborhood.")
await func.admin_log("Disconnecting ⁉", client, important=True)
return
cfg.DISCONNECTED = False
timings = {}
try:
await check_rename(guild, settings)
except Exception:
traceback.print_exc()
return timings
class MyClient(discord.AutoShardedClient):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
async def on_ready(self):
print('=' * 24)
curtime = datetime.now(pytz.timezone(cfg.CONFIG['log_timezone'])).strftime("%Y-%m-%d %H:%M")
print(curtime)
print('Logged in as')
print(self.user.name)
print(self.user.id)
if cfg.SAPPHIRE_ID is not None:
print("Sapphire:", cfg.SAPPHIRE_ID)
print("discordpy version: {}".format(discord.__version__))
print('-' * 24)
shards = {}
for g in func.get_guilds(self):
if g.shard_id in shards:
shards[g.shard_id] += 1
else:
shards[g.shard_id] = 1
settings = utils.get_serv_settings(g)
if 'prefix' in settings:
cfg.PREFIXES[g.id] = settings['prefix']
print("Shards:", len(shards))
for s in shards:
print("s{}: {} guilds".format(s, shards[s]))
print('=' * 24)
if 'disable_ready_message' in cfg.CONFIG and cfg.CONFIG['disable_ready_message']:
log("READY")
else:
await func.admin_log("🟥🟧🟨🟩 **Ready** 🟩🟨🟧🟥", self)
heartbeat_timeout = cfg.CONFIG['heartbeat_timeout'] if 'heartbeat_timeout' in cfg.CONFIG else 60
if NUM_SHARDS > 1:
client = MyClient(shard_count=NUM_SHARDS, heartbeat_timeout=heartbeat_timeout)
else:
client = MyClient(heartbeat_timeout=heartbeat_timeout)
async def reload_modules(m):
try:
if m:
commands.reload_command(m)
from importlib import reload
reload(commands)
reload(utils)
reload(func)
reload(translate)
cfg.CONFIG = utils.get_config()
return True
except:
await func.admin_log(traceback.format_exc(), client)
return False
# ----- COMMANDS -----
@client.event
async def on_message(message):
if not client.is_ready():
return
if message.author.bot:
# Don't respond to self or bots
return
guilds = func.get_guilds(client)
admin = ADMIN
admin_channels = []
if admin is not None:
admin_channels = [admin.dm_channel]
if 'admin_channel' in cfg.CONFIG and ADMIN_CHANNEL is not None:
admin_channels.append(ADMIN_CHANNEL)
if message.channel in admin_channels:
split = message.content.split(' ')
cmd = split[0].split('\n')[0].lower()
params_str = message.content[len(cmd):].strip()
params = params_str.split(' ')
if cmd == 'reload':
m = utils.strip_quotes(params_str)
success = await reload_modules(m)
await func.react(message, '✅' if success else '❌')
else:
ctx = {
'client': client,
'admin': admin,
'message': message,
'params': params,
'params_str': params_str,
'guilds': guilds,
'LAST_COMMIT': LAST_COMMIT,
'loops': loops,
}
await admin_commands.admin_command(cmd, ctx)
return
if not message.guild: # DM
if 'help' in message.content and len(message.content) <= len("@Auto Voice Channels help"):
await message.channel.send("Sorry I don't respond to commands in DMs, "
"you need to type the commands in a channel in your server.\n"
"If you've tried that already, then make sure I have the right permissions "
"to see and reply to your commands in that channel.")
elif message.content.lower().startswith("power-overwhelming"):
channel = message.channel
params_str = message.content[len("power-overwhelming"):].strip()
if not params_str:
await channel.send("You need to specify a guild ID. "
"Try typing `who am I` to get a list of guilds we're both in")
return
auth_guilds = params_str.replace(' ', '\n').split('\n')
for auth_guild in auth_guilds:
try:
g = client.get_guild(int(auth_guild))
if g is None:
await channel.send("`{}` is not a guild I know about, "
"maybe you need to invite me there first?".format(auth_guild))
return
except ValueError:
await channel.send("`{}` is not a valid guild ID, try typing "
"`who am I` to get a list of guilds we're both in.".format(auth_guild))
return
except Exception as e:
error_text = "Auth Error `{}`\nUser `{}`\nCMD `{}`".format(type(e).__name__,
message.author.id,
message.content)
await func.admin_log(error_text, ctx['client'])
log(error_text)
error_text = traceback.format_exc()
await func.admin_log(error_text, ctx['client'])
log(error_text)
return False, ("A `{}` error occured :(\n"
"An admin has been notified and will be in touch.\n"
"In the meantime, try asking for help in the support server: "
"https://discord.gg/qhMrz6u".format(type(e).__name__))
ctx = {
'message': message,
'channel': channel,
'client': client,
}
auth_guilds = [int(g) for g in auth_guilds]
success, response = await func.power_overwhelming(ctx, auth_guilds)
if success or response != "NO RESPONSE":
log("DM CMD {}: {}".format("Y" if success else "F", message.content))
if success:
if response:
if response != "NO RESPONSE":
await echo(response, channel, message.author)
else:
await func.react(message, '✅')
else:
if response != "NO RESPONSE":
await func.react(message, '❌')
if response:
await echo(response, channel, message.author)
elif message.content.lower() in ["who am i", "who am i?"]:
in_guilds = []
for g in client.guilds:
if message.author in g.members:
in_guilds.append("`{}` **{}**".format(g.id, g.name))
if in_guilds:
await message.channel.send("We're both in the following guilds:\n{}".format('\n'.join(in_guilds)))
else:
await message.channel.send("I'm not in any of the same guilds as you.")
else:
await admin_channels[-1].send(embed=discord.Embed(
title="DM from **{}** [`{}`]:".format(message.author.name, message.author.id),
description=message.content
))
return
if message.guild not in guilds:
return
prefix_m = message.guild.me.mention
prefix_mx = "<@!" + prefix_m[2:]
if message.guild.id in cfg.PREFIXES:
prefix_p = cfg.PREFIXES[message.guild.id]
else:
prefix_p = 'vc/'
prefix = None
if message.content.startswith(prefix_m):
prefix = prefix_m
print_prefix = "@{} ".format(message.guild.me.display_name)
elif message.content.startswith(prefix_mx):
prefix = prefix_mx
print_prefix = "@{} ".format(message.guild.me.display_name)
elif message.content.lower().startswith(prefix_p.lower()):
prefix = prefix_p
print_prefix = prefix_p
# Commands
if prefix:
msg = message.content[len(prefix):].strip() # Remove prefix
split = msg.split(' ')
cmd = split[0].lower()
params = split[1:]
params_str = ' '.join(params)
clean_paramstr = ' '.join(message.clean_content[len(prefix):].strip().split(' ')[1:])
guild = message.guild
channel = message.channel
settings = utils.get_serv_settings(guild)
if channel.id not in func.get_voice_context_channel_ids(guild, settings):
settings['last_channel'] = channel.id
utils.set_serv_settings(guild, settings)
ctx = {
'client': client,
'guild': guild,
'prefix': prefix,
'print_prefix': print_prefix,
'prefix_p': prefix_p,
'command': cmd,
'gold': func.is_gold(guild),
'sapphire': func.is_sapphire(guild),
'settings': settings,
'message': message,
'channel': channel,
'clean_paramstr': clean_paramstr,
}
# Restricted commands
perms = message.author.permissions_in(channel)
perms_required = [
perms.manage_channels,
perms.manage_roles,
]
ctx['admin'] = all(perms_required)
success, response = await commands.run(cmd, ctx, params)
if success or response != "NO RESPONSE":
log("CMD {}: {}".format("Y" if success else "F", msg), guild)
if success:
if response:
if response != "NO RESPONSE":
await echo(response, channel, message.author)
else:
await func.react(message, '✅')
else:
if response != "NO RESPONSE":
await func.react(message, '❌')
if response:
await echo(response, channel, message.author)
@client.event
async def on_reaction_add(reaction, user):
if not client.is_ready():
return
if user.bot:
return
guild = reaction.message.guild
guilds = func.get_guilds(client)
if guild not in guilds:
return
if reaction.message.id in cfg.VOTEKICKS:
if reaction.emoji == '✅':
vk = cfg.VOTEKICKS[reaction.message.id]
if time() < vk['end_time']:
if user not in vk['in_favor'] and user in vk['participants']:
vk['in_favor'].append(user)
log("{} voted to kick {}".format(user.display_name, vk['offender'].display_name), guild)
return
to_delete = []
joins_in_progress = list(cfg.JOINS_IN_PROGRESS.keys())
for uid in joins_in_progress:
try:
j = cfg.JOINS_IN_PROGRESS[uid]
except KeyError:
print("Ignoring error:")
traceback.print_exc()
continue
if reaction.message.id == j['mid'] and user.id == j['creator'].id:
reacted = False
if reaction.emoji == '✅':
reacted = True
try:
await j['vc'].set_permissions(j['requester'], connect=True)
await j['requester'].move_to(j['vc'])
except discord.errors.Forbidden:
await j['msg'].edit(content=":warning: I don't have permission to move {} to **{}** :(".format(
j['requester'].mention, func.esc_md(j['vc'].name)
))
except discord.errors.HTTPException as e:
await j['msg'].edit(content=":warning: Unable to move {} to {}'s channel ({})".format(
j['requester'].mention, j['creator'].mention, e.text
))
else:
await j['msg'].delete(delay=5)
elif reaction.emoji in ['❌', '⛔']:
reacted = True
try:
await j['requester'].move_to(None)
except discord.errors.Forbidden:
pass
except discord.errors.HTTPException:
pass
else:
await j['msg'].edit(content="Sorry {}, your request to join {} was denied.".format(
j['requester'].mention, j['creator'].mention
))
if reaction.emoji == '⛔':
try:
await j['jc'].set_permissions(j['requester'], connect=False)
except Exception as e:
await j['msg'].edit(content="{}\nFailed to block user ({}).".format(j['msg'].content,
type(e).__name__))
if reacted: