feat(commands|Utils): add source command

This commit is contained in:
Romain J 2020-11-11 01:27:51 +01:00
parent 3587a8f8a4
commit 5a65fe1a6c
2 changed files with 44 additions and 0 deletions

View file

@ -12,6 +12,7 @@ disable=
C0114, # missing-module-docstring
C0115, # missing-class-docstring
C0116, # missing-function-docstring
W0511, # fixme
W0703, # broad-except
R0801, # duplicate-code
R0902, # too-many-instance-attributes

View file

@ -1,4 +1,6 @@
import inspect
import logging
import os
import platform
import discord
@ -205,3 +207,44 @@ class Utils(commands.Cog, name="Utils"):
)
await ctx.send(embed=e)
# =========================================================================
@command_extra(name="source")
async def _invite(self, ctx: ContextPlus, *, name=None):
base_url = "https://github.com/Rom1-J/tuxbot-bot"
if not name:
return await ctx.send(f"<{base_url}>")
cmd = self.bot.get_command(name)
if cmd:
src = cmd.callback.__code__
rpath = src.co_filename
else:
return await ctx.send(
_("Unable to find `{}`", ctx, self.bot.config).format(name)
)
try:
lines, start_line = inspect.getsourcelines(src)
start_line -= 3 # todo: find why this -3 is necessary
except OSError:
return await ctx.send(
_(
"Unable to fetch lines for `{}`", ctx, self.bot.config
).format(name)
)
location = (
os.path.relpath(rpath)
.replace("\\", "/")
.split("site-packages/")[1]
)
final_url = (
f"<{base_url}/tree/master/{location}#L{start_line}"
f"-L{start_line + len(lines) - 1}>"
)
await ctx.send(final_url)