【问题标题】:Handle error when passing a wrong type argument to a command将错误的类型参数传递给命令时处理错误
【发布时间】:2021-07-20 07:15:17
【问题描述】:

当有人在不和谐中执行“c”命令并将不存在的角色作为参数传递时,我想处理这种情况。我收到以下错误日志:

@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)

所以,我想用try/except 处理它,但是在它进入tryblock 之前就发生了错误。

PS:我不想阻止错误,但要处理它

【问题讨论】:

    标签: python error-handling discord discord.py


    【解决方案1】:

    您必须自己为此添加一个错误处理程序,您可以以不同的方式执行此操作,并且必须通过commands.YourError 进行。要使用它,您无需导入其他内容,只需构建自己的“函数”并重新构建代码。

    看看下面的代码:

    @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")
    

    您还可以构建更多错误,但不能像您想要的那样使用except,因为您可以将它用于AttributeErrorSyntaxError 或类似的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      • 2016-05-21
      • 1970-01-01
      • 1970-01-01
      • 2015-12-22
      相关资源
      最近更新 更多