tux.cogs.snippets.edit_snippet
¶
Classes:
Name | Description |
---|---|
EditSnippet | |
Functions:
Name | Description |
---|---|
setup | Load the EditSnippet cog. |
Classes¶
EditSnippet(bot: Tux)
¶
Bases: SnippetsBaseCog
Methods:
Name | Description |
---|---|
edit_snippet | Edit an existing 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/edit_snippet.py
Functions¶
edit_snippet(ctx: commands.Context[Tux], name: str, *, content: str) -> None
async
¶
Edit an existing snippet.
Checks for ownership and lock status before editing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx | Context[Tux] | The context of the command. | required |
name | str | The name of the snippet to edit. | required |
content | str | The new content for the snippet. | required |
Source code in tux/cogs/snippets/edit_snippet.py
@commands.command(
name="editsnippet",
aliases=["es"],
)
@commands.guild_only()
async def edit_snippet(self, ctx: commands.Context[Tux], name: str, *, content: str) -> None:
"""Edit an existing snippet.
Checks for ownership and lock status before editing.
Parameters
----------
ctx : commands.Context[Tux]
The context of the command.
name : str
The name of the snippet to edit.
content : str
The new content for the snippet.
"""
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_edit, reason = await self.snippet_check(
ctx,
snippet_locked=snippet.locked,
snippet_user_id=snippet.snippet_user_id,
)
if not can_edit:
await self.send_snippet_error(ctx, description=reason)
return
# Update the snippet content
await self.db.snippet.update_snippet_by_id(
snippet_id=snippet.snippet_id,
snippet_content=content,
)
await ctx.send("Snippet edited.", delete_after=CONST.DEFAULT_DELETE_AFTER, ephemeral=True)
logger.info(f"{ctx.author} edited 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/edit_snippet.py
Parameters
----------
ctx : commands.Context[Tux]
The context of the command.
name : str
The name of the snippet to edit.
content : str
The new content for the snippet.
"""
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_edit, reason = await self.snippet_check(
ctx,
snippet_locked=snippet.locked,
snippet_user_id=snippet.snippet_user_id,
)
if not can_edit:
await self.send_snippet_error(ctx, description=reason)
return
# Update the snippet content
await self.db.snippet.update_snippet_by_id(
snippet_id=snippet.snippet_id,
snippet_content=content,
)
await ctx.send("Snippet edited.", delete_after=CONST.DEFAULT_DELETE_AFTER, ephemeral=True)
logger.info(f"{ctx.author} edited snippet '{name}'. Override: {reason}")
async def setup(bot: Tux) -> None:
"""Load the EditSnippet cog."""
await bot.add_cog(EditSnippet(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. |