Skip to content

Commit 4144380

Browse files
committed
SNIPPETS: Add base functionality for snippet edit.
1 parent cdc65f3 commit 4144380

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

bot/cogs/snippets.py

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
import discord
24
from discord.ext import commands
35
from libs.utils import GuildContext
@@ -82,10 +84,44 @@ async def show(self, ctx: GuildContext, name: str):
8284
async def list(self, ctx, *args):
8385
await ctx.send("placeholder for snippet list")
8486

87+
async def edit_prompt_user(self, ctx: GuildContext, name: str):
88+
raise NotImplementedError("TODO: Add prompt for editing snippet.")
89+
8590
@commands.guild_only()
8691
@snippet_cmd.command()
87-
async def edit(self, ctx, *args):
88-
await ctx.send("placeholder for snippet edit")
92+
async def edit(self, ctx: GuildContext, name: str, content: Optional[str]):
93+
if content is None:
94+
await self.edit_prompt_user(ctx, name)
95+
return
96+
query = """
97+
UPDATE snippets
98+
SET content = $3
99+
WHERE guild_id = $1 AND name = $2
100+
RETURNING name
101+
"""
102+
103+
result = await self._bot.pool.fetchrow(query, ctx.guild.id, name, content)
104+
if result is None:
105+
await ctx.reply(
106+
embed=discord.Embed(
107+
title="Oops...",
108+
colour=discord.Colour.red(),
109+
description=f"Cannot edit snippet `{name}` as there is no such "
110+
+ "snippet. To create a new snippet with the corresponding "
111+
+ f"name, please run `snippet new {name} <snippet text>`.",
112+
),
113+
ephemeral=True,
114+
)
115+
else:
116+
await ctx.reply(
117+
embed=discord.Embed(
118+
title="Snippet changed",
119+
colour=discord.Colour.green(),
120+
description=f"The contents of snippet {result[0]} has been "
121+
+ f"changed to \n\n{content}",
122+
),
123+
ephemeral=True,
124+
)
89125

90126

91127
async def setup(bot: Rodhaj):

0 commit comments

Comments
 (0)