forked from gregzaal/Auto-Voice-Channels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
561 lines (457 loc) · 14.4 KB
/
utils.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
import asyncio
import discord
import functools
import json
import operator
import os
import traceback
from contextlib import contextmanager
from copy import deepcopy
from datetime import datetime
from itertools import islice
from random import choice, seed
from requests import get
from time import time
from inspect import currentframe as iframe
import cfg
import pytz
def func_timer(threshold=0.5):
def duration(func):
@contextmanager
def wrapping_logic(parent_func):
start_ts = time()
yield
duration = time() - start_ts
if duration >= threshold:
print("TIMER: {0:.3f}s '{1}' called by '{2}'".format(duration, func.__name__, parent_func))
@functools.wraps(func)
def wrapper(*args, **kwargs):
parent_func = iframe().f_back.f_code.co_name
if not asyncio.iscoroutinefunction(func):
with wrapping_logic(parent_func):
return func(*args, **kwargs)
else:
async def tmp():
with wrapping_logic(parent_func):
return await func(*args, **kwargs)
return tmp()
return wrapper
return duration
@func_timer()
def format_timings():
s = ""
l = [[k, cfg.TIMINGS[k]] for k in cfg.TIMINGS]
l.sort(key=lambda k: k[1], reverse=True)
for t in l:
s += "`{0}`: {1:.2f}\n".format(t[0], t[1])
return s.strip()
@func_timer()
def log(msg, guild=None):
text = datetime.now(pytz.timezone(cfg.CONFIG['log_timezone'])).strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
if guild:
text += " [{}]{}".format(
guild.name,
"<{}>".format(guild.shard_id) if guild.shard_id != 0 else ""
)
text += "\n "
text += str(msg)
print(text)
@func_timer()
def read_json(fp):
with open(fp, 'r') as f:
data = json.load(f)
return data
@func_timer()
def write_json(fp, data, indent=1):
cfg.WRITES_IN_PROGRESS.append(fp)
d = os.path.dirname(fp)
if not os.path.exists(d):
os.makedirs(d)
try:
s = json.dumps(data, indent=indent, separators=(",", ":"), sort_keys=True)
except:
traceback.print_exc()
else:
with open(fp, 'w') as f:
f.write(s)
cfg.WRITES_IN_PROGRESS.remove(fp)
@func_timer()
def get_config():
cf = os.path.join(cfg.SCRIPT_DIR, 'config.json')
if not os.path.exists(cf):
print("Config file doesn't exist!\n"
"You need to create a 'config.json' file next to this script and fill in some details like your token.\n"
"Read the instructions on GitHub: https://github.com/gregzaal/Auto-Voice-Channels")
import sys
sys.exit(0)
return read_json(cf)
@func_timer()
def set_config(data):
cf = os.path.join(cfg.SCRIPT_DIR, 'config.json')
if not os.path.exists(cf):
print("Config file doesn't exist!")
import sys
sys.exit(0)
return write_json(cf, data, indent=4)
@func_timer()
def update_server_location():
try:
print("Getting server location...")
j = json.loads(get("http://ip-api.com/json/").text)
cfg.SERVER_LOCATION = "{}, {}, {}".format(j['city'], j['region'], j['country'])
print(cfg.SERVER_LOCATION)
except:
print("Failed to update server location")
print(traceback.format_exc())
@func_timer()
def get_serv_settings(guild, force_refetch=False):
if guild.id in cfg.GUILD_SETTINGS and not force_refetch:
cfg.PREV_GUILD_SETTINGS[guild.id] = deepcopy(cfg.GUILD_SETTINGS[guild.id])
return cfg.GUILD_SETTINGS[guild.id]
fp = os.path.join(cfg.SCRIPT_DIR, 'guilds', str(guild.id) + '.json')
if not os.path.exists(fp):
write_json(fp, read_json(os.path.join(cfg.SCRIPT_DIR, 'default_settings.json')))
data = read_json(fp)
# Convert string IDs to ints
old_data = deepcopy(data)
for p, v in old_data['auto_channels'].items():
del data['auto_channels'][p]
if isinstance(v['secondaries'], list):
# Convert old secondaries[] to secondaries{}
v['secondaries'] = dict.fromkeys(v['secondaries'], {"creator": 1})
old_v = deepcopy(v)
for s, sv in old_v['secondaries'].items():
del v['secondaries'][s]
v['secondaries'][int(s)] = sv
data['auto_channels'][int(p)] = v
if 'creators' in data: # Old method of storing creators
del data['creators']
cfg.GUILD_SETTINGS[guild.id] = data
cfg.PREV_GUILD_SETTINGS[guild.id] = data
return cfg.GUILD_SETTINGS[guild.id]
@func_timer()
def set_serv_settings(guild, settings):
# prev_settings = cfg.PREV_GUILD_SETTINGS[guild.id]
# prev_num_channels = 0
# for p in prev_settings['auto_channels']:
# prev_num_channels += len(prev_settings['auto_channels'][p]['secondaries'])
# num_channels = 0
# for p in settings['auto_channels']:
# num_channels += len(settings['auto_channels'][p]['secondaries'])
# if num_channels < prev_num_channels:
# print("{}REM:{} {} ln:{} fn:{} gt:{} gnt:{}".format(
# ' ' * 16,
# prev_num_channels - num_channels,
# guild.id,
# iframe().f_back.f_lineno,
# iframe().f_back.f_code.co_name,
# type(guild).__name__,
# type(guild.name).__name__)
# )
settings['guild_name'] = guild.name
cfg.GUILD_SETTINGS[guild.id] = settings
fp = os.path.join(cfg.SCRIPT_DIR, 'guilds', str(guild.id) + '.json')
return write_json(fp, settings)
@func_timer()
def permastore_secondary(cid):
success = False
attempts = 0
while not success and attempts < 100:
attempts += 1
try:
with open(os.path.join(cfg.SCRIPT_DIR, "secondaries.txt"), 'a') as f:
f.write(str(cid) + '\n')
except:
print("Failed to remember {}".format(cid))
else:
success = True
@func_timer()
def clean_permastore():
fp = os.path.join(cfg.SCRIPT_DIR, "secondaries.txt")
if not os.path.exists(fp):
return
with open(fp, 'r') as f:
lines = f.readlines()
lines = lines[-10000:] # Drop all but the last 10k lines
with open(fp, 'w') as f:
f.writelines(lines)
@func_timer()
def count_lines(fp):
n = 0
with open(fp, 'r', encoding="utf8") as f:
n += len([l for l in f.readlines() if l.strip()])
return n
@func_timer()
def get_primary_channel(guild, settings, channel):
for p, v in settings['auto_channels'].items():
for s in v['secondaries']:
if s == channel.id:
return guild.get_channel(p)
@func_timer()
def get_creator_id(settings, channel):
for p, pv in settings['auto_channels'].items():
for s, sv in pv['secondaries'].items():
if s == channel.id:
return sv['creator']
@func_timer()
def plain_mention(mention):
# Remove '!' from mention so that user and member mentions are the same.
return mention.replace('!', '')
@func_timer()
def get_user_in_channel(name, channel):
members = channel.members
name = name.strip()
for m in members:
if plain_mention(m.mention) == plain_mention(name):
return m
for m in members:
if m.name.lower() + '#' + m.discriminator == name.lower():
return m
for m in members:
if m.display_name.lower() == name.lower():
return m
return False
@func_timer()
def num_active_channels(guilds):
num_channels = 0
for g in guilds:
settings = get_serv_settings(g)
for p in settings['auto_channels']:
num_channels += len(settings['auto_channels'][p]['secondaries'])
return num_channels
@func_timer()
def guild_is_active(g):
curtime = time()
settings = get_serv_settings(g)
if 'last_activity' in settings:
age = curtime - settings['last_activity']
if age / 604800 <= 6: # 6 Weeks
return True
return False
@func_timer()
def num_active_guilds(guilds):
num_guilds = 0
for g in guilds:
if guild_is_active(g):
num_guilds += 1
return num_guilds
@func_timer()
def num_shards(guilds):
shards = []
for g in guilds:
if g.shard_id not in shards:
shards.append(g.shard_id)
return len(shards)
@func_timer()
def guild_size_icon(n):
if n < 500:
return ""
elif n < 1500:
return "💜"
elif n < 10000:
return "✨"
elif n < 100000:
return "🔥💗"
else:
return "💥🔥💗✨"
@func_timer()
def ldir(o):
''' Get all attributes/functions of an object, return them as a string in a nice format '''
return '[\n' + (',\n'.join(dir(o))) + '\n]'
@func_timer()
def fmsg(m):
# Format message to display in a code block
s = '```\n'
s += str(m)
s += '\n```'
return s
@func_timer()
def strip_quotes(s):
# TODO just use s.strip("\"' ")
chars_to_strip = ['\'', '"', ' ']
if s:
while s[0] in chars_to_strip:
if len(s) <= 1:
break
s = s[1:]
while s[-1] in chars_to_strip:
if len(s) <= 1:
break
s = s[:-1]
return s
@func_timer()
def match_case(target, source):
if source.isupper():
return target.upper()
if source.islower():
return target.lower()
if source.istitle():
return target.title()
if len(source) > 1:
if source[0].isupper() and source[1].isupper():
return target.upper()
if source[0].islower() and source[1].islower():
return target.lower()
if source[0].isupper() and source[1].islower():
return target.title()
return target
@func_timer()
def capitalize(s):
# s.title() does bad stuff with apostrophes, need to use our own method.
return ' '.join(x.capitalize() for x in s.split())
@func_timer()
def random_case(s):
seed_s = datetime.now().strftime("%Y%m%d%H") + s
seed_i = 0
for c in seed_s:
seed_i += ord(c)
ops = [str.upper, str.lower]
new_s = ""
for i, c in enumerate(s):
seed(seed_i + i)
new_s += choice(ops)(c)
return new_s
@func_timer()
def first_n_words(s, n=1):
return ' '.join(s.split(' ')[:n])
@func_timer()
def acronym(s):
words = s.split(' ')
return ''.join(w[0] for w in words if w)
@func_timer()
def remove_short_words(s):
words = s.split(' ')
short_words = "a an and at by from in is of on or the to".split(' ')
new_words = [w for w in words if w.lower() not in short_words]
return ' '.join(new_words)
@func_timer()
def full_strip(s):
s = s.strip()
while ' ' in s:
s = s.replace(' ', ' ')
return s
@func_timer()
def upsidedown(s):
try:
import upsidedown as _upsidedown
return ''.join((_upsidedown.transform(s)))
except ImportError:
log("Cannot import upsidedown")
return s
@func_timer()
def ascii_only(s):
ns = ""
printable_chars = list([chr(i) for i in range(32, 127)])
for c in s:
if c in printable_chars:
ns += c
else:
ns += '_'
return ns
@func_timer()
def nice_cname(text):
text = text.replace('/', ' ⁄ ')
text = text.replace(' ', ' ') # Fake space character
return text
@func_timer()
def get_display_name(settings, user):
uid = str(user.id)
if 'custom_nicks' in settings:
if uid in settings['custom_nicks']:
return settings['custom_nicks'][uid]
return user.display_name
@func_timer()
def eval_expression(text, is_sapphire, creator, party, game_name):
act = creator.activity
variables = {
'ROLE': [r.id for r in creator.roles],
'LIVE': ((creator.voice and hasattr(creator.voice, 'self_stream') and creator.voice.self_stream) or
(act and act.type == discord.ActivityType.streaming)),
'LIVE_DISCORD': creator.voice and hasattr(creator.voice, 'self_stream') and creator.voice.self_stream,
'LIVE_EXTERNAL': act and act.type == discord.ActivityType.streaming,
'GAME': game_name,
}
if is_sapphire:
try:
variables['PLAYERS'] = int(party['num_playing']) if party else 0
except ValueError:
variables['PLAYERS'] = 0
try:
variables['MAX'] = int(party['size']) if party else 0
except (ValueError, IndexError):
variables['MAX'] = 0
variables['RICH'] = party['rich'] if party else False
ops = [ # 2D list - can't use dict as it needs to be ordered
["<=", operator.le],
[">=", operator.ge],
["<", operator.lt],
[">", operator.gt],
["!=", operator.ne],
["=", operator.eq],
[":", operator.contains],
]
if '??' not in text:
return text
c, t = text.split('??', 1)
if '//' in t:
t, f = t.split('//', 1)
else:
f = ""
ch = c.strip()
cv = None
op = None
fn = None
for o in ops:
if o[0] in c:
op = o[0]
fn = o[1]
ch, cv = c.split(op, 1)
ch = ch.strip()
cv = cv.strip()
break
if ch in variables:
ch = variables[ch]
if op:
try:
cv = int(cv)
except ValueError:
pass
try:
c = fn(ch, cv)
except TypeError:
pass
else:
c = bool(ch)
return t if c and isinstance(c, bool) else f
@func_timer()
def debug_unicode(s):
text = ""
for c in s:
if 32 <= ord(c) <= 126:
text += c
else:
text += '`'
text += str(ord(c))
text += '` '
if text == s:
# All simple ascii characters, no need to print anything
return ""
else:
return '(' + text + ')'
@func_timer()
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]
@func_timer()
def dict_chunks(data, size=25):
it = iter(data)
for i in range(0, len(data), size):
yield {k: data[k] for k in islice(it, size)}
@func_timer()
async def hastebin(s):
from aiohttp import ClientSession
url = "https://hastebin.com"
async with ClientSession() as session:
async with session.post(url + "/documents", data=s.encode('utf-8')) as post:
return url + '/' + (await post.json())['key']