Command to find source code on github

pull/119/head
stijndcl 2022-07-02 00:34:12 +02:00
parent cb0b4a419e
commit 5b510d1f45
1 changed files with 51 additions and 0 deletions

View File

@ -0,0 +1,51 @@
import inspect
import os
from typing import Optional
from discord.ext import commands
from didier import Didier
class Meta(commands.Cog):
"""Cog for Didier-related commands"""
client: Didier
def __init__(self, client: Didier):
self.client = client
@commands.command(name="Source", aliases=["Src"])
async def source(self, ctx: commands.Context, *, command_name: Optional[str] = None):
"""Command to get links to the source code of Didier"""
repo_home = "https://github.com/stijndcl/didier"
if command_name is None:
return await ctx.reply(repo_home, mention_author=False)
if command_name == "help":
command = self.client.help_command
src = type(self.client.help_command)
filename = inspect.getsourcefile(src)
else:
command = self.client.get_command(command_name)
src = command.callback.__code__
filename = src.co_filename
if command is None:
return await ctx.reply(f"Geen commando gevonden voor ``{command_name}``.", mention_author=False)
lines, first_line = inspect.getsourcelines(src)
if filename is None:
return await ctx.reply(f"Geen code gevonden voor ``{command_name}``.", mention_author=False)
file_location = os.path.relpath(filename).replace("\\", "/")
github_url = f"{repo_home}/blob/master/{file_location}#L{first_line}-L{first_line + len(lines) - 1}"
await ctx.reply(github_url, mention_author=False)
async def setup(client: Didier):
"""Load the cog"""
await client.add_cog(Meta(client))