Skip to content

tux.cogs.snippets.get_snippet

Classes:

Name Description
Snippet

Functions:

Name Description
setup

Load the Snippet cog.

Classes

Snippet(bot: Tux)

Bases: SnippetsBaseCog

Methods:

Name Description
snippet

Retrieve and display a snippet's content.

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.py
Python
def __init__(self, bot: Tux) -> None:
    super().__init__(bot)
    self.snippet.usage = generate_usage(self.snippet)

Functions

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

Retrieve and display a snippet's content.

If the snippet is an alias, it resolves the alias and displays the target snippet's content, indicating the alias relationship.

Parameters:

Name Type Description Default
ctx Context[Tux]

The context of the command.

required
name str

The name of the snippet to retrieve.

required
Source code in tux/cogs/snippets/get_snippet.py
Python
@commands.command(
    name="snippet",
    aliases=["s"],
)
@commands.guild_only()
async def snippet(self, ctx: commands.Context[Tux], name: str) -> None:
    """Retrieve and display a snippet's content.

    If the snippet is an alias, it resolves the alias and displays the
    target snippet's content, indicating the alias relationship.

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

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

    # Increment uses before potentially resolving alias
    await self.db.snippet.increment_snippet_uses(snippet.snippet_id)

    # Handle aliases
    if snippet.alias:
        # Fetch the target snippet
        aliased_snippet = await self.db.snippet.get_snippet_by_name_and_guild_id(
            snippet.alias,
            ctx.guild.id,
        )

        # If alias target doesn't exist, delete the broken alias
        if aliased_snippet is None:
            await self.db.snippet.delete_snippet_by_id(snippet.snippet_id)

            await self.send_snippet_error(
                ctx,
                description=f"Alias `{snippet.snippet_name}` points to a non-existent snippet (`{snippet.alias}`). Deleting alias.",
            )
            return

        # Format message for alias
        text = f"`{snippet.snippet_name}.txt -> {aliased_snippet.snippet_name}.txt` "

        if aliased_snippet.locked:
            text += "🔒 "

        text += f"|| {aliased_snippet.snippet_content}"

    else:
        # Format message for regular snippet
        text = f"`/snippets/{snippet.snippet_name}.txt` "

        if snippet.locked:
            text += "🔒 "

        text += f"|| {snippet.snippet_content}"

    # pagination if text > 2000 characters
    if len(text) <= 2000:
        await ctx.send(text, allowed_mentions=AllowedMentions.none())
        return

    menu = ViewMenu(
        ctx,
        menu_type=ViewMenu.TypeText,
        all_can_click=True,
        show_page_director=False,
        timeout=180,
        delete_on_timeout=True,
    )

    for i in range(0, len(text), 2000):
        page: str = text[i : i + 2000]
        menu.add_page(content=page)

    menu.add_button(ViewButton.back())
    menu.add_button(ViewButton.next())

    await menu.start()
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.py
Python
If the snippet is an alias, it resolves the alias and displays the
target snippet's content, indicating the alias relationship.

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

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

# Increment uses before potentially resolving alias
await self.db.snippet.increment_snippet_uses(snippet.snippet_id)

# Handle aliases
if snippet.alias:
    # Fetch the target snippet
    aliased_snippet = await self.db.snippet.get_snippet_by_name_and_guild_id(
        snippet.alias,
        ctx.guild.id,
    )

    # If alias target doesn't exist, delete the broken alias
    if aliased_snippet is None:
        await self.db.snippet.delete_snippet_by_id(snippet.snippet_id)

        await self.send_snippet_error(
            ctx,
            description=f"Alias `{snippet.snippet_name}` points to a non-existent snippet (`{snippet.alias}`). Deleting alias.",
        )
        return

    # Format message for alias
    text = f"`{snippet.snippet_name}.txt -> {aliased_snippet.snippet_name}.txt` "

    if aliased_snippet.locked:
        text += "🔒 "

    text += f"|| {aliased_snippet.snippet_content}"
_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.py
Python
            # Format message for regular snippet
            text = f"`/snippets/{snippet.snippet_name}.txt` "

            if snippet.locked:
                text += "🔒 "

            text += f"|| {snippet.snippet_content}"

        # pagination if text > 2000 characters
        if len(text) <= 2000:
            await ctx.send(text, allowed_mentions=AllowedMentions.none())
            return

        menu = ViewMenu(
            ctx,
            menu_type=ViewMenu.TypeText,
            all_can_click=True,
            show_page_director=False,
            timeout=180,
            delete_on_timeout=True,
        )

        for i in range(0, len(text), 2000):
            page: str = text[i : i + 2000]
            menu.add_page(content=page)

        menu.add_button(ViewButton.back())
        menu.add_button(ViewButton.next())

        await menu.start()


async def setup(bot: Tux) -> None:
    """Load the Snippet cog."""
    await bot.add_cog(Snippet(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 Snippet cog.

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