From ef4e42fff0be6232d4c74bbd1f18c5f5fc2681b1 Mon Sep 17 00:00:00 2001 From: kku Date: Mon, 16 Dec 2024 20:49:58 +0100 Subject: [PATCH] Improved error handling --- cogs/custom_channels.py | 44 +++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/cogs/custom_channels.py b/cogs/custom_channels.py index 14f2f98..e55053a 100644 --- a/cogs/custom_channels.py +++ b/cogs/custom_channels.py @@ -1,6 +1,7 @@ -from typing import Any, Dict, List, Union +import logging +from typing import Any, Dict, Union -from discord import ApplicationContext, Embed, option, TextChannel +from discord import ApplicationContext, Embed, option, TextChannel, Role from discord import utils as ds_utils from discord.abc import GuildChannel from discord.commands import SlashCommandGroup @@ -14,6 +15,8 @@ from enums import Color from modules.database import col_users from modules.utils_sync import guild_name +logger = logging.getLogger(__name__) + class CustomChannels(commands.Cog): def __init__(self, client: PycordBot): @@ -37,11 +40,23 @@ class CustomChannels(commands.Cog): @option("name", description="Назва каналу") @option("reactions", description="Дозволити реакції") @option("threads", description="Дозволити гілки") - async def customchannel_get_cmd( + async def custom_channel_get_cmd( self, ctx: ApplicationContext, name: str, reactions: bool, threads: bool ) -> None: holo_user_ctx: HoloUser = HoloUser(ctx.user) + # Return if the user is using the command outside of a guild + if not hasattr(ctx.author, "guild"): + await ctx.defer(ephemeral=True) + await ctx.respond( + embed=Embed( + title="Помилка виконання", + description="Виконання за межами сервера не є можливим.", + color=Color.FAIL, + ) + ) + return + # Return if the user already has a custom channel if holo_user_ctx.customchannel is not None: await ctx.defer(ephemeral=True) @@ -91,14 +106,19 @@ class CustomChannels(commands.Cog): ) ) - bots: List[Dict[str, Any]] = await config_get("bots") + bots: Dict[str, Any] = await config_get("bots") for bot in bots: - await created_channel.set_permissions( - ds_utils.get(ctx.user.guild.roles, id=bots[bot]["role"]), - view_channel=False, + role: Union[Role, None] = ds_utils.get( + ctx.user.guild.roles, id=bots[bot]["role"] ) + if role is not None: + await created_channel.set_permissions( + role, + view_channel=False, + ) + @custom_channel_group.command( name="edit", description="Змінити параметри особистого каналу", @@ -107,7 +127,7 @@ class CustomChannels(commands.Cog): @option("name", description="Назва каналу") @option("reactions", description="Дозволити реакції") @option("threads", description="Дозволити гілки") - async def customchannel_edit_cmd( + async def custom_channel_edit_cmd( self, ctx: ApplicationContext, name: str, reactions: bool, threads: bool ) -> None: holo_user_ctx: HoloUser = HoloUser(ctx.user) @@ -150,7 +170,7 @@ class CustomChannels(commands.Cog): guild_ids=[sync_config_get("guild")], ) @option("confirm", description="Підтвердження операції") - async def customchannel_remove_cmd( + async def custom_channel_remove_cmd( self, ctx: ApplicationContext, confirm: bool = False ) -> None: holo_user_ctx: HoloUser = HoloUser(ctx.user) @@ -200,7 +220,7 @@ class CustomChannels(commands.Cog): await holo_user_ctx.set("customchannel", None) - if ctx.channel_id != custom_channel.id: + try: await ctx.respond( embed=Embed( title="Канал знищено", @@ -208,6 +228,10 @@ class CustomChannels(commands.Cog): color=Color.DEFAULT, ) ) + except Exception as exc: + logger.warning( + "Could not send a custom channel removal confirmation due to: %s", exc + ) def setup(client: PycordBot) -> None: