Pycord-Development / pycord

Pycord is a modern, easy to use, feature-rich, and async ready API wrapper for Discord written in Python
https://docs.pycord.dev
MIT License
2.73k stars 459 forks source link

Custom Command Check raises unkown error (slash command) #854

Closed martinbndr closed 2 years ago

martinbndr commented 2 years ago

Summary

Custom Command Check raises unkown error that can not be ignored/should not be there (slash command)

Reproduction Steps

  1. Create a custom command check like this:

    def is_admin():
    async def predicate(ctx):
        developers = [id1,id2]
        if ctx.author.guild_permissions.administrator or ctx.author.guild_permissions.manage_guild or ctx.author.id in developers:
            await ctx.defer(ephemeral=True)
            await ctx.send_followup("You do not have administrator permissions in this server to run this command.")
            return True
        else:
            await ctx.defer(ephemeral=True)
            await ctx.send_followup("You do not have administrator permissions in this server to run this command.")
            return False
    
    return commands.check(predicate)
  2. Create the global application command error handler:
    @bot.event
    async def on_application_command_error(ctx, error):
    if isinstance(error, commands.CheckFailure):
        return
    else:
        raise error

Minimal Reproducible Code

@bot.event
async def on_application_command_error(ctx, error):
    if isinstance(error, commands.CheckFailure): # Somewhere here in the if statement
        return
    else:
        raise error

Expected Results

If the command check fails there should not be a error because its handled in the error event.

Actual Results

This the check works but this error still gets raised:

Ignoring exception in on_application_command_error
Traceback (most recent call last):
  File "C:\Users\flore\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\client.py", line 352, in _run_event
    await coro(*args, **kwargs)
  File "c:\Users\flore\Documents\GitHub\Kaytem-Development\run.py", line 96, in on_application_command_error
    if isinstance(error, commands.CheckFailure):
AttributeError: 'list' object has no attribute 'CheckFailure'

AttributeError: 'list' object has no attribute 'CheckFailure'

Intents

intent = discord.Intents.default() intent.members = True intent.messages = True

System Information

Checklist

Additional Context

So to say finally: Having a custom check handled by the on_application_command_error works but there is a additional error that might be in the code of the pycord libary.

Dorukyum commented 2 years ago

It seems what you defined as "commands" is a list

martinbndr commented 2 years ago

Hmm strange it also does not work when I use the errors attribute of commands.errors, so commands.errors.CheckFailure

Ignoring exception in on_application_command_error
Traceback (most recent call last):
  File "C:\Users\flore\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\client.py", line 352, in _run_event
    await coro(*args, **kwargs)
  File "c:\Users\flore\Documents\GitHub\Kaytem-Development\run.py", line 96, in on_application_command_error
    if isinstance(error, commands.errors.CheckFailure):
AttributeError: 'list' object has no attribute 'errors'

I use these Imports for discord: from discord.ext import commands, tasks from discord.commands import permissions from discord.ext.commands import BucketType

I´m a bit confused why that happens because I saw the cooldown example that shows how to handle slash command errors. That works fine but with the same method But this check failure raises that error so maybe something related to the check failure in the pycord code or a issue with my code? Confuesd because I don't know there this error could be

Makiyu-py commented 2 years ago

cannot reproduce.

Try checking in your file if you have some sort of variable named commands that may have overwritten the module.

martinbndr commented 2 years ago

Sry I solved it by using discord.commands.errors.errors.CheckFailure directly. Idk why I forgot to test that first xD