简体   繁体   English

如何将错误添加到特定命令 discord.py

[英]How can I add an error to specific commands discord.py

I want to have errors for each command, instead of using an error event which can't be specific and troubleshoot a command that may need extra help given to the user.我希望每个命令都有错误,而不是使用不能具体的错误事件并解决可能需要向用户提供额外帮助的命令。 Is there a way I can put an error onto a command?有没有办法可以将错误添加到命令中?


@client.command(aliases=['translate', 'translator'])
async def trans(ctx, lang=None, *, args=None):

    t = Translator()
    a = t.translate(args, dest=lang)

    if lang == None:
        await ctx.send(f'{ctx.author.mention}, please use a Valid destination language. You can view them with ``&languages``')
        return

    if args == None:
        await ctx.send(f'{ctx.author.mention}, please provide a message in English to translate!``')
        return

    if isinstance(error, commands.CommandInvokeError):
            embed = discord.Embed(title="API Error!", description=f"{ctx.author.mention}, google translate's API is bugged, this command could fix by sending it another time", color=0xE74C3C)
            message = await ctx.message.channel.send(embed=embed, delete_after=20)


You can't do it like that, you have to use an error handler你不能那样做,你必须使用错误处理程序

@bot.command()
async def some_command_name(ctx):
    # ...


@some_command_name.error # ← name of the command + .error
async def some_command_name_error(ctx, error):
    # Handle all the errors for a command here
    if isinstance(error, commands.CommandInvokeError):
        await ctx.send("API error")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM