我正在为discord创建一个清晰的命令,我想让它在你放一个字符串而不是int的时候,它会在聊天中发送一条消息,“请使用数字”。
我尝试过if isinstance(amount, int):
,但似乎不起作用。
脚本V
@client.command()
async def clear(ctx, amount=1):
role = discord.utils.get(ctx.guild.roles, name="[+] Admin")
if role in ctx.author.roles:
if isinstance(amount, int):
print('int')
else:
print('str')
else:
await ctx.send(f'You lack the permissions.', delete_after=3)```
> It can identiy if it is an int value
``` Bot is ready. int```
但不是字符串。
Ignoring exception in command clear:
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 367, in _actual_conversion
return converter(argument)
ValueError: invalid literal for int() with base 10: 'Hello'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\bot.py", line 859, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 718, in invoke
await self.prepare(ctx)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 682, in prepare
await self._parse_arguments(ctx)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 596, in _parse_arguments
transformed = await self.transform(ctx, param)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 452, in transform
return await self.do_conversion(ctx, converter, argument, param)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 405, in do_conversion
return await self._actual_conversion(ctx, converter, argument, param)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 376, 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 "amount".
发布于 2019-05-25 16:45:42
使用converter并编写error handler来处理无法将输入转换为int时引发的错误:
从discord.ext.commands导入BadArgument
@client.command()
async def clear(ctx, amount: int=1):
...
@clear.error
async def clear_error(ctx, error):
if isinstance(error, BadArgument):
await ctx.send("Invalid integer argument.")
else:
raise error
https://stackoverflow.com/questions/56304548
复制相似问题