Skip to content

Commit 32f7f2d

Browse files
committed
SNIPPET: Add basic snippet create routine.
1 parent d633fbc commit 32f7f2d

File tree

1 file changed

+61
-4
lines changed

1 file changed

+61
-4
lines changed

bot/cogs/snippets.py

+61-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING, Optional
3+
from typing import TYPE_CHECKING, Optional, Union
44

5+
import asyncpg.exceptions
56
import discord
67
from discord.ext import commands
78

9+
from libs.snippets.model import create_snippet, get_snippet
10+
from libs.snippets.views import SnippetPreCreationConfirmationView
11+
812
if TYPE_CHECKING:
913
from libs.utils.context import GuildContext
1014
from rodhaj import Rodhaj
@@ -64,8 +68,41 @@ async def remove(self, ctx: GuildContext, name: str):
6468
# TODO: Run all str inputs through custom converters
6569
@commands.guild_only()
6670
@snippet.command()
67-
async def new(self, ctx, name: str, *, content: Optional[str] = None):
68-
await ctx.send("placeholder for snippet new")
71+
async def new(
72+
self,
73+
ctx: GuildContext,
74+
name: str,
75+
*,
76+
content: Optional[str] = None,
77+
):
78+
if (
79+
await get_snippet(self.pool, ctx.guild.id, ctx.message.author.id, name)
80+
is not None
81+
):
82+
await ctx.send(
83+
content=f"Snippet `{name}` already exists!",
84+
)
85+
return
86+
87+
if not content:
88+
timeout = 15
89+
confirmation_view = SnippetPreCreationConfirmationView(
90+
self.bot, ctx, name, timeout
91+
)
92+
await ctx.reply(
93+
content=f"Create snippet with id `{name}`?",
94+
view=confirmation_view,
95+
delete_after=timeout,
96+
)
97+
else:
98+
self.bot.dispatch(
99+
"snippet_create",
100+
ctx.guild,
101+
ctx.message.author,
102+
name,
103+
content,
104+
ctx,
105+
)
69106

70107
@commands.guild_only()
71108
@snippet.command(name="list")
@@ -111,7 +148,6 @@ async def edit(self, ctx: GuildContext, name: str, content: Optional[str]):
111148
WHERE name = $1
112149
RETURNING name
113150
"""
114-
115151
result = await self.pool.fetchrow(query, name, content)
116152
if result is None:
117153
await ctx.reply(
@@ -135,6 +171,27 @@ async def edit(self, ctx: GuildContext, name: str, content: Optional[str]):
135171
ephemeral=True,
136172
)
137173

174+
@commands.Cog.listener()
175+
async def on_snippet_create(
176+
self,
177+
guild: discord.Guild,
178+
creator: Union[discord.User, discord.Member],
179+
snippet_name: str,
180+
snippet_text: str,
181+
response_context: GuildContext,
182+
):
183+
try:
184+
await create_snippet(
185+
self.pool, guild.id, creator.id, snippet_name, snippet_text
186+
)
187+
if response_context:
188+
await response_context.send(
189+
"Snippet created successfully", delete_after=5
190+
)
191+
except asyncpg.exceptions.UniqueViolationError:
192+
if response_context:
193+
await response_context.send("Snippet already exists", delete_after=5)
194+
138195

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

0 commit comments

Comments
 (0)