Try/Except 在 discord.py 中的处理不起作用
Try/Except handling in discord.py doesn't work
因此,我决定发送多个嵌入,以防我的号码生成器出现错误。它工作正常,但我是否设置 try/except 块并不重要。该错误仅打印在控制台中
@bot.command()
async def random(ctx, number1, number2):
num1 = int(number1)
num2 = int(number2)
random_num=randint(num1, num2)
randsuc=embed(
title = "Random bumber",
description = f"{random_num}",
color=discord.Colour.blue())
randsuc.set_footer(text = f'LelushBot | {datetime.utcnow().strftime("%d.%m.%Y | %H:%M:%S")}')
randfail1=embed(
title="Random number",
description=f"An error has occured! {num1} и {num2} are not the numbers",
color=discord.Colour.red())
randfail1.set_footer(text = f'LelushBot | {datetime.utcnow().strftime("%d.%m.%Y | %H:%M:%S")}')
randfail2=embed(
title="Random number",
description=f"An error has occured! Insufficient arguments",
color=discord.Colour.red())
randfail2.set_footer(text = f'LelushBot | {datetime.utcnow().strftime("%d.%m.%Y | %H:%M:%S")}')
try:
await ctx.message.reply(embed=randsuc)
except ValueError:
await ctx.message.reply(embed=randfail1)
else:
await ctx.message.reply(embed=randfail2)
遇到这种情况我该怎么办?提前致谢
您正在尝试执行 num1 = int(number1)
,如果未提供格式正确的输入,可能会导致错误。相反,您应该使用 converters.
@client.command()
async def random_number(ctx, num1: int, num2: int):
random_num = random.randint(num1, num2)
rand_embed = discord.Embed(
title="random_number",
description=f"{random_num}",
color=discord.Colour.blue()
)
rand_embed.set_footer(text='LelushBot')
rand_embed.timestamp = datetime.datetime.utcfromtimestamp(time.time()) # sidenote: this shows the timestamp properly and can be viewed by other people in different timezones
await ctx.send(embed=rand_embed)
然后您可以使用类似这样的方法进行错误处理:
async def on_command_error(ctx, err):
if err.__class__ is commands.MissingRequiredArgument:
await ctx.send(f'Error: {err}')
结果:
因此,我决定发送多个嵌入,以防我的号码生成器出现错误。它工作正常,但我是否设置 try/except 块并不重要。该错误仅打印在控制台中
@bot.command()
async def random(ctx, number1, number2):
num1 = int(number1)
num2 = int(number2)
random_num=randint(num1, num2)
randsuc=embed(
title = "Random bumber",
description = f"{random_num}",
color=discord.Colour.blue())
randsuc.set_footer(text = f'LelushBot | {datetime.utcnow().strftime("%d.%m.%Y | %H:%M:%S")}')
randfail1=embed(
title="Random number",
description=f"An error has occured! {num1} и {num2} are not the numbers",
color=discord.Colour.red())
randfail1.set_footer(text = f'LelushBot | {datetime.utcnow().strftime("%d.%m.%Y | %H:%M:%S")}')
randfail2=embed(
title="Random number",
description=f"An error has occured! Insufficient arguments",
color=discord.Colour.red())
randfail2.set_footer(text = f'LelushBot | {datetime.utcnow().strftime("%d.%m.%Y | %H:%M:%S")}')
try:
await ctx.message.reply(embed=randsuc)
except ValueError:
await ctx.message.reply(embed=randfail1)
else:
await ctx.message.reply(embed=randfail2)
遇到这种情况我该怎么办?提前致谢
您正在尝试执行 num1 = int(number1)
,如果未提供格式正确的输入,可能会导致错误。相反,您应该使用 converters.
@client.command()
async def random_number(ctx, num1: int, num2: int):
random_num = random.randint(num1, num2)
rand_embed = discord.Embed(
title="random_number",
description=f"{random_num}",
color=discord.Colour.blue()
)
rand_embed.set_footer(text='LelushBot')
rand_embed.timestamp = datetime.datetime.utcfromtimestamp(time.time()) # sidenote: this shows the timestamp properly and can be viewed by other people in different timezones
await ctx.send(embed=rand_embed)
然后您可以使用类似这样的方法进行错误处理:
async def on_command_error(ctx, err):
if err.__class__ is commands.MissingRequiredArgument:
await ctx.send(f'Error: {err}')
结果: