簡體   English   中英

如何將錯誤添加到特定命令 discord.py

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

我希望每個命令都有錯誤,而不是使用不能具體的錯誤事件並解決可能需要向用戶提供額外幫助的命令。 有沒有辦法可以將錯誤添加到命令中?


@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)


你不能那樣做,你必須使用錯誤處理程序

@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