11"""Pre-built cog to display source code links for commands and cogs."""
22import enum
33import inspect
4+ from importlib import metadata
45from pathlib import Path
56from typing import NamedTuple , TYPE_CHECKING
67
1314
1415
1516GITHUB_AVATAR = "https://avatars1.githubusercontent.com/u/9919"
17+ BOT_CORE_REPO = "https://github.com/python-discord/bot-core"
1618
1719class _TagIdentifierStub (NamedTuple ):
1820 """A minmally functioning stub representing a tag identifier."""
@@ -34,7 +36,9 @@ class _SourceType(enum.StrEnum):
3436
3537 help_command = enum .auto ()
3638 text_command = enum .auto ()
39+ core_command = enum .auto ()
3740 cog = enum .auto ()
41+ core_cog = enum .auto ()
3842 tag = enum .auto ()
3943 extension_not_loaded = enum .auto ()
4044
@@ -81,10 +85,14 @@ async def _get_source_object(ctx: commands.Context["Bot"], argument: str) -> tup
8185
8286 cog = ctx .bot .get_cog (argument )
8387 if cog :
88+ if cog .__module__ .startswith ("pydis_core.exts" ):
89+ return cog , _SourceType .core_cog
8490 return cog , _SourceType .cog
8591
8692 cmd = ctx .bot .get_command (argument )
8793 if cmd :
94+ if cmd .module .startswith ("pydis_core.exts" ):
95+ return cmd , _SourceType .core_command
8896 return cmd , _SourceType .text_command
8997
9098 tags_cog = ctx .bot .get_cog ("Tags" )
@@ -109,7 +117,7 @@ def _get_source_link(self, source_item: object, source_type: _SourceType) -> tup
109117
110118 Raise BadArgument if `source_item` is a dynamically-created object (e.g. via internal eval).
111119 """
112- if source_type == _SourceType .text_command :
120+ if source_type == _SourceType .text_command or source_type == _SourceType . core_command :
113121 source_item = inspect .unwrap (source_item .callback )
114122 src = source_item .__code__
115123 filename = src .co_filename
@@ -137,10 +145,25 @@ def _get_source_link(self, source_item: object, source_type: _SourceType) -> tup
137145 # Handle tag file location differently than others to avoid errors in some cases
138146 if not first_line_no :
139147 file_location = Path (filename )
148+ elif source_type == _SourceType .core_command :
149+ package_location = metadata .distribution ("pydis_core" ).locate_file ("" ) / "pydis_core"
150+ internal_location = Path (filename ).relative_to (package_location ).as_posix ()
151+ file_location = "pydis_core/" + internal_location
152+ elif source_type == _SourceType .core_cog :
153+ package_location = metadata .distribution ("pydis_core" ).locate_file ("" ) / "pydis_core" / "exts"
154+ internal_location = Path (filename ).relative_to (package_location ).as_posix ()
155+ file_location = "pydis_core/exts/" + internal_location
140156 else :
141157 file_location = Path (filename ).relative_to (Path .cwd ()).as_posix ()
142158
143- url = f"{ self .github_repo } /blob/main/{ file_location } { lines_extension } "
159+ repo = self .github_repo if source_type != _SourceType .core_command else BOT_CORE_REPO
160+
161+ if source_type == _SourceType .core_command or source_type == _SourceType .core_cog :
162+ version = f"v{ metadata .version ('pydis_core' )} "
163+ else :
164+ version = "main"
165+
166+ url = f"{ repo } /blob/{ version } /{ file_location } { lines_extension } "
144167
145168 return url , file_location , first_line_no or None
146169
@@ -154,6 +177,12 @@ async def _build_embed(self, source_object: object, source_type: _SourceType) ->
154177 elif source_type == _SourceType .text_command :
155178 description = source_object .short_doc
156179 title = f"Command: { source_object .qualified_name } "
180+ elif source_type == _SourceType .core_command :
181+ description = source_object .short_doc
182+ title = f"Core Command: { source_object .qualified_name } "
183+ elif source_type == _SourceType .core_cog :
184+ title = f"Core Cog: { source_object .qualified_name } "
185+ description = source_object .description .splitlines ()[0 ]
157186 elif source_type == _SourceType .tag :
158187 title = f"Tag: { source_object } "
159188 description = ""
0 commit comments