Skip to content

Commit

Permalink
SNIPPET: Add basic snippet create routine.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamievlin committed Jul 13, 2024
1 parent d633fbc commit 32f7f2d
Showing 1 changed file with 61 additions and 4 deletions.
65 changes: 61 additions & 4 deletions bot/cogs/snippets.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING, Optional, Union

import asyncpg.exceptions
import discord
from discord.ext import commands

from libs.snippets.model import create_snippet, get_snippet
from libs.snippets.views import SnippetPreCreationConfirmationView

if TYPE_CHECKING:
from libs.utils.context import GuildContext
from rodhaj import Rodhaj
Expand Down Expand Up @@ -64,8 +68,41 @@ async def remove(self, ctx: GuildContext, name: str):
# TODO: Run all str inputs through custom converters
@commands.guild_only()
@snippet.command()
async def new(self, ctx, name: str, *, content: Optional[str] = None):
await ctx.send("placeholder for snippet new")
async def new(
self,
ctx: GuildContext,
name: str,
*,
content: Optional[str] = None,
):
if (
await get_snippet(self.pool, ctx.guild.id, ctx.message.author.id, name)
is not None
):
await ctx.send(
content=f"Snippet `{name}` already exists!",
)
return

if not content:
timeout = 15
confirmation_view = SnippetPreCreationConfirmationView(
self.bot, ctx, name, timeout
)
await ctx.reply(
content=f"Create snippet with id `{name}`?",
view=confirmation_view,
delete_after=timeout,
)
else:
self.bot.dispatch(
"snippet_create",
ctx.guild,
ctx.message.author,
name,
content,
ctx,
)

@commands.guild_only()
@snippet.command(name="list")
Expand Down Expand Up @@ -111,7 +148,6 @@ async def edit(self, ctx: GuildContext, name: str, content: Optional[str]):
WHERE name = $1
RETURNING name
"""

result = await self.pool.fetchrow(query, name, content)
if result is None:
await ctx.reply(
Expand All @@ -135,6 +171,27 @@ async def edit(self, ctx: GuildContext, name: str, content: Optional[str]):
ephemeral=True,
)

@commands.Cog.listener()
async def on_snippet_create(
self,
guild: discord.Guild,
creator: Union[discord.User, discord.Member],
snippet_name: str,
snippet_text: str,
response_context: GuildContext,
):
try:
await create_snippet(
self.pool, guild.id, creator.id, snippet_name, snippet_text
)
if response_context:
await response_context.send(
"Snippet created successfully", delete_after=5
)
except asyncpg.exceptions.UniqueViolationError:
if response_context:
await response_context.send("Snippet already exists", delete_after=5)


async def setup(bot: Rodhaj):
await bot.add_cog(Snippets(bot))

0 comments on commit 32f7f2d

Please sign in to comment.