|
| 1 | +from typing import Optional |
| 2 | + |
1 | 3 | import discord
|
2 | 4 | from discord.ext import commands
|
3 | 5 | from libs.utils import GuildContext
|
@@ -82,10 +84,44 @@ async def show(self, ctx: GuildContext, name: str):
|
82 | 84 | async def list(self, ctx, *args):
|
83 | 85 | await ctx.send("placeholder for snippet list")
|
84 | 86 |
|
| 87 | + async def edit_prompt_user(self, ctx: GuildContext, name: str): |
| 88 | + raise NotImplementedError("TODO: Add prompt for editing snippet.") |
| 89 | + |
85 | 90 | @commands.guild_only()
|
86 | 91 | @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 | + ) |
89 | 125 |
|
90 | 126 |
|
91 | 127 | async def setup(bot: Rodhaj):
|
|
0 commit comments