-
Notifications
You must be signed in to change notification settings - Fork 50
/
management.py
535 lines (486 loc) · 19.5 KB
/
management.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
"""This is a cog for a discord.py bot.
It will add some management commands to a bot.
Commands:
version show the hash of the latest commit
load load an extension / cog
unload unload an extension / cog
reload reload an extension / cog
cogs show currently active extensions / cogs
list make felix compute a list
└duplicates find duplicate usernames
pull pull latest changes from github (superuser only)
error print the traceback of the last unhandled error to chat
Only users that have an admin role can use the commands.
"""
import subprocess
import json
import traceback
import typing
from datetime import datetime, timezone
from os import path, listdir
from discord import Activity, Embed, Member
from discord.ext import commands
class Management(commands.Cog, name='Management'):
def __init__(self, client):
self.client = client
self.reload_config()
async def cog_check(self, ctx):
return self.client.user_is_admin(ctx.author)
@commands.Cog.listener()
async def on_ready(self):
loaded = self.client.extensions
unloaded = [x for x in self.crawl_cogs() if x not in loaded]
# Cogs without extra in their name should be loaded at startup so if
# any cog without "extra" in it's name is unloaded here -> Error in cog
if any('extra' not in cog_name for cog_name in unloaded):
activity_name = 'ERROR in cog'
activity_type = 3
else:
felix_version = self.get_version_info()[0][:7]
activity_name = f'on {felix_version}'
activity_type = 0
await self.client.change_presence(
activity=Activity(name=activity_name, type=activity_type)
)
@commands.Cog.listener()
async def on_member_join(self, member):
if self.client.flood_mode:
return
await self.client.main_guild.system_channel.send(
f'Welcome to the Engineer Man Discord Server, {member.mention}\n'
'Please take a moment to review the server rules in <#484103976296644608>, '
'say `felix help` to learn about available commands, '
'and finally, please be kind and decent to one another and enjoy your stay.'
)
# ----------------------------------------------
# Error handler
# ----------------------------------------------
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
if isinstance(error, commands.CommandNotFound):
return
if isinstance(error, commands.CommandOnCooldown):
await ctx.send(error)
return
if isinstance(error, commands.MissingRequiredArgument):
par = str(error.param)
missing = par.split(": ")[0]
if ':' in par:
missing_type = ' (' + str(par).split(": ")[1] + ')'
else:
missing_type = ''
await ctx.send(
f'Missing parameter: `{missing}{missing_type}`' +
f'\nIf you are not sure how to use the command, try running ' +
f'`felix help {ctx.command.qualified_name}`'
)
return
if isinstance(error, commands.CheckFailure):
await ctx.send('Sorry, you are not allowed to run this command.')
return
if isinstance(error, commands.BadArgument):
# It's in an embed to prevent mentions from working
embed = Embed(
title='Error',
description=str(error),
color=0x2ECC71
)
await ctx.send(embed=embed)
return
if isinstance(error, commands.UnexpectedQuoteError):
await ctx.send('`Unexpected quote encountered`')
return
# In case of an unhandled error -> Save the error + current datetime + ctx + original text
# so it can be accessed later with the error command
await ctx.send('Sorry, something went wrong. The Error was saved - we will look into it.')
self.client.last_errors.append((error, datetime.utcnow(), ctx, ctx.message.content))
await self.client.change_presence(
activity=Activity(name='ERROR encountered', url=None, type=3)
)
print(f'Ignoring exception in command {ctx.command}:', flush=True)
traceback.print_exception(
type(error), error, error.__traceback__
)
print('-------------------------------------------------------------', flush=True)
def reload_config(self):
with open("../config.json") as conffile:
self.client.config = json.load(conffile)
def get_version_info(self):
version = 'unknown'
date = 'unknown'
try:
gitlog = subprocess.check_output(
['git', 'log', '-n', '1', '--date=iso']).decode()
for line in gitlog.split('\n'):
if line.startswith('commit'):
version = line.split(' ')[1]
elif line.startswith('Date'):
date = line[5:].strip()
date = date.replace(' +', '+').replace(' ', 'T')
else:
pass
except Exception as e:
self.client.last_errors.append((e, datetime.utcnow(), None))
raise e
return (version, date)
async def get_remote_commits(self):
last_commit = self.get_version_info()[0]
ext = f'?per_page=10&sha=master'
repo = 'engineer-man/felix'
nxt = f'https://api.github.com/repos/{repo}/commits{ext}'
repo_data = []
repo_shas = []
while last_commit not in repo_shas:
async with self.client.session.get(nxt) as response:
r = await response.json()
repo_data += r
repo_shas = [x['sha'] for x in repo_data]
try:
nxt = r.links['next']['url']
except:
nxt = ''
num_comm = repo_shas.index(last_commit)
return (num_comm, repo_data[0:(num_comm if num_comm > 10 else 10)])
def crawl_cogs(self, directory='cogs'):
cogs = []
for element in listdir(directory):
if element == 'samples':
continue
abs_el = path.join(directory, element)
if path.isdir(abs_el):
cogs += self.crawl_cogs(abs_el)
else:
filename, ext = path.splitext(element)
if ext == '.py':
dot_dir = directory.replace('\\', '.')
dot_dir = dot_dir.replace('/', '.')
cogs.append(f'{dot_dir}.' + filename)
return cogs
# ----------------------------------------------
# Function to disply the version
# ----------------------------------------------
@commands.command(
name='version',
brief='Show current version of felix',
description='Show current version and changelog of felix',
hidden=True,
)
async def version(self, ctx):
#await ctx.trigger_typing()
version, date = self.get_version_info()
num_commits, remote_data = await self.get_remote_commits()
status = "I am up to date with 'origin/master'"
changelog = 'Changelog:\n'
if num_commits:
status = f"I am [{num_commits}] commits behind 'origin/master'"\
f" [{remote_data[0]['commit']['author']['date']}]"
for i, commit in enumerate(remote_data):
commitmessage = commit['commit']['message']
if 'merge pull' in commitmessage.lower():
continue
changelog += ('+ ' if i < num_commits else '* ') \
+ commitmessage.split('\n')[0] + '\n'
await ctx.send(
f'```css\nCurrent Version: [{version[:7]}].from [{date}]' +
f'\n{status}``````diff\n{changelog}```'
)
# ----------------------------------------------
# Function to load extensions
# ----------------------------------------------
@commands.command(
name='load',
brief='Load bot extension',
description='Load bot extension\n\nExample: felix load cogs.stats',
hidden=True,
)
async def load_extension(self, ctx, extension_name):
for cog_name in self.crawl_cogs():
if extension_name in cog_name:
target_extension = cog_name
break
try:
self.client.load_extension(target_extension)
except Exception as e:
self.client.last_errors.append((e, datetime.utcnow(), ctx))
await ctx.send(f'```py\n{type(e).__name__}: {str(e)}\n```')
return
await ctx.send(f'```css\nExtension [{target_extension}] loaded.```')
# ----------------------------------------------
# Function to unload extensions
# ----------------------------------------------
@commands.command(
name='unload',
brief='Unload bot extension',
description='Unload bot extension\n\nExample: felix unload cogs.stats',
hidden=True,
)
async def unload_extension(self, ctx, extension_name):
for cog_name in self.client.extensions:
if extension_name in cog_name:
target_extension = cog_name
break
if target_extension.lower() in 'cogs.management':
await ctx.send(
f"```diff\n- {target_extension} can't be unloaded" +
f"\n+ try felix reload {target_extension}!```"
)
return
if self.client.extensions.get(target_extension) is None:
return
self.client.unload_extension(target_extension)
await ctx.send(f'```css\nExtension [{target_extension}] unloaded.```')
# ----------------------------------------------
# Function to reload extensions
# ----------------------------------------------
@commands.command(
name='reload',
brief='Reload bot extension',
description='Reload bot extension\n\nExample: felix reload cogs.stats',
hidden=True,
aliases=['re']
)
async def reload_extension(self, ctx, extension_name):
target_extensions = []
if extension_name == 'all':
target_extensions = [__name__] + \
[x for x in self.client.extensions if not x == __name__]
else:
for cog_name in self.client.extensions:
if extension_name in cog_name:
target_extensions = [cog_name]
break
if not target_extensions:
return
result = []
for ext in target_extensions:
try:
self.client.reload_extension(ext)
result.append(f'Extension [{ext}] reloaded.')
except Exception as e:
self.client.last_errors.append((e, datetime.utcnow(), ctx))
result.append(f'#ERROR loading [{ext}]')
continue
result = '\n'.join(result)
await ctx.send(f'```css\n{result}```')
# ----------------------------------------------
# Function to get bot extensions
# ----------------------------------------------
@commands.command(
name='cogs',
brief='Get loaded cogs',
description='Get loaded cogs',
aliases=['extensions'],
hidden=True,
)
async def print_cogs(self, ctx):
loaded = self.client.extensions
unloaded = [x for x in self.crawl_cogs() if x not in loaded]
response = ['\n[Loaded extensions]'] + ['\n ' + x for x in loaded]
response += ['\n[Unloaded extensions]'] + \
['\n ' + x for x in unloaded]
await ctx.send(f'```css{"".join(response)}```')
return True
# ----------------------------------------------
# Function Group to clear channel of messages
# ----------------------------------------------
@commands.group(
invoke_without_command=True,
name='list',
hidden=True
)
async def _list(self, ctx):
"""List stuff"""
await ctx.send_help('list')
return True
@_list.command(
name='duplicates'
)
async def duplicates(self, ctx):
"""List duplicate usernames"""
name_count = {}
aka = {}
pages = []
usernames = [(x.name, x.display_name) for x in ctx.guild.members]
l_max = max([len(x[0]) for x in usernames]) + 1
for name, display_name in usernames:
name_count[name] = name_count.get(name, 0) + 1
if not name == display_name:
aka[name] = aka.get(name, []) + [display_name]
page = []
for key, value in sorted(name_count.items(),
key=lambda x: x[1], reverse=True):
if value == 1:
break
if len('\n'.join(page)) > 1900:
pages.append(page)
page = []
a = ', '.join(aka.get(key, []))
page.append(f'{value}x {key.ljust(l_max)}' + f'aka: {a}' * bool(a))
if page:
pages.append(page)
if not pages:
await ctx.send('No duplicate usernames found')
for n, page in enumerate(pages):
await ctx.send(f'{n+1}/{len(pages)}\n```' + '\n'.join(page) + '```')
@_list.command(
name='earliest'
)
async def earliest(self, ctx, n: int = 50, start: int = 0):
"""List earliest Members"""
sorted_members = sorted(self.client.main_guild.members, key=lambda x: x.joined_at)
await ctx.send(
'```\n' +
'\n'.join(f'{x.name} ({x.joined_at.strftime("%Y-%m-%d")})'
for x in sorted_members[start:start+n]) +
'\n```'
)
@_list.command(
name='oldest'
)
async def oldest(self, ctx, n: int = 50, start: int = 0):
"""List oldest Member accounts"""
sorted_members = sorted(self.client.main_guild.members, key=lambda x: x.created_at)
await ctx.send(
'```\n' +
'\n'.join(f'{x.name} ({x.created_at.strftime("%Y-%m-%d")})'
for x in sorted_members[start:start+n]) +
'\n```'
)
# ----------------------------------------------
# Function to get the date a member joined
# ----------------------------------------------
@commands.command(
name='joined',
hidden=True,
)
async def joined(self, ctx, members: commands.Greedy[Member]):
"""Print the date a member joined"""
if not members:
raise commands.BadArgument('Please specify at least 1 member')
#await ctx.trigger_typing()
result = []
now = datetime.now(tz=timezone.utc)
for member in members:
join = member.joined_at
if not join:
result.append(f'No join date found for {member.name}')
continue
difference = now - join
result.append(
f'{member.name} joined [{join.isoformat().split(".")[0]}] - '
f'[{difference.days}] days and '
f'[{difference.seconds / 3600:.1f}] hours ago'
)
if not result:
return
await ctx.send('```css\n' + '\n'.join(result) + '\n```')
@commands.group(
invoke_without_command=True,
name='error',
hidden=True,
aliases=['errors']
)
async def error(self, ctx, n: typing.Optional[int] = None):
"""Show a concise list of stored errors"""
if n is not None:
await self.print_traceback(ctx, n)
return
NUM_ERRORS_PER_PAGE = 15
error_log = self.client.last_errors
if not error_log:
await ctx.send('Error log is empty')
return
response = [f'```css\nNumber of stored errors: {len(error_log)}']
for i, exc_tuple in enumerate(error_log):
exc, date, error_source, *_ = exc_tuple
call_info = (
f'CMD: {error_source.invoked_with}'
if isinstance(error_source, commands.Context) else 'outside command'
)
response.append(
f'{i}: ['
+ date.isoformat().split('.')[0]
+ '] - ['
+ call_info
+ f']\nException: {exc}'
)
if i % NUM_ERRORS_PER_PAGE == NUM_ERRORS_PER_PAGE-1:
response.append('```')
await ctx.send('\n'.join(response))
response = [f'```css']
if len(response) > 1:
response.append('```')
await ctx.send('\n'.join(response))
@error.command(
name='clear',
aliases=['delete'],
)
async def error_clear(self, ctx, n: int = None):
"""Clear error with index [n]"""
if n is None:
self.client.last_errors = []
await ctx.send('Error log cleared')
else:
self.client.last_errors.pop(n)
await ctx.send(f'Deleted error #{n}')
@error.command(
name='traceback',
aliases=['tb'],
)
async def error_traceback(self, ctx, n: int = None):
"""Print the traceback of error [n] from the error log"""
await self.print_traceback(ctx, n)
async def print_traceback(self, ctx, n):
error_log = self.client.last_errors
if not error_log:
await ctx.send('Error log is empty')
return
if n is None:
await ctx.send('Please specify an error index')
await self.client.get_command('error').invoke(ctx)
return
if n >= len(error_log) or n < 0:
await ctx.send('Error index does not exist')
return
exc, date, error_source, orig_content = error_log[n]
delta = (datetime.utcnow() - date).total_seconds()
hours = int(delta // 3600)
seconds = int(delta - (hours * 3600))
delta_str = f'{hours} hours and {seconds} seconds ago'
tb = ''.join(
traceback.format_exception(type(exc), exc, exc.__traceback__)
)
response = [f'`Error occured {delta_str}`']
if error_source is not None:
response.append(
f'`Server:{error_source.guild.name} | Channel: {error_source.channel.name}`'
)
response.append(
f'`User: {error_source.author.name}#{error_source.author.discriminator}`'
)
if isinstance(error_source, commands.Context):
response.append(f'`Command: {error_source.invoked_with}`')
response.append(error_source.message.jump_url)
else:
response.append(f'`Command: No Command`')
response.append(error_source.jump_url)
response.append(f'```python\n')
num_chars = sum(len(line) for line in response)
for line in tb.split('\n'):
num_chars += len(line)
response.append(line)
if num_chars > 1900:
response.append('```')
await ctx.send('\n'.join(response))
response = ['```python\n']
num_chars = 0
response.append('```')
await ctx.send('\n'.join(response))
if error_source is not None:
e = Embed(title='Full command that caused the error:',
description=orig_content)
e.set_footer(text=error_source.author.display_name,
icon_url=error_source.author.display_avatar)
await ctx.send(embed=e)
def setup(client):
client.add_cog(Management(client))