Skip to content

tux.cogs.snippets.get_snippet_info

Classes:

Name Description
SnippetInfo

Functions:

Name Description
setup

Load the SnippetInfo cog.

Classes

SnippetInfo(bot: Tux)

Bases: SnippetsBaseCog

Methods:

Name Description
snippet_info

Display detailed information about a snippet.

is_snippetbanned

Check if a user is currently snippet banned in a guild.

check_if_user_has_mod_override

Check if the user invoking the command has moderator permissions (PL >= configured level).

snippet_check

Check if a user is allowed to modify or delete a snippet.

send_snippet_error

Send a standardized snippet error embed.

Source code in tux/cogs/snippets/get_snippet_info.py
Python
def __init__(self, bot: Tux) -> None:
    super().__init__(bot)
    self.snippet_info.usage = generate_usage(self.snippet_info)

Functions

snippet_info(ctx: commands.Context[Tux], name: str) -> None async

Display detailed information about a snippet.

Shows the author, creation date, content/alias target, uses, and lock status.

Parameters:

Name Type Description Default
ctx Context[Tux]

The context of the command.

required
name str

The name of the snippet to get information about.

required
Source code in tux/cogs/snippets/get_snippet_info.py
Python
@commands.command(
    name="snippetinfo",
    aliases=["si"],
)
@commands.guild_only()
async def snippet_info(self, ctx: commands.Context[Tux], name: str) -> None:
    """Display detailed information about a snippet.

    Shows the author, creation date, content/alias target, uses, and lock status.

    Parameters
    ----------
    ctx : commands.Context[Tux]
        The context of the command.
    name : str
        The name of the snippet to get information about.
    """
    assert ctx.guild

    # Fetch the snippet, send error if not found
    snippet = await self._get_snippet_or_error(ctx, name)
    if not snippet:
        return

    # Attempt to resolve author, default to showing ID if not found
    author = self.bot.get_user(snippet.snippet_user_id)
    author_display = author.mention if author else f"<@!{snippet.snippet_user_id}> (Not found)"

    # Attempt to get aliases if any
    aliases = [alias.snippet_name for alias in (await self.db.snippet.get_all_aliases(name, ctx.guild.id))]

    # Determine content field details
    content_field_name = "Alias Target" if snippet.alias else "Content Preview"
    content_field_value = f"{snippet.alias or snippet.snippet_content}"

    # Create and populate the info embed
    embed: discord.Embed = EmbedCreator.create_embed(
        bot=self.bot,
        embed_type=EmbedCreator.DEFAULT,
        user_name=ctx.author.name,
        user_display_avatar=ctx.author.display_avatar.url,
        title="Snippet Information",
        message_timestamp=snippet.snippet_created_at or datetime.fromtimestamp(0, UTC),
    )

    embed.add_field(name="Name", value=snippet.snippet_name, inline=True)
    embed.add_field(name="Aliases", value=", ".join(f"`{alias}`" for alias in aliases), inline=True)
    embed.add_field(name="Author", value=author_display, inline=True)
    embed.add_field(name="Uses", value=str(snippet.uses), inline=True)  # Ensure string
    embed.add_field(name="Locked", value="Yes" if snippet.locked else "No", inline=True)

    embed.add_field(
        name=content_field_name,
        value=f"> -# {truncate(text=content_field_value, length=256)}",
        inline=False,
    )

    await ctx.send(embed=embed)
is_snippetbanned(guild_id: int, user_id: int) -> bool async

Check if a user is currently snippet banned in a guild.

Parameters:

Name Type Description Default
guild_id int

The ID of the guild to check.

required
user_id int

The ID of the user to check.

required

Returns:

Type Description
bool

True if the user is snippet banned, False otherwise.

Source code in tux/cogs/snippets/get_snippet_info.py
Python
"""Display detailed information about a snippet.

Shows the author, creation date, content/alias target, uses, and lock status.

Parameters
----------
ctx : commands.Context[Tux]
    The context of the command.
name : str
    The name of the snippet to get information about.
"""
assert ctx.guild

# Fetch the snippet, send error if not found
snippet = await self._get_snippet_or_error(ctx, name)
if not snippet:
    return

# Attempt to resolve author, default to showing ID if not found
author = self.bot.get_user(snippet.snippet_user_id)
author_display = author.mention if author else f"<@!{snippet.snippet_user_id}> (Not found)"

# Attempt to get aliases if any
aliases = [alias.snippet_name for alias in (await self.db.snippet.get_all_aliases(name, ctx.guild.id))]

# Determine content field details
content_field_name = "Alias Target" if snippet.alias else "Content Preview"
content_field_value = f"{snippet.alias or snippet.snippet_content}"

# Create and populate the info embed
embed: discord.Embed = EmbedCreator.create_embed(
    bot=self.bot,
    embed_type=EmbedCreator.DEFAULT,
    user_name=ctx.author.name,
    user_display_avatar=ctx.author.display_avatar.url,
    title="Snippet Information",
    message_timestamp=snippet.snippet_created_at or datetime.fromtimestamp(0, UTC),
)

embed.add_field(name="Name", value=snippet.snippet_name, inline=True)
embed.add_field(name="Aliases", value=", ".join(f"`{alias}`" for alias in aliases), inline=True)
embed.add_field(name="Author", value=author_display, inline=True)
embed.add_field(name="Uses", value=str(snippet.uses), inline=True)  # Ensure string
embed.add_field(name="Locked", value="Yes" if snippet.locked else "No", inline=True)

embed.add_field(
_create_snippets_list_embed(ctx: commands.Context[Tux], snippets: list[Snippet], total_snippets: int, search_query: str | None = None) -> discord.Embed

Create an embed for displaying a paginated list of snippets.

Parameters:

Name Type Description Default
ctx Context[Tux]

The context object.

required
snippets list[Snippet]

The list of snippets for the current page.

required
total_snippets int

The total number of snippets matching the query.

required
search_query str | None

The search query used, if any.

None

Returns:

Type Description
Embed

The generated embed.

Source code in tux/cogs/snippets/get_snippet_info.py
Python
            value=f"> -# {truncate(text=content_field_value, length=256)}",
            inline=False,
        )

        await ctx.send(embed=embed)


async def setup(bot: Tux) -> None:
    """Load the SnippetInfo cog."""
    await bot.add_cog(SnippetInfo(bot))
check_if_user_has_mod_override(ctx: commands.Context[Tux]) -> bool async

Check if the user invoking the command has moderator permissions (PL >= configured level).

snippet_check(ctx: commands.Context[Tux], snippet_locked: bool = False, snippet_user_id: int = 0) -> tuple[bool, str] async

Check if a user is allowed to modify or delete a snippet.

Checks for moderator override, snippet bans, role restrictions, snippet lock status, and snippet ownership.

Parameters:

Name Type Description Default
ctx Context[Tux]

The context object.

required
snippet_locked bool

Whether the snippet is locked. Checked only if True. Defaults to False.

False
snippet_user_id int

The ID of the snippet's author. Checked only if non-zero. Defaults to 0.

0

Returns:

Type Description
tuple[bool, str]

A tuple containing a boolean indicating permission status and a reason string.

_get_snippet_or_error(ctx: commands.Context[Tux], name: str) -> Snippet | None async

Fetch a snippet by name and guild, sending an error embed if not found.

Parameters:

Name Type Description Default
ctx Context[Tux]

The context object.

required
name str

The name of the snippet to fetch.

required

Returns:

Type Description
Snippet | None

The fetched Snippet object, or None if not found.

send_snippet_error(ctx: commands.Context[Tux], description: str) -> None async

Send a standardized snippet error embed.

Parameters:

Name Type Description Default
ctx Context[Tux]

The context object.

required
description str

The error message description.

required

Functions

setup(bot: Tux) -> None async

Load the SnippetInfo cog.

Source code in tux/cogs/snippets/get_snippet_info.py
Python
async def setup(bot: Tux) -> None:
    """Load the SnippetInfo cog."""
    await bot.add_cog(SnippetInfo(bot))