From 878835ba2947852eeb674af50a499df8b674bba5 Mon Sep 17 00:00:00 2001 From: Supakorn 'Jamie' Rassameemasmuang Date: Sun, 17 Mar 2024 01:36:54 -0600 Subject: [PATCH] SNIPPETS: Add base functionality for snippet edit. --- bot/cogs/snippets.py | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/bot/cogs/snippets.py b/bot/cogs/snippets.py index 61c441e..7f05df1 100644 --- a/bot/cogs/snippets.py +++ b/bot/cogs/snippets.py @@ -1,3 +1,5 @@ +from typing import Optional + import discord from discord.ext import commands from libs.utils import GuildContext @@ -82,10 +84,44 @@ async def show(self, ctx: GuildContext, name: str): async def list(self, ctx, *args): await ctx.send("placeholder for snippet list") + async def edit_prompt_user(self, ctx: GuildContext, name: str): + raise NotImplementedError("TODO: Add prompt for editing snippet.") + @commands.guild_only() @snippet_cmd.command() - async def edit(self, ctx, *args): - await ctx.send("placeholder for snippet edit") + async def edit(self, ctx: GuildContext, name: str, content: Optional[str]): + if content is None: + await self.edit_prompt_user(ctx, name) + return + query = """ + UPDATE snippets + SET content = $3 + WHERE guild_id = $1 AND name = $2 + RETURNING name + """ + + result = await self._bot.pool.fetchrow(query, ctx.guild.id, name, content) + if result is None: + await ctx.reply( + embed=discord.Embed( + title="Oops...", + colour=discord.Colour.red(), + description=f"Cannot edit snippet `{name}` as there is no such " + + "snippet. To create a new snippet with the corresponding " + + f"name, please run `snippet new {name} `.", + ), + ephemeral=True, + ) + else: + await ctx.reply( + embed=discord.Embed( + title="Snippet changed", + colour=discord.Colour.green(), + description=f"The contents of snippet {result[0]} has been " + + f"changed to \n\n{content}", + ), + ephemeral=True, + ) async def setup(bot: Rodhaj):