Last active
January 18, 2023 22:36
-
-
Save sgtlaggy/388f0ffd5ba3975e71e14450c04e82d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Annotated, Any | |
from discord import Interaction, app_commands | |
from discord.app_commands import Choice | |
from discord.ext import commands | |
import discord | |
class CategoryTransformer(app_commands.Transformer): | |
async def autocomplete(self, interaction: Interaction, value: str) -> list[Choice[str]]: # type: ignore | |
bot: commands.Bot = interaction.client # type: ignore | |
return [ | |
Choice(name=cog.qualified_name, value=name) | |
for name, cog in bot.cogs.items() | |
if cog.qualified_name.casefold().startswith(value.casefold()) | |
][:25] | |
async def transform(self, interaction: Interaction, value: str) -> commands.Cog: | |
cog: commands.Cog = interaction.client.get_cog(value) # type: ignore | |
if cog is None: | |
raise ValueError(f'Category {value} not found.') | |
else: | |
return cog | |
class CommandTransformer(app_commands.Transformer): | |
async def autocomplete(self, interaction: Interaction, value: str) -> list[Choice[str]]: # type: ignore | |
bot: commands.Bot = interaction.client # type: ignore | |
return [ | |
Choice(name=command.qualified_name, value=command.qualified_name) | |
for command in bot.walk_commands() | |
if command.qualified_name.casefold().startswith(value.casefold()) | |
][:25] | |
async def transform(self, interaction: Interaction, value: str) -> commands.Command[Any, ..., Any]: | |
command: commands.Command[Any, ..., Any] = interaction.client.get_command(value) # type: ignore | |
if command is None: | |
raise ValueError(f'Command {value} not found.') | |
else: | |
return command | |
class HelpCommand(commands.MinimalHelpCommand): | |
def __init__(self, *args: Any, ephemeral: bool = True, **kwargs: dict[str, Any]): | |
super().__init__(*args, **kwargs) | |
self.ephemeral = ephemeral | |
def get_destination(self) -> discord.abc.Messageable: | |
return self.context | |
async def send_pages(self) -> None: | |
ctx = self.context | |
for page in self.paginator.pages: | |
await ctx.send(page, ephemeral=self.ephemeral) | |
class Help(commands.Cog): | |
def __init__(self, bot: commands.Bot): | |
self.bot = bot | |
async def cog_load(self): | |
self._original_help_command = self.bot.help_command | |
self.bot.help_command = HelpCommand() | |
async def cog_unload(self): | |
self.bot.help_command = self._original_help_command | |
@app_commands.command() | |
async def help( | |
self, | |
interaction: Interaction, | |
category: Annotated[commands.Cog, CategoryTransformer] | None = None, | |
command: Annotated[commands.Command[Any, ..., Any], CommandTransformer] | None = None, | |
): | |
if category is not None: | |
arg = category.qualified_name | |
elif command is not None: | |
arg = command.qualified_name | |
else: | |
await interaction.response.send_message('You must specify a category or command.') | |
return | |
ctx = await interaction.client.get_context(interaction) | |
help_command = self.bot.help_command._command_impl # type: ignore | |
await help_command.prepare(ctx) | |
await help_command(ctx, command=arg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment