如果在表达式中添加单词,如何使“ add”命令发送错误?

时间:2019-11-04 13:37:07

标签: python bots discord.py-rewrite

这是我为该命令编写的代码:

#?dodaj
@client.command()
async def dodaj(ctx, a: int, b: int):
    if (a == int and b == int):
        await ctx.send(a + b)
    else:
        await ctx.send("Błąd : Nie mogłem obliczyc tego działania!")

我想知道这是怎么回事,因为它无法正常工作。

这是我通过输入“?dodajcośjeszcze”得到的错误:

Ignoring exception in command dodaj:
Traceback (most recent call last):
  File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\ext\commands\core.py", line 370, in _actual_conversion
    return converter(argument)
ValueError: invalid literal for int() with base 10: 'coś'

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

Traceback (most recent call last):
  File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\ext\commands\bot.py", line 863, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\ext\commands\core.py", line 721, in invoke
    await self.prepare(ctx)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\ext\commands\core.py", line 685, in prepare
    await self._parse_arguments(ctx)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\ext\commands\core.py", line 599, in _parse_arguments
    transformed = await self.transform(ctx, param)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\ext\commands\core.py", line 455, in transform
    return await self.do_conversion(ctx, converter, argument, param)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\ext\commands\core.py", line 408, in do_conversion
    return await self._actual_conversion(ctx, converter, argument, param)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\ext\commands\core.py", line 379, in _actual_conversion
    raise BadArgument('Converting to "{}" failed for parameter "{}".'.format(name, param.name)) from exc
discord.ext.commands.errors.BadArgument: Converting to "int" failed for parameter "a".

此外,当我键入“?dodaj 20 20”时,它应该加上20,但它会在代码中的else参数中发送错误消息。

1 个答案:

答案 0 :(得分:0)

a == int and b == int

这就是问题所在。 a不等于intint是一门课程。 a是类int的实例。

这意味着您需要检查a的类型,或者更好-如果aint的实例(它可以是从int继承的任何类, ):

isinstance(a, int) and isinstance(b, int)