mirror of
https://github.com/allthingslinux/tux.git
synced 2024-10-03 00:53:12 +00:00
Merge pull request #222 from allthingslinux/per-guild-snippets
[Commands] Make snippets per-guild
This commit is contained in:
commit
53cd967c47
3 changed files with 65 additions and 6 deletions
|
@ -126,6 +126,10 @@ model Snippets {
|
|||
// The creation time of the snippet
|
||||
created_at DateTime? @default(now())
|
||||
|
||||
// The server ID of the guild where the snippet was created
|
||||
// 0 is the default value for this field for migration purposes
|
||||
server_id BigInt @default(0)
|
||||
|
||||
// This field establishes a relationship with the `Users` model. `author_id` is the ID of the user who created the snippet. The line `author Users @relation(fields: [author_id], references: [id])` links to the `Users` model, indicating that an instance of `Users` (the author) is associated with this snippet.
|
||||
author Users @relation(fields: [author_id], references: [id])
|
||||
author_id BigInt
|
||||
|
|
|
@ -34,10 +34,17 @@ class Snippets(commands.Cog):
|
|||
The page number, by default 1.
|
||||
"""
|
||||
|
||||
if ctx.guild is None:
|
||||
await ctx.send("This command cannot be used in direct messages.")
|
||||
return
|
||||
|
||||
snippets: list[SnippetsModel] = await self.db_controller.get_all_snippets_sorted(
|
||||
newestfirst=True
|
||||
)
|
||||
|
||||
# remove snippets that are not in the current server
|
||||
snippets = [snippet for snippet in snippets if snippet.server_id == ctx.guild.id]
|
||||
|
||||
# Calculate the number of pages based on the number of snippets
|
||||
pages = 1 if len(snippets) <= 10 else len(snippets) // 10 + 1
|
||||
|
||||
|
@ -93,7 +100,11 @@ class Snippets(commands.Cog):
|
|||
The name of the snippet.
|
||||
"""
|
||||
|
||||
snippet = await self.db_controller.get_snippet_by_name(name)
|
||||
if ctx.guild is None:
|
||||
await ctx.send("This command cannot be used in direct messages.")
|
||||
return
|
||||
|
||||
snippet = await self.db_controller.get_snippet_by_name_in_server(name, ctx.guild.id)
|
||||
|
||||
if snippet is None:
|
||||
embed = EmbedCreator.create_error_embed(
|
||||
|
@ -131,7 +142,11 @@ class Snippets(commands.Cog):
|
|||
The name of the snippet.
|
||||
"""
|
||||
|
||||
snippet = await self.db_controller.get_snippet_by_name(name)
|
||||
if ctx.guild is None:
|
||||
await ctx.send("This command cannot be used in direct messages.")
|
||||
return
|
||||
|
||||
snippet = await self.db_controller.get_snippet_by_name_in_server(name, ctx.guild.id)
|
||||
|
||||
if snippet is None:
|
||||
embed = EmbedCreator.create_error_embed(
|
||||
|
@ -164,7 +179,11 @@ class Snippets(commands.Cog):
|
|||
The name of the snippet.
|
||||
"""
|
||||
|
||||
snippet = await self.db_controller.get_snippet_by_name(name)
|
||||
if ctx.guild is None:
|
||||
await ctx.send("This command cannot be used in direct messages.")
|
||||
return
|
||||
|
||||
snippet = await self.db_controller.get_snippet_by_name_in_server(name, ctx.guild.id)
|
||||
|
||||
if snippet is None:
|
||||
embed = EmbedCreator.create_error_embed(
|
||||
|
@ -207,6 +226,10 @@ class Snippets(commands.Cog):
|
|||
The name and content of the snippet.
|
||||
"""
|
||||
|
||||
if ctx.guild is None:
|
||||
await ctx.send("This command cannot be used in direct messages.")
|
||||
return
|
||||
|
||||
args = arg.split(" ")
|
||||
if len(args) < 2:
|
||||
embed = EmbedCreator.create_error_embed(
|
||||
|
@ -221,9 +244,10 @@ class Snippets(commands.Cog):
|
|||
content = " ".join(args[1:])
|
||||
created_at = datetime.datetime.now(datetime.UTC)
|
||||
author_id = ctx.author.id
|
||||
server_id = ctx.guild.id
|
||||
|
||||
# Check if the snippet already exists
|
||||
if await self.db_controller.get_snippet_by_name(name) is not None:
|
||||
if await self.db_controller.get_snippet_by_name_in_server(name, server_id) is not None:
|
||||
embed = EmbedCreator.create_error_embed(
|
||||
title="Error", description="Snippet already exists.", ctx=ctx
|
||||
)
|
||||
|
@ -246,6 +270,7 @@ class Snippets(commands.Cog):
|
|||
content=content,
|
||||
created_at=created_at,
|
||||
author_id=author_id,
|
||||
server_id=server_id,
|
||||
)
|
||||
|
||||
await ctx.send("Snippet created.")
|
||||
|
|
|
@ -54,8 +54,26 @@ class SnippetsController:
|
|||
"""
|
||||
return await self.table.find_first(where={"name": name})
|
||||
|
||||
async def get_snippet_by_name_in_server(self, name: str, server_id: int) -> Snippets | None:
|
||||
"""
|
||||
Retrieves a snippet from the database based on the specified name and server ID.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
name : str
|
||||
The name of the snippet to retrieve.
|
||||
server_id : int
|
||||
The server ID to retrieve the snippet from.
|
||||
|
||||
Returns
|
||||
-------
|
||||
Snippets or None
|
||||
The snippet if found, otherwise None.
|
||||
"""
|
||||
return await self.table.find_first(where={"name": name, "server_id": server_id})
|
||||
|
||||
async def create_snippet(
|
||||
self, name: str, content: str, created_at: datetime.datetime, author_id: int
|
||||
self, name: str, content: str, created_at: datetime.datetime, author_id: int, server_id: int
|
||||
) -> Snippets:
|
||||
"""
|
||||
Creates a new snippet in the database with the specified name, content, creation date, and author ID.
|
||||
|
@ -82,6 +100,7 @@ class SnippetsController:
|
|||
"content": content,
|
||||
"created_at": created_at,
|
||||
"author_id": author_id,
|
||||
"server_id": server_id,
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -100,7 +119,9 @@ class SnippetsController:
|
|||
"""
|
||||
await self.table.delete(where={"name": name})
|
||||
|
||||
async def update_snippet(self, name: str, content: str) -> Snippets | None:
|
||||
async def update_snippet(
|
||||
self, name: str, content: str, server_id: int | None
|
||||
) -> Snippets | None:
|
||||
"""
|
||||
Updates a snippet in the database with the specified name and new content.
|
||||
|
||||
|
@ -110,12 +131,21 @@ class SnippetsController:
|
|||
The name of the snippet to update.
|
||||
content : str
|
||||
The new content for the snippet.
|
||||
server_id : int, optional
|
||||
The server ID to update the snippet with, by default None.
|
||||
|
||||
Returns
|
||||
-------
|
||||
Snippets or None
|
||||
The updated snippet if successful, otherwise None if the snippet was not found.
|
||||
"""
|
||||
|
||||
if server_id:
|
||||
return await self.table.update(
|
||||
where={"name": name},
|
||||
data={"content": content, "server_id": server_id},
|
||||
)
|
||||
|
||||
return await self.table.update(
|
||||
where={"name": name},
|
||||
data={"content": content},
|
||||
|
|
Loading…
Reference in a new issue