Create owner-guild-only commands, make sync a bit fancier

pull/125/head
stijndcl 2022-07-24 16:39:27 +02:00
parent 0834a4ccbc
commit edc6343e12
5 changed files with 46 additions and 14 deletions

View File

@ -19,7 +19,10 @@ class Other(commands.Cog):
async def define(self, ctx: commands.Context, *, query: str): async def define(self, ctx: commands.Context, *, query: str):
"""Look up the definition of a word on the Urban Dictionary""" """Look up the definition of a word on the Urban Dictionary"""
async with ctx.typing(): 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) await ctx.reply(embed=definitions[0].to_embed(), mention_author=False)
@commands.hybrid_command(name="google", description="Google search", usage="[Query]") @commands.hybrid_command(name="google", description="Google search", usage="[Query]")

View File

@ -4,11 +4,12 @@ import discord
from discord import app_commands from discord import app_commands
from discord.ext import commands from discord.ext import commands
import settings
from database.crud import custom_commands from database.crud import custom_commands
from database.exceptions.constraints import DuplicateInsertException from database.exceptions.constraints import DuplicateInsertException
from database.exceptions.not_found import NoResultFoundException from database.exceptions.not_found import NoResultFoundException
from didier import Didier 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 from didier.views.modals import AddDadJoke, CreateCustomCommand, EditCustomCommand
@ -18,8 +19,18 @@ class Owner(commands.Cog):
client: Didier client: Didier
# Slash groups # Slash groups
add_slash = app_commands.Group(name="add", description="Add something new to the database") add_slash = app_commands.Group(
edit_slash = app_commands.Group(name="edit", description="Edit an existing database entry") 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): def __init__(self, client: Didier):
self.client = client self.client = client
@ -31,16 +42,21 @@ class Owner(commands.Cog):
""" """
return await self.client.is_owner(ctx.author) return await self.client.is_owner(ctx.author)
@commands.command(name="Error") @commands.command(name="Error", aliases=["Raise"])
async def _error(self, ctx: commands.Context): async def _error(self, ctx: commands.Context, message: str = "Debug"):
"""Raise an exception for debugging purposes""" """Raise an exception for debugging purposes"""
raise Exception("Debug") raise Exception(message)
@commands.command(name="Sync") @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""" """Sync all application-commands in Discord"""
if guild is not None: 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) await self.client.tree.sync(guild=guild)
else: else:
await self.client.tree.sync() await self.client.tree.sync()
@ -52,7 +68,7 @@ class Owner(commands.Cog):
"""Command group for [add X] message commands""" """Command group for [add X] message commands"""
@add_msg.command(name="Custom") @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""" """Add a new custom command"""
async with self.client.db_session as session: async with self.client.db_session as session:
try: try:
@ -63,7 +79,7 @@ class Owner(commands.Cog):
await self.client.reject_message(ctx.message) await self.client.reject_message(ctx.message)
@add_msg.command(name="Alias") @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""" """Add a new alias for a custom command"""
async with self.client.db_session as session: async with self.client.db_session as session:
try: try:

View File

@ -1,3 +1,5 @@
from http import HTTPStatus
from aiohttp import ClientSession from aiohttp import ClientSession
from didier.data.embeds.urban_dictionary import Definition from didier.data.embeds.urban_dictionary import Definition
@ -8,10 +10,13 @@ __all__ = ["lookup", "PER_PAGE"]
PER_PAGE = 10 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""" """Fetch the Urban Dictionary definitions for a given word"""
url = "https://api.urbandictionary.com/v0/define" url = "https://api.urbandictionary.com/v0/define"
async with http_session.get(url, params={"term": query}) as response: async with http_session.get(url, params={"term": query}) as response:
if response.status != HTTPStatus.OK:
return response.status, []
response_json = await response.json() response_json = await response.json()
return list(map(Definition.parse_obj, response_json["list"])) return 200, list(map(Definition.parse_obj, response_json["list"]))

View File

@ -2,7 +2,7 @@ from typing import Optional
from didier.utils.discord.flags import PosixFlags from didier.utils.discord.flags import PosixFlags
__all__ = ["EditCustomFlags"] __all__ = ["EditCustomFlags", "SyncOptionFlags"]
class EditCustomFlags(PosixFlags): class EditCustomFlags(PosixFlags):
@ -10,3 +10,10 @@ class EditCustomFlags(PosixFlags):
name: Optional[str] = None name: Optional[str] = None
response: Optional[str] = None response: Optional[str] = None
class SyncOptionFlags(PosixFlags):
"""Flags for the sync command"""
clear: bool = False
copy_globals: bool = False

View File

@ -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_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_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_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_BOOS_REACT: str = env.str("DISCORD_BOOS_REACT", "<:boos:629603785840263179>")
DISCORD_CUSTOM_COMMAND_PREFIX: str = env.str("DISCORD_CUSTOM_COMMAND_PREFIX", "?") DISCORD_CUSTOM_COMMAND_PREFIX: str = env.str("DISCORD_CUSTOM_COMMAND_PREFIX", "?")
BIRTHDAY_ANNOUNCEMENT_CHANNEL: Optional[int] = env.int("BIRTHDAY_ANNOUNCEMENT_CHANNEL", None) BIRTHDAY_ANNOUNCEMENT_CHANNEL: Optional[int] = env.int("BIRTHDAY_ANNOUNCEMENT_CHANNEL", None)