1
0
Fork 0
mirror of synced 2024-04-27 09:12:22 +12:00

Provide source link as well, since preview fails sometimes

This commit is contained in:
Dan Hess 2021-04-28 16:24:56 -08:00
parent 98f7266c6f
commit af49c66c42

View file

@ -278,16 +278,24 @@ class Miscellaneous(commands.Cog):
EXAMPLE: !source source
RESULTS: Shows the code for this command!
"""
source_url = "https://github.com/Phxntxm/Bonfire"
branch = "master"
if command is None:
return await ctx.send("https://github.com/Phxntxm/Bonfire")
return await ctx.send(source_url)
obj = ctx.bot.get_command(command)
if obj is None:
return await ctx.send(f"Could not find command {command}")
src = io.StringIO(inspect.getsource(obj.callback.__code__))
# Get source from the callback
src = obj.callback.__code__
lines, firstlineno = inspect.getsourcelines(src)
location = os.path.relpath(src.co_filename).replace("\\", "/")
await ctx.send(file=discord.File(src, filename=f"{command}.py"))
final_url = f"<{source_url}/blob/{branch}/{location}#L{firstlineno}-L{firstlineno + len(lines) - 1}>"
# Provide the source as a file, for the preview file thing
src = io.StringIO(inspect.getsource(src))
await ctx.send(final_url, file=discord.File(src, filename=f"{command}.py"))
@commands.command(enabled=False)
@utils.can_run(send_messages=True)