tux.cogs.utility.tldr
¶
Classes:
Name | Description |
---|---|
Tldr | |
Classes¶
Tldr(bot: Tux)
¶
Bases: Cog
Methods:
Name | Description |
---|---|
get_autocomplete | Provide autocomplete suggestions for TLDR commands based on user query. |
slash_tldr | Show a TLDR page for a CLI command |
prefix_tldr | Show a TLDR page for a CLI command |
get_tldr_page | Retrieves the TLDR page for a given command. |
get_tldrs | Fetches a list of available TLDR pages. |
Source code in tux/cogs/utility/tldr.py
Functions¶
get_autocomplete(interaction: discord.Interaction, query: str) -> list[app_commands.Choice[str]]
async
¶
Provide autocomplete suggestions for TLDR commands based on user query.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interaction | Interaction | The interaction object where autocomplete happens. | required |
query | str | Partial input from the user used to filter suggestions. | required |
Returns:
Type | Description |
---|---|
List[Choice[str]] | A list of up to 25 command names as autocomplete choices. |
Source code in tux/cogs/utility/tldr.py
async def get_autocomplete(
self,
interaction: discord.Interaction,
query: str,
) -> list[app_commands.Choice[str]]:
"""
Provide autocomplete suggestions for TLDR commands based on user query.
Parameters
----------
interaction : discord.Interaction
The interaction object where autocomplete happens.
query : str
Partial input from the user used to filter suggestions.
Returns
-------
List[app_commands.Choice[str]]
A list of up to 25 command names as autocomplete choices.
"""
# TODO: Resolve why interaction is not being used.
commands = self.get_tldrs()
filtered_commands = [
app_commands.Choice(name=cmd, value=cmd) for cmd in commands if cmd.lower().startswith(query.lower())
]
return filtered_commands[:25]
slash_tldr(interaction: discord.Interaction, command: str) -> None
async
¶
Show a TLDR page for a CLI command
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interaction | Interaction | The discord interaction object. | required |
command | str | The command to retrieve the TLDR page for. | required |
Source code in tux/cogs/utility/tldr.py
@app_commands.command(name="tldr")
@app_commands.guild_only()
@app_commands.autocomplete(command=get_autocomplete)
async def slash_tldr(self, interaction: discord.Interaction, command: str) -> None:
"""
Show a TLDR page for a CLI command
Parameters
----------
interaction : discord.Interaction
The discord interaction object.
command : str
The command to retrieve the TLDR page for.
"""
tldr_page = self.get_tldr_page(command)
embed = EmbedCreator.create_embed(
bot=self.bot,
embed_type=EmbedCreator.INFO,
user_name=interaction.user.name,
user_display_avatar=interaction.user.display_avatar.url,
title=f"TLDR for {command}",
description=tldr_page,
)
await interaction.response.send_message(embed=embed)
prefix_tldr(ctx: commands.Context[Tux], command: str) -> None
async
¶
Show a TLDR page for a CLI command
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx | Context[Tux] | The context object for the command. | required |
command | str | The command to retrieve the TLDR page for. | required |
Source code in tux/cogs/utility/tldr.py
@commands.command(
name="tldr",
aliases=["man"],
)
@commands.guild_only()
async def prefix_tldr(self, ctx: commands.Context[Tux], command: str) -> None:
"""
Show a TLDR page for a CLI command
Parameters
----------
ctx : commands.Context[Tux]
The context object for the command.
command : str
The command to retrieve the TLDR page for.
"""
tldr_page = self.get_tldr_page(command)
embed = EmbedCreator.create_embed(
bot=self.bot,
embed_type=EmbedCreator.INFO,
user_name=ctx.author.name,
user_display_avatar=ctx.author.display_avatar.url,
title=f"TLDR for {command}",
description=tldr_page,
)
await ctx.send(embed=embed)
get_tldr_page(command: str) -> str
¶
Retrieves the TLDR page for a given command.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
command | str | The command to lookup. | required |
Returns:
Type | Description |
---|---|
str | The content of the TLDR page or an error message. |
Source code in tux/cogs/utility/tldr.py
def get_tldr_page(self, command: str) -> str:
"""
Retrieves the TLDR page for a given command.
Parameters
----------
command : str
The command to lookup.
Returns
-------
str
The content of the TLDR page or an error message.
"""
if command.startswith("-"):
return "Invalid command: Command can't start with a dash (-)."
return self._run_subprocess(["tldr", "-r", command], "No TLDR page found.")
get_tldrs() -> list[str]
¶
_run_subprocess(command_list: list[str], default_response: str) -> str
staticmethod
¶
Helper method to run subprocesses for CLI interactions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
command_list | list[str] | List containing the command and its arguments. | required |
default_response | str | The default response if subprocess does not output anything. | required |
Returns:
Type | Description |
---|---|
str | The stdout from the subprocess as a string, or an error message. |
Raises:
Type | Description |
---|---|
CalledProcessError | If the subprocess fails to run. |
Source code in tux/cogs/utility/tldr.py
@staticmethod
def _run_subprocess(command_list: list[str], default_response: str) -> str:
"""
Helper method to run subprocesses for CLI interactions.
Parameters
----------
command_list : list[str]
List containing the command and its arguments.
default_response : str
The default response if subprocess does not output anything.
Returns
-------
str
The stdout from the subprocess as a string, or an error message.
Raises
-------
subprocess.CalledProcessError
If the subprocess fails to run.
"""
try:
process = subprocess.run(command_list, capture_output=True, text=True, check=True)
except subprocess.CalledProcessError:
return default_response
else:
return process.stdout