Rafael Pereira
Rafael Pereira

Reputation: 57

Handle error when passing a wrong type argument to a command

I'm want to treat the case when someone execute the "c" command on discord and passes a non-existent role as argument. I get the following error log:

@bot.command()
# Send DM to people that have the role
async def c(ctx, role: discord.Role, *, dm):
    try:
        for members in role.members:
            await members.create_dm.send(f'Massage from {ctx.author.display.name}: {dm}')
            print('Message sent to a member')
    except RoleNotFound:
        print('Role not found')
Ignoring exception in command c:
Traceback (most recent call last):
  File "C:\Users\Rafael\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Rafael\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke
    await self.prepare(ctx)
  File "C:\Users\Rafael\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 789, in prepare
    await self._parse_arguments(ctx)
  File "C:\Users\Rafael\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 697, in _parse_arguments
    transformed = await self.transform(ctx, param)
  File "C:\Users\Rafael\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 552, in transform
    return await self.do_conversion(ctx, converter, argument, param)
  File "C:\Users\Rafael\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 505, in do_conversion
    return await self._actual_conversion(ctx, converter, argument, param)
  File "C:\Users\Rafael\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 451, in _actual_conversion
    ret = await instance.convert(ctx, argument)
  File "C:\Users\Rafael\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\converter.py", line 635, in convert
    raise RoleNotFound(argument)

So, i thought treating it using try/except, but the error occours before it enters the tryblock.

PS: I don't want to prevent the error, but handle it

Upvotes: 0

Views: 450

Answers (1)

Dominik
Dominik

Reputation: 3592

You have to add an error handler for this yourself, you can do this in a different way and have to go via commands.YourError. To use it you do not need to import something else but build your own "function" and re-structure your code.

Have a look at the following code:

@bot.command()
# Send DM to people that have the role
async def c(ctx, role: discord.Role, *, dm):
    for members in role.members:
        await members.create_dm.send(f'Massage from {ctx.author.display.name}: {dm}')
        print('Message sent to a member')

@c.error
async def dm_error(ctx, error):
    if isinstance(error, commands.RoleNotFound):
        await ctx.send("Role not found")

You can also build in more errors but can't use except as you wanted to do that first because you can just use it for AttributeError or SyntaxError or something similar.

Upvotes: 1

Related Questions