如何让 discord.py 向聊天发送错误消息
How do i make discord.py send an error message into chat
好的,这是我的代码:
@bot.command(name="randint")
async def random_num(ctx, min_num:int, max_num:int):
result = random.randint(min_num, max_num)
embed=discord.Embed(color=0x1e9f9c)
embed.add_field(name="Random number", value="min:%d, max:%d" % (min_num, max_num), inline=False)
embed.add_field(name="Result", value=result, inline=False)
await ctx.send(embed=embed)
现在我想添加到我的代码中的是,当输入字符串而不是数字时,它会向使用命令的通道发送错误,如果没有输入任何内容,它也会执行相同的操作根本。
我到处搜索,我也试过这个:
@bot.event
async def on_command_error(error, ctx):
if isinstance(error, commands.BadArgument):
ctx.send("bruh input numbers")
if isinstance(error, commands.MissingRequiredArgument):
ctx.send("bruh you do know you have to input 2 numbers lol")
但是当我尝试这样做时,它对错误没有任何反应,甚至没有将错误输入终端。
它不起作用,因为您调换了参数的顺序。查看 on_command_error
的 API Docs,您首先需要 ctx
& ,然后 error
。因此,您的 isinstance
中的 none 将起作用(因为 ctx
永远不会出现错误)。
您不再在终端中收到任何其他错误,因为您正在覆盖默认错误处理程序,默认情况下它会打印这些错误,因此不再发生。
此外,您没有 await
ing 您的 ctx.send
s,这将导致您看不到的错误,因为您忽略了该类型的错误。建议 raise
所有你没有再次发现的错误,这样你仍然可以看到它们。
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.BadArgument):
await ctx.send("bruh input numbers")
else if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("bruh you do know you have to input 2 numbers lol")
else:
raise error
您必须等待 ctx.send
命令。
此外,我不建议对特定命令进行错误处理,因为当在 randint
命令以外的命令中触发错误时,错误消息将毫无意义。
@bot.event
async def on_command_error(error, ctx):
if isinstance(error, commands.BadArgument):
await ctx.send("bruh input numbers")
raise error
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("bruh you do know you have to input 2 numbers lol")
raise error
好的,这是我的代码:
@bot.command(name="randint")
async def random_num(ctx, min_num:int, max_num:int):
result = random.randint(min_num, max_num)
embed=discord.Embed(color=0x1e9f9c)
embed.add_field(name="Random number", value="min:%d, max:%d" % (min_num, max_num), inline=False)
embed.add_field(name="Result", value=result, inline=False)
await ctx.send(embed=embed)
现在我想添加到我的代码中的是,当输入字符串而不是数字时,它会向使用命令的通道发送错误,如果没有输入任何内容,它也会执行相同的操作根本。 我到处搜索,我也试过这个:
@bot.event
async def on_command_error(error, ctx):
if isinstance(error, commands.BadArgument):
ctx.send("bruh input numbers")
if isinstance(error, commands.MissingRequiredArgument):
ctx.send("bruh you do know you have to input 2 numbers lol")
但是当我尝试这样做时,它对错误没有任何反应,甚至没有将错误输入终端。
它不起作用,因为您调换了参数的顺序。查看 on_command_error
的 API Docs,您首先需要 ctx
& ,然后 error
。因此,您的 isinstance
中的 none 将起作用(因为 ctx
永远不会出现错误)。
您不再在终端中收到任何其他错误,因为您正在覆盖默认错误处理程序,默认情况下它会打印这些错误,因此不再发生。
此外,您没有 await
ing 您的 ctx.send
s,这将导致您看不到的错误,因为您忽略了该类型的错误。建议 raise
所有你没有再次发现的错误,这样你仍然可以看到它们。
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.BadArgument):
await ctx.send("bruh input numbers")
else if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("bruh you do know you have to input 2 numbers lol")
else:
raise error
您必须等待 ctx.send
命令。
此外,我不建议对特定命令进行错误处理,因为当在 randint
命令以外的命令中触发错误时,错误消息将毫无意义。
@bot.event
async def on_command_error(error, ctx):
if isinstance(error, commands.BadArgument):
await ctx.send("bruh input numbers")
raise error
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("bruh you do know you have to input 2 numbers lol")
raise error