Skip to content

Commit 168925f

Browse files
committed
rework cog to better align standards and provide better structure
1 parent 0466650 commit 168925f

File tree

1 file changed

+40
-27
lines changed

1 file changed

+40
-27
lines changed

bot/cogs/snippets.py

+40-27
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,47 @@
1-
from typing import Optional
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING, Optional
24

35
import discord
46
from discord.ext import commands
5-
from libs.utils import GuildContext
6-
from rodhaj import Rodhaj
7+
8+
if TYPE_CHECKING:
9+
from libs.utils.context import GuildContext
10+
11+
from rodhaj import Rodhaj
712

813

914
class Snippets(commands.Cog):
10-
"""
11-
Cog for snippet-related commands (#21)
12-
"""
15+
"""Send or display pre-written text to users"""
1316

1417
def __init__(self, bot: Rodhaj):
15-
self._bot = bot
18+
self.bot = bot
19+
self.pool = self.bot.pool
20+
21+
# Editing Utilities
22+
23+
async def edit_prompt_user(self, ctx: GuildContext, name: str):
24+
raise NotImplementedError("TODO: Add prompt for editing snippet.")
1625

1726
@commands.guild_only()
18-
@commands.group(name="snippet")
19-
async def snippet_cmd(self, ctx: GuildContext):
20-
if ctx.invoked_subcommand is None:
21-
await ctx.send("placeholder for base command")
27+
@commands.hybrid_group(name="snippets", alias=["snippet"], fallback="get")
28+
async def snippet(self, ctx: GuildContext, *, name: str):
29+
"""Allows for use snippets of text for later retrieval or for quicker responses
30+
31+
If an subcommand is not called, then this will search
32+
the database for the requested snippet
33+
"""
34+
await ctx.send("Implement getting snippets here")
2235

2336
@commands.guild_only()
24-
@snippet_cmd.command()
37+
@snippet.command()
2538
async def remove(self, ctx: GuildContext, name: str):
2639
query = """
2740
DELETE FROM snippets
2841
WHERE guild_id = $1 AND name = $2
2942
RETURNING name
3043
"""
31-
result = await self._bot.pool.fetchrow(query, ctx.guild.id, name)
44+
result = await self.pool.fetchrow(query, ctx.guild.id, name)
3245
if result is None:
3346
await ctx.reply(
3447
embed=discord.Embed(
@@ -49,19 +62,27 @@ async def remove(self, ctx: GuildContext, name: str):
4962
ephemeral=True,
5063
)
5164

65+
# TODO: Run all str inputs through custom converters
5266
@commands.guild_only()
53-
@snippet_cmd.command()
54-
async def new(self, ctx, *args):
67+
@snippet.command()
68+
async def new(self, ctx, name: str, *, content: Optional[str] = None):
5569
await ctx.send("placeholder for snippet new")
5670

5771
@commands.guild_only()
58-
@snippet_cmd.command()
72+
@snippet.command(name="list")
73+
async def snippets_list(
74+
self, ctx: GuildContext, json: Optional[bool] = False
75+
) -> None:
76+
await ctx.send("list snippets")
77+
78+
@commands.guild_only()
79+
@snippet.command()
5980
async def show(self, ctx: GuildContext, name: str):
6081
query = """
6182
SELECT content FROM snippets
6283
WHERE guild_id = $1 AND name = $2
6384
"""
64-
data = await self._bot.pool.fetchrow(query, ctx.guild.id, name)
85+
data = await self.pool.fetchrow(query, ctx.guild.id, name)
6586
if data is None:
6687
ret_embed = discord.Embed(
6788
title="Oops...",
@@ -80,15 +101,7 @@ async def show(self, ctx: GuildContext, name: str):
80101
await ctx.reply(embed=ret_data, ephemeral=True)
81102

82103
@commands.guild_only()
83-
@snippet_cmd.command()
84-
async def list(self, ctx, *args):
85-
await ctx.send("placeholder for snippet list")
86-
87-
async def edit_prompt_user(self, ctx: GuildContext, name: str):
88-
raise NotImplementedError("TODO: Add prompt for editing snippet.")
89-
90-
@commands.guild_only()
91-
@snippet_cmd.command()
104+
@snippet.command()
92105
async def edit(self, ctx: GuildContext, name: str, content: Optional[str]):
93106
if content is None:
94107
await self.edit_prompt_user(ctx, name)
@@ -100,7 +113,7 @@ async def edit(self, ctx: GuildContext, name: str, content: Optional[str]):
100113
RETURNING name
101114
"""
102115

103-
result = await self._bot.pool.fetchrow(query, ctx.guild.id, name, content)
116+
result = await self.pool.fetchrow(query, ctx.guild.id, name, content)
104117
if result is None:
105118
await ctx.reply(
106119
embed=discord.Embed(

0 commit comments

Comments
 (0)