Improved error handling

This commit is contained in:
kku 2024-12-16 20:49:58 +01:00
parent c05cf64ae0
commit ef4e42fff0

View File

@ -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 import utils as ds_utils
from discord.abc import GuildChannel from discord.abc import GuildChannel
from discord.commands import SlashCommandGroup from discord.commands import SlashCommandGroup
@ -14,6 +15,8 @@ from enums import Color
from modules.database import col_users from modules.database import col_users
from modules.utils_sync import guild_name from modules.utils_sync import guild_name
logger = logging.getLogger(__name__)
class CustomChannels(commands.Cog): class CustomChannels(commands.Cog):
def __init__(self, client: PycordBot): def __init__(self, client: PycordBot):
@ -37,11 +40,23 @@ class CustomChannels(commands.Cog):
@option("name", description="Назва каналу") @option("name", description="Назва каналу")
@option("reactions", description="Дозволити реакції") @option("reactions", description="Дозволити реакції")
@option("threads", description="Дозволити гілки") @option("threads", description="Дозволити гілки")
async def customchannel_get_cmd( async def custom_channel_get_cmd(
self, ctx: ApplicationContext, name: str, reactions: bool, threads: bool self, ctx: ApplicationContext, name: str, reactions: bool, threads: bool
) -> None: ) -> None:
holo_user_ctx: HoloUser = HoloUser(ctx.user) 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 # Return if the user already has a custom channel
if holo_user_ctx.customchannel is not None: if holo_user_ctx.customchannel is not None:
await ctx.defer(ephemeral=True) 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: for bot in bots:
await created_channel.set_permissions( role: Union[Role, None] = ds_utils.get(
ds_utils.get(ctx.user.guild.roles, id=bots[bot]["role"]), ctx.user.guild.roles, id=bots[bot]["role"]
view_channel=False,
) )
if role is not None:
await created_channel.set_permissions(
role,
view_channel=False,
)
@custom_channel_group.command( @custom_channel_group.command(
name="edit", name="edit",
description="Змінити параметри особистого каналу", description="Змінити параметри особистого каналу",
@ -107,7 +127,7 @@ class CustomChannels(commands.Cog):
@option("name", description="Назва каналу") @option("name", description="Назва каналу")
@option("reactions", description="Дозволити реакції") @option("reactions", description="Дозволити реакції")
@option("threads", description="Дозволити гілки") @option("threads", description="Дозволити гілки")
async def customchannel_edit_cmd( async def custom_channel_edit_cmd(
self, ctx: ApplicationContext, name: str, reactions: bool, threads: bool self, ctx: ApplicationContext, name: str, reactions: bool, threads: bool
) -> None: ) -> None:
holo_user_ctx: HoloUser = HoloUser(ctx.user) holo_user_ctx: HoloUser = HoloUser(ctx.user)
@ -150,7 +170,7 @@ class CustomChannels(commands.Cog):
guild_ids=[sync_config_get("guild")], guild_ids=[sync_config_get("guild")],
) )
@option("confirm", description="Підтвердження операції") @option("confirm", description="Підтвердження операції")
async def customchannel_remove_cmd( async def custom_channel_remove_cmd(
self, ctx: ApplicationContext, confirm: bool = False self, ctx: ApplicationContext, confirm: bool = False
) -> None: ) -> None:
holo_user_ctx: HoloUser = HoloUser(ctx.user) holo_user_ctx: HoloUser = HoloUser(ctx.user)
@ -200,7 +220,7 @@ class CustomChannels(commands.Cog):
await holo_user_ctx.set("customchannel", None) await holo_user_ctx.set("customchannel", None)
if ctx.channel_id != custom_channel.id: try:
await ctx.respond( await ctx.respond(
embed=Embed( embed=Embed(
title="Канал знищено", title="Канал знищено",
@ -208,6 +228,10 @@ class CustomChannels(commands.Cog):
color=Color.DEFAULT, 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: def setup(client: PycordBot) -> None: