aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar ks129 <[email protected]>2020-05-20 08:46:27 +0300
committerGravatar ks129 <[email protected]>2020-05-20 08:46:27 +0300
commita4ad94904638c94b44006d546861d399eba77ed5 (patch)
treec547794f3752811802df52783d678375f9000e2e
parentSource: Create converter for source object converting (diff)
Source: Create `get_source_link` function that build item's GitHub link
-rw-r--r--bot/cogs/source.py25
1 files changed, 23 insertions, 2 deletions
diff --git a/bot/cogs/source.py b/bot/cogs/source.py
index 76f75f83b..1f9e0e84d 100644
--- a/bot/cogs/source.py
+++ b/bot/cogs/source.py
@@ -1,11 +1,14 @@
+import inspect
+import os
from typing import Union
-from discord.ext.commands import BadArgument, Cog, Context, Converter, Command, HelpCommand
+from discord.ext.commands import BadArgument, Cog, Command, Context, Converter, HelpCommand
from bot.bot import Bot
+from bot.constants import URLs
-class SourceConverted(Converter):
+class SourceConverter(Converter):
"""Convert argument to help command, command or Cog."""
async def convert(self, ctx: Context, argument: str) -> Union[HelpCommand, Command, Cog]:
@@ -38,6 +41,24 @@ class Source(Cog):
def __init__(self, bot: Bot):
self.bot = bot
+ @staticmethod
+ def get_source_link(source_item: Union[HelpCommand, Command, Cog]) -> str:
+ """Build GitHub link of source item."""
+ if isinstance(source_item, HelpCommand):
+ src = type(source_item)
+ filename = inspect.getsourcefile(src)
+ elif isinstance(source_item, Command):
+ src = source_item.callback.__code__
+ filename = src.co_filename
+ else:
+ src = type(source_item)
+ filename = inspect.getsourcefile(src)
+
+ lines, first_line_no = inspect.getsourcelines(src)
+ file_location = os.path.relpath(filename)
+
+ return f"{URLs.github_bot_repo}/blob/master/{file_location}#L{first_line_no}-L{first_line_no+len(lines)-1}"
+
def setup(bot: Bot) -> None:
"""Load `Source` cog."""