From af49c66c42e508150138f7ab7f3aaa5e89d0f34f Mon Sep 17 00:00:00 2001 From: Dan Hess Date: Wed, 28 Apr 2021 16:24:56 -0800 Subject: [PATCH] Provide source link as well, since preview fails sometimes --- cogs/misc.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cogs/misc.py b/cogs/misc.py index 9c5f2b7..3fceaf5 100644 --- a/cogs/misc.py +++ b/cogs/misc.py @@ -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)