From edc6343e12e1b11c6e566c188e1f55c394dd1ba7 Mon Sep 17 00:00:00 2001 From: stijndcl Date: Sun, 24 Jul 2022 16:39:27 +0200 Subject: [PATCH] Create owner-guild-only commands, make sync a bit fancier --- didier/cogs/other.py | 5 +++- didier/cogs/owner.py | 36 ++++++++++++++++++++-------- didier/data/apis/urban_dictionary.py | 9 +++++-- didier/utils/discord/flags/owner.py | 9 ++++++- settings.py | 1 + 5 files changed, 46 insertions(+), 14 deletions(-) diff --git a/didier/cogs/other.py b/didier/cogs/other.py index 04175ed..2a3da2a 100644 --- a/didier/cogs/other.py +++ b/didier/cogs/other.py @@ -19,7 +19,10 @@ class Other(commands.Cog): async def define(self, ctx: commands.Context, *, query: str): """Look up the definition of a word on the Urban Dictionary""" async with ctx.typing(): - definitions = await urban_dictionary.lookup(self.client.http_session, query) + status_code, definitions = await urban_dictionary.lookup(self.client.http_session, query) + if not definitions: + return await ctx.reply(f"Something went wrong (status {status_code})") + await ctx.reply(embed=definitions[0].to_embed(), mention_author=False) @commands.hybrid_command(name="google", description="Google search", usage="[Query]") diff --git a/didier/cogs/owner.py b/didier/cogs/owner.py index 9a93fdf..3977b49 100644 --- a/didier/cogs/owner.py +++ b/didier/cogs/owner.py @@ -4,11 +4,12 @@ import discord from discord import app_commands from discord.ext import commands +import settings from database.crud import custom_commands from database.exceptions.constraints import DuplicateInsertException from database.exceptions.not_found import NoResultFoundException from didier import Didier -from didier.utils.discord.flags.owner import EditCustomFlags +from didier.utils.discord.flags.owner import EditCustomFlags, SyncOptionFlags from didier.views.modals import AddDadJoke, CreateCustomCommand, EditCustomCommand @@ -18,8 +19,18 @@ class Owner(commands.Cog): client: Didier # Slash groups - add_slash = app_commands.Group(name="add", description="Add something new to the database") - edit_slash = app_commands.Group(name="edit", description="Edit an existing database entry") + add_slash = app_commands.Group( + name="add", + description="Add something new to the database", + guild_ids=settings.DISCORD_OWNER_GUILDS, + guild_only=True, + ) + edit_slash = app_commands.Group( + name="edit", + description="Edit an existing database entry", + guild_ids=settings.DISCORD_OWNER_GUILDS, + guild_only=True, + ) def __init__(self, client: Didier): self.client = client @@ -31,16 +42,21 @@ class Owner(commands.Cog): """ return await self.client.is_owner(ctx.author) - @commands.command(name="Error") - async def _error(self, ctx: commands.Context): + @commands.command(name="Error", aliases=["Raise"]) + async def _error(self, ctx: commands.Context, message: str = "Debug"): """Raise an exception for debugging purposes""" - raise Exception("Debug") + raise Exception(message) @commands.command(name="Sync") - async def sync(self, ctx: commands.Context, guild: Optional[discord.Guild] = None): + async def sync(self, ctx: commands.Context, guild: Optional[discord.Guild] = None, *, flags: SyncOptionFlags): """Sync all application-commands in Discord""" if guild is not None: - self.client.tree.copy_global_to(guild=guild) + if flags.clear: + self.client.tree.clear_commands(guild=guild) + + if flags.copy_globals: + self.client.tree.copy_global_to(guild=guild) + await self.client.tree.sync(guild=guild) else: await self.client.tree.sync() @@ -52,7 +68,7 @@ class Owner(commands.Cog): """Command group for [add X] message commands""" @add_msg.command(name="Custom") - async def add_custom(self, ctx: commands.Context, name: str, *, response: str): + async def add_custom_msg(self, ctx: commands.Context, name: str, *, response: str): """Add a new custom command""" async with self.client.db_session as session: try: @@ -63,7 +79,7 @@ class Owner(commands.Cog): await self.client.reject_message(ctx.message) @add_msg.command(name="Alias") - async def add_alias(self, ctx: commands.Context, command: str, alias: str): + async def add_alias_msg(self, ctx: commands.Context, command: str, alias: str): """Add a new alias for a custom command""" async with self.client.db_session as session: try: diff --git a/didier/data/apis/urban_dictionary.py b/didier/data/apis/urban_dictionary.py index 40381ea..e0ebf15 100644 --- a/didier/data/apis/urban_dictionary.py +++ b/didier/data/apis/urban_dictionary.py @@ -1,3 +1,5 @@ +from http import HTTPStatus + from aiohttp import ClientSession from didier.data.embeds.urban_dictionary import Definition @@ -8,10 +10,13 @@ __all__ = ["lookup", "PER_PAGE"] PER_PAGE = 10 -async def lookup(http_session: ClientSession, query: str) -> list[Definition]: +async def lookup(http_session: ClientSession, query: str) -> tuple[int, list[Definition]]: """Fetch the Urban Dictionary definitions for a given word""" url = "https://api.urbandictionary.com/v0/define" async with http_session.get(url, params={"term": query}) as response: + if response.status != HTTPStatus.OK: + return response.status, [] + response_json = await response.json() - return list(map(Definition.parse_obj, response_json["list"])) + return 200, list(map(Definition.parse_obj, response_json["list"])) diff --git a/didier/utils/discord/flags/owner.py b/didier/utils/discord/flags/owner.py index 282957c..b12fb8d 100644 --- a/didier/utils/discord/flags/owner.py +++ b/didier/utils/discord/flags/owner.py @@ -2,7 +2,7 @@ from typing import Optional from didier.utils.discord.flags import PosixFlags -__all__ = ["EditCustomFlags"] +__all__ = ["EditCustomFlags", "SyncOptionFlags"] class EditCustomFlags(PosixFlags): @@ -10,3 +10,10 @@ class EditCustomFlags(PosixFlags): name: Optional[str] = None response: Optional[str] = None + + +class SyncOptionFlags(PosixFlags): + """Flags for the sync command""" + + clear: bool = False + copy_globals: bool = False diff --git a/settings.py b/settings.py index d03b48b..cf20b95 100644 --- a/settings.py +++ b/settings.py @@ -44,6 +44,7 @@ DISCORD_TOKEN: str = env.str("DISCORD_TOKEN") DISCORD_READY_MESSAGE: str = env.str("DISCORD_READY_MESSAGE", "I'M READY I'M READY I'M READY") DISCORD_STATUS_MESSAGE: str = env.str("DISCORD_STATUS_MESSAGE", "with your Didier Dinks.") DISCORD_TEST_GUILDS: list[int] = env.list("DISCORD_TEST_GUILDS", [], subcast=int) +DISCORD_OWNER_GUILDS: Optional[list[int]] = env.list("DISCORD_OWNER_GUILDS", [], subcast=int) or None DISCORD_BOOS_REACT: str = env.str("DISCORD_BOOS_REACT", "<:boos:629603785840263179>") DISCORD_CUSTOM_COMMAND_PREFIX: str = env.str("DISCORD_CUSTOM_COMMAND_PREFIX", "?") BIRTHDAY_ANNOUNCEMENT_CHANNEL: Optional[int] = env.int("BIRTHDAY_ANNOUNCEMENT_CHANNEL", None)