Skip to content

Commit

Permalink
SNIPPETS: Add base functionality for snippet edit.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamievlin committed Mar 20, 2024
1 parent c5a99be commit 98b2006
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions bot/cogs/snippets.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

import discord
from discord.ext import commands
from libs.utils import GuildContext
Expand Down Expand Up @@ -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} <snippet text>`.",
),
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):
Expand Down

0 comments on commit 98b2006

Please sign in to comment.