Как считывать недостаток прав при использовании команды? disnake.py

Рейтинг: 2Ответов: 1Опубликовано: 13.02.2023

Создаю бота в дискорд на disnake.py, пытаюсь сделать обратное сообщение при нехватке прав для использования команды

Код:

@add_video.error
async def add_video_error(ctx: disnake.CommandInteraction, error):
    author = ctx.author
    if isinstance(error, commands.MissingPermissions):
        await ctx.edit_original_response('У вас недостаточно прав')

Ранее использовал этот код для обычных команд, а на slash-команды не работает

Ошибка:

Traceback (most recent call last):
  File "C:\Kirill\Python\lib\site-packages\disnake\ext\commands\base_core.py", line 70, in wrapped
    ret = await coro(*args, **kwargs)
  File "directory", line 171, in add_video_error
    await ctx.edit_original_response('У вас недостаточно прав')
  File "C:\Kirill\Python\lib\site-packages\disnake\interactions\base.py", line 506, in edit_original_response
    raise InteractionNotResponded(self) from e
disnake.errors.InteractionNotResponded: This interaction hasn't been responded to yet

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Kirill\Python\lib\site-packages\disnake\client.py", line 700, in _run_event
    await coro(*args, **kwargs)
  File "C:\Kirill\Python\lib\site-packages\disnake\ext\commands\interaction_bot_base.py", line 1361, in on_application_command
    await self.process_application_commands(interaction)
  File "C:\Kirill\Python\lib\site-packages\disnake\ext\commands\interaction_bot_base.py", line 1358, in process_application_commands
    await app_command.dispatch_error(interaction, exc)
  File "C:\Kirill\Python\lib\site-packages\disnake\ext\commands\base_core.py", line 465, in dispatch_error
    if not await self._call_local_error_handler(inter, error):
  File "C:\Kirill\Python\lib\site-packages\disnake\ext\commands\base_core.py", line 454, in _call_local_error_handler
    return await injected(inter, error)
  File "C:\Kirill\Python\lib\site-packages\disnake\ext\commands\base_core.py", line 76, in wrapped
    raise CommandInvokeError(exc) from exc
disnake.ext.commands.errors.CommandInvokeError: Command raised an exception: InteractionNotResponded: This interaction hasn't been responded to yet

Как проверять на наличие прав использования команды?

Ответы

▲ 1Принят

InteractionNotResponded означает, что вы не отправляете первичный ответ, чтобы в дальнейшем его редактировать.

Если вы хотите просто вывести сообщение о нехватке прав (не проверяя конкретно какого права у пользователя нет), то вот вам рабочий код

@add_video.error
async def add_video_error(ctx: disnake.CommandInteraction, error):
    author = ctx.author
    if isinstance(error, commands.MissingPermissions):
        await ctx.response.send_message('У вас недостаточно прав')