Skip to content

tux.cogs.snippets.delete_snippet

Classes:

Name Description
DeleteSnippet

Functions:

Name Description
setup

Load the DeleteSnippet cog.

Classes

DeleteSnippet(bot: Tux)

Bases: SnippetsBaseCog

Methods:

Name Description
delete_snippet

Delete a snippet by name.

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

Functions

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

Delete a snippet by name.

Checks for ownership and lock status before deleting.

Parameters:

Name Type Description Default
ctx Context[Tux]

The context of the command.

required
name str

The name of the snippet to delete.

required
Source code in tux/cogs/snippets/delete_snippet.py
Python
@commands.command(
    name="deletesnippet",
    aliases=["ds"],
)
@commands.guild_only()
async def delete_snippet(self, ctx: commands.Context[Tux], name: str) -> None:
    """Delete a snippet by name.

    Checks for ownership and lock status before deleting.

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

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

    # Check permissions (role, ban, lock, ownership)
    can_delete, reason = await self.snippet_check(
        ctx,
        snippet_locked=snippet.locked,
        snippet_user_id=snippet.snippet_user_id,
    )

    if not can_delete:
        await self.send_snippet_error(ctx, description=reason)
        return

    # Delete the snippet
    await self.db.snippet.delete_snippet_by_id(snippet.snippet_id)

    await ctx.send("Snippet deleted.", delete_after=CONST.DEFAULT_DELETE_AFTER, ephemeral=True)

    logger.info(f"{ctx.author} deleted snippet '{name}'. Override: {reason}")
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/delete_snippet.py
Python
        Parameters
        ----------
        ctx : commands.Context[Tux]
            The context of the command.
        name : str
            The name of the snippet to delete.
        """
        assert ctx.guild

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

        # Check permissions (role, ban, lock, ownership)
        can_delete, reason = await self.snippet_check(
            ctx,
            snippet_locked=snippet.locked,
            snippet_user_id=snippet.snippet_user_id,
        )

        if not can_delete:
            await self.send_snippet_error(ctx, description=reason)
            return

        # Delete the snippet
        await self.db.snippet.delete_snippet_by_id(snippet.snippet_id)

        await ctx.send("Snippet deleted.", delete_after=CONST.DEFAULT_DELETE_AFTER, ephemeral=True)

        logger.info(f"{ctx.author} deleted snippet '{name}'. Override: {reason}")


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

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 DeleteSnippet cog.

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