我在检测是否指定了输入时遇到了一些问题。它只是跳到代码的末尾并给出一个错误,跳过if
语句。
@bot.command()
async def div(ctx, left: int, right: int):
if left == "":
embedVar = discord.Embed(discription='.div', color=0xFA8072)
embedVar.add_field(name="ERROR", value='Can not divide by zero', inline=False)
return await ctx.reply(embed=embedVar)
if right == "":
embedVar = discord.Embed(discription='.div', color=0xFA8072)
embedVar.add_field(name="ERROR", value='Please specify a input', inline=False)
return await ctx.reply(embed=embedVar)
embedVar = discord.Embed(discription='.div', color=0xFA8072)
embedVar.add_field(name="Your answer is...", value=left / right, inline=False)
return await ctx.reply(embed=embedVar)
除了if
语句之外的所有内容都可以正常
对于可选参数,您必须设置一个默认值。例如:
@bot.command()
async def test(ctx, arg1: int = None, arg2: int = None):
if arg1 is None:
# handle
if arg2 is None:
# handle
# send
最好使用on_command_error事件,而不是检查是否在每个命令中都指定了参数。
您正在检查变量left
和right
是否有空字符串。然而,这些变量被定义为类型int
。尝试检查None
istead:
if left == None:
# do something
if right == None:
# do something else