This repository was archived by the owner on Jan 22, 2021. It is now read-only.
forked from scarletcafe/jishaku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcog.py
973 lines (773 loc) · 37.1 KB
/
cog.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
# -*- coding: utf-8 -*-
"""
jishaku
~~~~~~~~~~~
The Jishaku debugging and diagnostics cog.
:copyright: 2019 Devon (Gorialis), 2020 GamingGeek
:license: MIT, see LICENSE for more details.
"""
import asyncio
import aiohttp
import collections
import contextlib
import datetime
import inspect
import itertools
import io
import os
import os.path
import re
import sys
import time
import json
import traceback
import typing
import discord
import humanize
from discord.ext import commands
from fire.converters import User, UserWithFallback, Member, TextChannel, VoiceChannel
from jishaku.codeblocks import Codeblock, CodeblockConverter
from jishaku.exception_handling import ReplResponseReactor
from jishaku.meta import __version__
from jishaku.models import copy_context_with
from jishaku.modules import ExtensionConverter, package_version
from jishaku.paginators import PaginatorInterface, WrappedFilePaginator, WrappedPaginator, PaginatorEmbedInterface
from jishaku.repl import AsyncCodeExecutor, Scope, all_inspections, get_var_dict_from_ctx
from jishaku.shell import ShellReader
from jishaku.voice import BasicYouTubeDLSource, connected_check, playing_check, vc_check, youtube_dl
try:
import psutil
except ImportError:
psutil = None
__all__ = (
"Jishaku",
"setup"
)
ENABLED_SYMBOLS = ("true", "t", "yes", "y", "on", "1")
JISHAKU_HIDE = True
JISHAKU_RETAIN = os.getenv("JISHAKU_RETAIN", "").lower() in ENABLED_SYMBOLS
CommandTask = collections.namedtuple("CommandTask", "index ctx task")
class Jishaku(commands.Cog): # pylint: disable=too-many-public-methods
"""
The cog that includes Jishaku's Discord-facing default functionality.
"""
load_time = datetime.datetime.now()
def __init__(self, bot):
self.bot = bot
self._scope = Scope()
self.retain = JISHAKU_RETAIN
self.last_result = None
self.start_time = datetime.datetime.now()
self.tasks = collections.deque()
self.task_count: int = 0
@property
def scope(self):
"""
Gets a scope for use in REPL.
If retention is on, this is the internal stored scope,
otherwise it is always a new Scope.
"""
if self.retain:
return self._scope
return Scope()
@contextlib.contextmanager
def submit(self, ctx: commands.Context):
"""
A context-manager that submits the current task to jishaku's task list
and removes it afterwards.
Arguments
---------
ctx: commands.Context
A Context object used to derive information about this command task.
"""
self.task_count += 1
cmdtask = CommandTask(self.task_count, ctx,
asyncio.Task.current_task())
self.tasks.append(cmdtask)
try:
yield cmdtask
finally:
if cmdtask in self.tasks:
self.tasks.remove(cmdtask)
async def cog_check(self, ctx: commands.Context):
"""
Local check, makes all commands in this cog owner-only
"""
if not await ctx.bot.is_owner(ctx.author):
raise commands.NotOwner("You must own this bot to use Jishaku.")
return True
@commands.group(name="admin", aliases=["administration", "jsk"], hidden=JISHAKU_HIDE,
invoke_without_command=True, ignore_extra=False)
async def jsk(self, ctx: commands.Context):
"""
The Jishaku debug and diagnostic commands.
This command on its own gives a status brief.
All other functionality is within its subcommands.
"""
summary = [
f"Jishaku v{__version__}, discord.py `{package_version('discord.py')}`, "
f"`Python {sys.version}` on `{sys.platform}`".replace("\n", ""),
f"Module was loaded {humanize.naturaltime(self.load_time)}, "
f"cog was loaded {humanize.naturaltime(self.start_time)}.",
""
]
if psutil:
proc = psutil.Process()
with proc.oneshot():
mem = proc.memory_full_info()
summary.append(f"Using {humanize.naturalsize(mem.rss)} physical memory and "
f"{humanize.naturalsize(mem.vms)} virtual memory, "
f"{humanize.naturalsize(mem.uss)} of which unique to this process.")
name = proc.name()
pid = proc.pid
thread_count = proc.num_threads()
summary.append(
f"Running on PID {pid} (`{name}`) with {thread_count} thread(s).")
summary.append("") # blank line
cache_summary = f"{len(self.bot.guilds)} guild(s) and {len(self.bot.users)} user(s)"
if isinstance(self.bot, discord.AutoShardedClient):
summary.append(
f"This bot is automatically sharded and can see {cache_summary}.")
elif self.bot.shard_count:
summary.append(
f"This bot is manually sharded and can see {cache_summary}.")
else:
summary.append(
f"This bot is not sharded and can see {cache_summary}.")
summary.append(
f"Average websocket latency: {round(self.bot.latency * 1000, 2)}ms")
await ctx.send("\n".join(summary))
__cat_line_regex = re.compile(
r"(?:\.\/+)?(.+?)(?:#L?(\d+)(?:\-L?(\d+))?)?$")
@jsk.command(name="cat")
async def jsk_cat(self, ctx: commands.Context, argument: str):
"""
Read out a file, using syntax highlighting if detected.
Lines and linespans are supported by adding '#L12' or '#L12-14' etc to the end of the filename.
"""
match = self.__cat_line_regex.search(argument)
if not match: # should never happen
return await ctx.send("Couldn't parse this input.")
path = match.group(1)
line_span = None
if match.group(2):
start = int(match.group(2))
line_span = (start, int(match.group(3) or start))
if not os.path.exists(path) or os.path.isdir(path):
return await ctx.send(f"`{path}`: No file by that name.")
size = os.path.getsize(path)
if size <= 0:
return await ctx.send(f"`{path}`: Cowardly refusing to read a file with no size stat"
f" (it may be empty, endless or inaccessible).")
if size > 50 * (1024 ** 2):
return await ctx.send(f"`{path}`: Cowardly refusing to read a file >50MB.")
try:
with open(path, "rb") as file:
paginator = WrappedFilePaginator(
file, line_span=line_span, max_size=1985)
except UnicodeDecodeError:
return await ctx.send(f"`{path}`: Couldn't determine the encoding of this file.")
except ValueError as exc:
return await ctx.send(f"`{path}`: Couldn't read this file, {exc}")
interface = PaginatorInterface(ctx.bot, paginator, owner=ctx.author)
await interface.send_to(ctx)
@jsk.command(name="curl")
async def jsk_curl(self, ctx: commands.Context, url: str):
"""
Download and display a text file from the internet.
This command is similar to jsk cat, but accepts a URL.
"""
# remove embed maskers if present
url = url.lstrip("<").rstrip(">")
if 'api.hypixel.net' in url:
if '?' in url:
url = f'{url}&key={self.bot.config["hypixel"][0]}'
else:
url = f'{url}?key={self.bot.config["hypixel"][0]}'
async with ReplResponseReactor(ctx):
async with aiohttp.ClientSession(headers={'User-Agent': 'Fire Discord Bot'}) as session:
async with session.get(url) as response:
content_type = response.headers.get(
'Content-Type', 'text/plain').lower()
if content_type == 'application/json':
data = await response.json()
data = json.dumps(data, indent=2).encode('utf-8')
elif content_type.startswith('image/'):
ext = content_type.split('image/')[-1]
f = discord.File(io.BytesIO((await response.read())), filename=f'image.{ext}')
return await ctx.send(file=f)
elif any(ext in content_type for ext in ['png', 'jpg', 'jpeg', 'gif']):
ext = [ext for ext in ['png', 'jpg', 'jpeg',
'gif'] if ext in content_type][0]
f = discord.File(io.BytesIO((await response.read())), filename=f'image.{ext}')
return await ctx.send(file=f)
else:
data = await response.read()
hints = (
response.content_type,
url
)
code = response.status
if not data:
return await ctx.send(f"HTTP response was empty (status code {code}).")
try:
paginator = WrappedFilePaginator(io.BytesIO(
data), language_hints=hints, max_size=1985)
except UnicodeDecodeError:
return await ctx.send(f"Couldn't determine the encoding of the response. (status code {code})")
except ValueError as exc:
return await ctx.send(f"Couldn't read response (status code {code}), {exc}")
interface = PaginatorInterface(
ctx.bot, paginator, owner=ctx.author)
await interface.send_to(ctx)
@jsk.command(name="tasks")
async def jsk_tasks(self, ctx: commands.Context):
"""
Shows the currently running jishaku tasks.
"""
if not self.tasks:
return await ctx.send("No currently running tasks.")
paginator = commands.Paginator(max_size=1985)
for task in self.tasks:
paginator.add_line(f"{task.index}: `{task.ctx.command.qualified_name}`, invoked at "
f"{task.ctx.message.created_at.strftime('%Y-%m-%d %H:%M:%S')} UTC")
interface = PaginatorInterface(ctx.bot, paginator, owner=ctx.author)
await interface.send_to(ctx)
@jsk.command(name="cancel")
async def jsk_cancel(self, ctx: commands.Context, index: int, *, name: str = None):
"""
Cancels a task with the given index.
If the index passed is -1, will cancel the last task instead.
"""
if not self.tasks:
return await ctx.error("No tasks to cancel.")
if index == -1 and name:
for task in self.tasks.copy():
if task.ctx.command.qualified_name == name:
task = discord.utils.get(self.tasks, index=task.index)
if task:
task.task.cancel()
self.tasks.remove(task)
return await ctx.success('Successfully canceled tasks')
elif index == -1:
index = self.tasks[-1].index
task = discord.utils.get(self.tasks, index=index)
if task:
self.tasks.remove(task)
else:
return await ctx.error("Unknown task.")
task.task.cancel()
return await ctx.success(f"Cancelled task {task.index}: `{task.ctx.command.qualified_name}`,"
f" invoked at {task.ctx.message.created_at.strftime('%Y-%m-%d %H:%M:%S')} UTC")
@jsk.command(name="retain")
async def jsk_retain(self, ctx: commands.Context, *, toggle: bool):
"""
Turn variable retention for REPL on or off.
"""
if toggle:
if self.retain:
return await ctx.send("Variable retention is already set to ON.")
self.retain = True
self._scope = Scope()
return await ctx.send("Variable retention is ON. Future REPL sessions will retain their scope.")
if not self.retain:
return await ctx.send("Variable retention is already set to OFF.")
self.retain = False
return await ctx.send("Variable retention is OFF. Future REPL sessions will dispose their scope when done.")
@jsk.command(name="py", aliases=["python"])
async def jsk_python(self, ctx: commands.Context, *, argument: CodeblockConverter):
"""
Direct evaluation of Python code.
"""
arg_dict = get_var_dict_from_ctx(ctx)
scope = self.scope
scope.clean()
arg_dict["_"] = self.last_result
async with ReplResponseReactor(ctx, argument=argument):
with self.submit(ctx):
start = time.perf_counter()
async for result in AsyncCodeExecutor(argument.content, scope, arg_dict=arg_dict):
end = time.perf_counter()
if result is None:
continue
resultstr = str(result)
self.last_result = result
resulttype = result.__class__.__name__
if self.bot.http.token in resultstr:
return
tokenlist = self.bot.http.token.split('.')
if any(x in resultstr for x in tokenlist):
return
if isinstance(result, discord.File):
await ctx.send(file=result)
elif isinstance(result, discord.Embed):
await ctx.send(embed=result)
elif isinstance(result, PaginatorInterface):
await result.send_to(ctx)
elif isinstance(result, discord.Message) and result.id > ctx.message.id:
return
else:
if not isinstance(result, str):
# repr all non-strings
result = repr(result)
result = result.replace("``", "`\u200b`")
if len(result) > 1024:
# inconsistency here, results get wrapped in codeblocks when they are too large
# but don't if they're not. probably not that bad, but noting for later review
paginator = WrappedPaginator(
prefix='```py', suffix='```', max_size=1985)
paginator.add_line(result)
embed = discord.Embed(title="<:check:674359197378281472> Evaluation Complete",
colour=ctx.author.color, description=f"Output Type: {resulttype}")
embed.add_field(
name=":inbox_tray: Input", value=f"```py\n{argument.content}```", inline=False)
embed.set_footer(
text=f'Evaluated in {end - start:.3f}s.')
paginatorembed = discord.Embed(
colour=ctx.author.color)
interface = PaginatorEmbedInterface(
ctx.bot, paginator, owner=ctx.author, _embed=paginatorembed)
await ctx.send(embed=embed)
await interface.send_to(ctx)
else:
if result.strip() == '':
result = "\u200b"
embed = discord.Embed(title="<:check:674359197378281472> Evaluation Complete",
colour=ctx.author.color, description=f"Output Type: {resulttype}")
embed.add_field(
name=":inbox_tray: Input", value=f"```py\n{argument.content}```", inline=False)
embed.add_field(
name=":outbox_tray: Output", value=f"```py\n{result}```", inline=False)
embed.set_footer(
text=f'Evaluated in {end - start:.3f}s.')
await ctx.send(embed=embed)
@jsk.command(name="py_inspect", aliases=["pyi", "python_inspect", "pythoninspect"])
async def jsk_python_inspect(self, ctx: commands.Context, *, argument: CodeblockConverter):
"""
Evaluation of Python code with inspect information.
"""
if ctx.author.id == 376817315830038530:
return await ctx.send('No eval for you buddy!!! This is my bot, this is MY land, you CANNOT come close to the might of Geek')
arg_dict = get_var_dict_from_ctx(ctx)
scope = self.scope
scope.clean()
arg_dict["_"] = self.last_result
async with ReplResponseReactor(ctx, argument=argument):
with self.submit(ctx):
async for result in AsyncCodeExecutor(argument.content, scope, arg_dict=arg_dict):
resultstr = str(result)
self.last_result = result
if self.bot.http.token in resultstr:
return
tokenlist = self.bot.http.token.split('.')
if any(x in resultstr for x in tokenlist):
return
header = repr(result).replace("``", "`\u200b`")
if len(header) > 485:
header = header[0:482] + "..."
output = []
output.append(f'```prolog\n=== {header} ===\n')
for name, res in all_inspections(result):
output.append(f"{name:16.16} :: {res}")
output.append('```')
res = '\n'.join(output)
embed = discord.Embed(
title="<:check:674359197378281472> Evaluation Complete", colour=ctx.author.color)
embed.add_field(
name=":inbox_tray: Input", value=f"```py\n{argument.content}```", inline=False)
embed.add_field(name=":outbox_tray: Output",
value=f"{res}", inline=False)
await ctx.send(embed=embed)
@jsk.command(name="shell", aliases=["sh", "cmd"])
async def jsk_shell(self, ctx: commands.Context, *, argument: CodeblockConverter):
"""
Executes statements in the system shell.
This uses the bash shell. Execution can be cancelled by closing the paginator.
"""
async with ReplResponseReactor(ctx):
with self.submit(ctx):
paginator = WrappedPaginator(prefix="```sh", max_size=1985)
paginator.add_line(f"$ {argument.content}\n")
interface = PaginatorInterface(
ctx.bot, paginator, owner=ctx.author)
self.bot.loop.create_task(interface.send_to(ctx))
with ShellReader(argument.content) as reader:
async for line in reader:
if interface.closed:
return
await interface.add_line(line)
await interface.add_line(f"\n[status] Return code {reader.close_code}")
@jsk.command(name="git")
async def jsk_git(self, ctx: commands.Context, *, argument: CodeblockConverter):
"""
Shortcut for 'jsk sh git'. Invokes the system shell.
"""
return await ctx.invoke(self.jsk_shell, argument=Codeblock(argument.language, "git " + argument.content))
@jsk.command(name="pm2")
async def jsk_pm2(self, ctx: commands.Context, *, argument: CodeblockConverter):
"""
Shortcut for 'jsk sh pm2'. Invokes the system shell.
"""
return await ctx.invoke(self.jsk_shell, argument=Codeblock(argument.language, "pm2 " + argument.content))
@jsk.command(name="pwrsh")
async def jsk_powershell(self, ctx: commands.Context, *, argument: CodeblockConverter):
"""
Shortcut for 'jsk sh powershell'. Invokes the system shell.
"""
return await ctx.invoke(self.jsk_shell, argument=Codeblock(argument.language, "powershell " + argument.content))
@jsk.command(name="load", aliases=["reload"])
async def jsk_load(self, ctx: commands.Context, *extensions: ExtensionConverter):
"""
Loads or reloads the given extension names.
Reports any extensions that failed to load.
"""
paginator = commands.Paginator(prefix='', suffix='')
tasks = None
for extension in itertools.chain(*extensions):
if extension == 'api.main':
try:
await self.bot.get_cog('Fire API').stop()
except Exception:
pass
modules = sys.modules.copy()
for m in modules:
if m.startswith('api.endpoints'):
sys.modules.pop(m)
if extension == 'jishaku':
tasks = self.tasks.copy()
method, icon = (
(self.bot.reload_extension,
"\N{CLOCKWISE RIGHTWARDS AND LEFTWARDS OPEN CIRCLE ARROWS}")
if extension in self.bot.extensions else
(self.bot.load_extension, "\N{INBOX TRAY}")
)
try:
method(extension)
except Exception as exc: # pylint: disable=broad-except
traceback_data = ''.join(traceback.format_exception(
type(exc), exc, exc.__traceback__, 1))
paginator.add_line(
f"{icon}\N{WARNING SIGN} `{extension}`\n```py\n{traceback_data}\n```",
empty=True
)
else:
paginator.add_line(f"{icon} `{extension}`", empty=True)
if tasks:
self.bot.get_cog('Jishaku').tasks = tasks
for page in paginator.pages:
await ctx.send(page)
@jsk.command(name="unload")
async def jsk_unload(self, ctx: commands.Context, *extensions: ExtensionConverter):
"""
Unloads the given extension names.
Reports any extensions that failed to unload.
"""
paginator = commands.Paginator(prefix='', suffix='')
icon = "\N{OUTBOX TRAY}"
for extension in itertools.chain(*extensions):
try:
if extension == 'fishin.abucket':
await self.bot.get_cog('Fire API').stop()
self.bot.unload_extension(extension)
except Exception as exc: # pylint: disable=broad-except
traceback_data = "".join(traceback.format_exception(
type(exc), exc, exc.__traceback__, 1))
paginator.add_line(
f"{icon}\N{WARNING SIGN} `{extension}`\n```py\n{traceback_data}\n```",
empty=True
)
else:
paginator.add_line(f"{icon} `{extension}`", empty=True)
for page in paginator.pages:
await ctx.send(page)
@jsk.command(name='createvanity')
async def jsk_createvanity(self, ctx, gid: int, code: str, inv: str):
con = await self.bot.db.acquire()
async with con.transaction():
query = 'INSERT INTO vanity (\"gid\", \"code\", \"invite\") VALUES ($1, $2, $3);'
await self.bot.db.execute(query, str(gid), code, inv)
await self.bot.db.release(con)
await self.bot.get_cog('Vanity URLs').request_fetch()
return await ctx.success(f'Successfully created https://{"test." if self.bot.dev else ""}inv.wtf/{code}')
@jsk.command(name='vanitybl')
async def jsk_vanitybl(self, ctx, gid: int, *, reason: str):
guild = self.bot.get_guild(gid)
if not guild:
return await ctx.error(f'Unknown Guild')
con = await self.bot.db.acquire()
async with con.transaction():
query = 'INSERT INTO vanitybl (\"guild\", \"gid\", \"reason\") VALUES ($1, $2, $3);'
await self.bot.db.execute(query, guild.name, str(guild.id), reason)
await self.bot.db.release(con)
await self.bot.get_cog('Vanity URLs').delete_guild(guild.id)
return await ctx.success(f'Successfully blacklisted {guild} from vanity features!')
@jsk.command(name='vanityubl')
async def jsk_vanityubl(self, ctx, gid: int):
guild = self.bot.get_guild(gid)
if not guild:
return await ctx.error(f'Unknown Guild')
con = await self.bot.db.acquire()
async with con.transaction():
query = 'DELETE FROM vanitybl WHERE gid=$1;'
await self.bot.db.execute(query, str(guild.id))
await self.bot.db.release(con)
return await ctx.success(f'Successfully unblacklisted {guild} from vanity features!')
@jsk.command(name='setdesc')
async def jsk_setdesc(self, ctx, gid: int, *, desc: str):
await self.bot.configs[gid].set('main.description', desc)
return await ctx.success(f'Successfully set description')
@jsk.command(name='alias')
async def jsk_alias(self, ctx, user: UserWithFallback, *, alias: str):
if alias.lower() == 'hasalias':
return await ctx.error('"hasalias" cannot be used as an alias')
hasalias = json.loads((await self.bot.redis.get('hasalias', encoding='utf-8')))
if user.id not in hasalias:
con = await self.bot.db.acquire()
async with con.transaction():
query = 'INSERT INTO aliases (\"uid\", \"aliases\") VALUES ($1, $2);'
await self.bot.db.execute(query, str(user.id), [alias])
await self.bot.db.release(con)
else:
aliases = json.loads((await self.bot.redis.get('aliases', encoding='utf-8')))
aliases = [a for a in aliases if aliases[a] == user.id]
aliases.append(alias)
con = await self.bot.db.acquire()
async with con.transaction():
query = 'UPDATE aliases SET \"aliases\"=$2 WHERE uid = $1;'
await self.bot.db.execute(query, str(user.id), aliases)
await self.bot.db.release(con)
await self.bot.get_cog('Settings').load_aliases()
return await ctx.success(f'Successfully added alias')
@jsk.command(name='delalias')
async def jsk_delalias(self, ctx, user: UserWithFallback, *, alias: str):
if alias in self.bot.aliases and alias != 'hasalias':
aliases = [a for a in self.bot.aliases if a !=
'hasalias' and self.bot.aliases[a] == user.id]
if alias not in aliases:
return await ctx.error(f'Alias doesn\'t belong to the specified user')
aliases.remove(alias)
con = await self.bot.db.acquire()
async with con.transaction():
if aliases:
query = 'UPDATE aliases SET \"aliases\"=$2 WHERE uid = $1;'
else:
query = 'DELETE FROM aliases WHERE uid=$1;'
await self.bot.db.execute(query, str(user.id), aliases)
await self.bot.db.release(con)
else:
return await ctx.error('Invalid alias')
await self.bot.get_cog('Settings').load_aliases()
return await ctx.success(f'Successfully deleted alias')
@jsk.group(name="voice", aliases=["vc"])
@commands.check(vc_check)
async def jsk_voice(self, ctx: commands.Context):
"""
Voice-related commands.
If invoked without subcommand, relays current voice state.
"""
# if using a subcommand, short out
if ctx.invoked_subcommand is not None and ctx.invoked_subcommand is not self.jsk_voice:
return
# give info about the current voice client if there is one
voice = ctx.guild.voice_client
if not voice or not voice.is_connected():
return await ctx.send("Not connected.")
await ctx.send(f"Connected to {voice.channel.name}, "
f"{'paused' if voice.is_paused() else 'playing' if voice.is_playing() else 'idle'}.")
@jsk_voice.command(name="join", aliases=["connect"])
async def jsk_vc_join(self, ctx: commands.Context, *,
destination: typing.Union[VoiceChannel, Member] = None):
"""
Joins a voice channel, or moves to it if already connected.
Passing a voice channel uses that voice channel.
Passing a member will use that member's current voice channel.
Passing nothing will use the author's voice channel.
"""
destination = destination or ctx.author
if isinstance(destination, discord.Member):
if destination.voice and destination.voice.channel:
destination = destination.voice.channel
else:
return await ctx.send("Member has no voice channel.")
voice = ctx.guild.voice_client
if voice:
await voice.move_to(destination)
else:
await destination.connect(reconnect=True)
await ctx.send(f"Connected to {destination.name}.")
@jsk_voice.command(name="disconnect", aliases=["dc"])
@commands.check(connected_check)
async def jsk_vc_disconnect(self, ctx: commands.Context):
"""
Disconnects from the voice channel in this guild, if there is one.
"""
voice = ctx.guild.voice_client
await voice.disconnect()
await ctx.send(f"Disconnected from {voice.channel.name}.")
@jsk_voice.command(name="stop")
@commands.check(playing_check)
async def jsk_vc_stop(self, ctx: commands.Context):
"""
Stops running an audio source, if there is one.
"""
voice = ctx.guild.voice_client
voice.stop()
await ctx.send(f"Stopped playing audio in {voice.channel.name}.")
@jsk_voice.command(name="pause")
@commands.check(playing_check)
async def jsk_vc_pause(self, ctx: commands.Context):
"""
Pauses a running audio source, if there is one.
"""
voice = ctx.guild.voice_client
if voice.is_paused():
return await ctx.send("Audio is already paused.")
voice.pause()
await ctx.send(f"Paused audio in {voice.channel.name}.")
@jsk_voice.command(name="resume")
@commands.check(playing_check)
async def jsk_vc_resume(self, ctx: commands.Context):
"""
Resumes a running audio source, if there is one.
"""
voice = ctx.guild.voice_client
if not voice.is_paused():
return await ctx.send("Audio is not paused.")
voice.resume()
await ctx.send(f"Resumed audio in {voice.channel.name}.")
@jsk_voice.command(name="volume")
@commands.check(playing_check)
async def jsk_vc_volume(self, ctx: commands.Context, *, percentage: float):
"""
Adjusts the volume of an audio source if it is supported.
"""
volume = max(0.0, min(1.0, percentage / 100))
source = ctx.guild.voice_client.source
if not isinstance(source, discord.PCMVolumeTransformer):
return await ctx.send("This source doesn't support adjusting volume or "
"the interface to do so is not exposed.")
source.volume = volume
await ctx.send(f"Volume set to {volume * 100:.2f}%")
@jsk_voice.command(name="play", aliases=["play_local"])
@commands.check(connected_check)
async def jsk_vc_play(self, ctx: commands.Context, *, uri: str):
"""
Plays audio direct from a URI.
Can be either a local file or an audio resource on the internet.
"""
voice = ctx.guild.voice_client
if voice.is_playing():
voice.stop()
# remove embed maskers if present
uri = uri.lstrip("<").rstrip(">")
voice.play(discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(uri)))
await ctx.send(f"Playing in {voice.channel.name}.")
@jsk_voice.command(name="youtube_dl", aliases=["youtubedl", "ytdl", "yt"])
@commands.check(connected_check)
async def jsk_vc_youtube_dl(self, ctx: commands.Context, *, url: str):
"""
Plays audio from youtube_dl-compatible sources.
"""
if not youtube_dl:
return await ctx.send("youtube_dl is not installed.")
voice = ctx.guild.voice_client
if voice.is_playing():
voice.stop()
# remove embed maskers if present
url = url.lstrip("<").rstrip(">")
voice.play(discord.PCMVolumeTransformer(BasicYouTubeDLSource(url)))
await ctx.send(f"Playing in {voice.channel.name}.")
@jsk.command(name="su")
async def jsk_su(self, ctx: commands.Context, target: UserWithFallback, *, command_string: str):
"""
Run a command as someone else.
This will try to resolve to a Member, but will use a User if it can't find one.
"""
if ctx.guild:
# Try to upgrade to a Member instance
# This used to be done by a Union converter, but doing it like this makes
# the command more compatible with chaining, e.g. `jsk in .. jsk su ..`
target = ctx.guild.get_member(target.id) or target
alt_ctx = await copy_context_with(ctx, author=target, content=ctx.prefix + command_string, silent=ctx.silent)
if alt_ctx.command is None:
if alt_ctx.invoked_with is None:
return await ctx.send('This bot has been hard-configured to ignore this user.')
return await ctx.send(f'Command "{alt_ctx.invoked_with}" is not found')
return await alt_ctx.command.invoke(alt_ctx)
@jsk.command(name="in")
async def jsk_in(self, ctx: commands.Context, channel: TextChannel, *, command_string: str):
"""
Run a command as if it were in a different channel.
"""
alt_ctx = await copy_context_with(ctx, channel=channel, content=ctx.prefix + command_string, silent=ctx.silent)
if alt_ctx.command is None:
return await ctx.send(f'Command "{alt_ctx.invoked_with}" is not found')
return await alt_ctx.command.invoke(alt_ctx)
@jsk.command(name="sudo")
async def jsk_sudo(self, ctx: commands.Context, *, command_string: str):
"""
Run a command bypassing all checks and cooldowns.
This also bypasses permission checks so this has a high possibility of making a command raise.
"""
alt_ctx = await copy_context_with(ctx, content=ctx.prefix + command_string, silent=ctx.silent)
if alt_ctx.command is None:
return await ctx.send(f'Command "{alt_ctx.invoked_with}" is not found')
return await alt_ctx.command.reinvoke(alt_ctx)
@jsk.command(name="repeat")
async def jsk_repeat(self, ctx: commands.Context, times: int, *, command_string: str):
"""
Runs a command multiple times in a row.
This acts like the command was invoked several times manually, so it obeys cooldowns.
"""
with self.submit(ctx): # allow repeats to be cancelled
for _ in range(times):
alt_ctx = await copy_context_with(ctx, content=ctx.prefix + command_string, silent=ctx.silent)
if alt_ctx.command is None:
return await ctx.send(f'Command "{alt_ctx.invoked_with}" is not found')
await alt_ctx.command.reinvoke(alt_ctx)
@jsk.command(name="debug", aliases=["dbg"])
async def jsk_debug(self, ctx: commands.Context, *, command_string: str):
"""
Run a command timing execution and catching exceptions.
"""
alt_ctx = await copy_context_with(ctx, content=ctx.prefix + command_string, silent=ctx.silent)
if alt_ctx.command is None:
return await ctx.send(f'Command "{alt_ctx.invoked_with}" is not found')
start = time.perf_counter()
async with ReplResponseReactor(ctx):
with self.submit(ctx):
await alt_ctx.command.invoke(alt_ctx)
end = time.perf_counter()
return await ctx.send(f"Command `{alt_ctx.command.qualified_name}` finished in {end - start:.3f}s.")
@jsk.command(name="source", aliases=["src"])
async def jsk_source(self, ctx: commands.Context, *, command_name: str):
"""
Displays the source code for a command.
"""
command = self.bot.get_command(command_name)
if not command:
return await ctx.send(f"Couldn't find command `{command_name}`.")
try:
source_lines, _ = inspect.getsourcelines(command.callback)
except (TypeError, OSError):
return await ctx.send(f"Was unable to retrieve the source for `{command}` for some reason.")
# getsourcelines for some reason returns WITH line endings
source_lines = ''.join(source_lines).split('\n')
paginator = WrappedPaginator(
prefix='```py', suffix='```', max_size=1985)
for line in source_lines:
paginator.add_line(line)
interface = PaginatorInterface(ctx.bot, paginator, owner=ctx.author)
await interface.send_to(ctx)
@jsk.command(name="shutdown", aliases=["logout"])
async def jsk_shutdown(self, ctx: commands.Context):
"""
Logs this bot out.
"""
await ctx.bot.logout()
def setup(bot: commands.Bot):
"""
Adds the Jishaku cog to the bot.
"""
bot.add_cog(Jishaku(bot=bot))
bot.logger.info(f'$GREENLoaded Jishaku')