discord.py默认的错误处理

huangapple go评论49阅读模式
英文:

discord.py default errors handling

问题

discord.py 有其自己的错误处理方式,但我需要处理 Python 错误。示例:

def create(a: str, b: str, c: str):
    list = [a, b, c]
    return list

@client.command()
async def test(ctx):
    a = "First"
    b = "Second"
    list = create(a, b, c)
    await ctx.send(list)

在使用命令时,终端会显示 Command raised an exception: NameError: name 'c' is not defined,我可以使用以下方法处理它吗:

@client.event
async def on_error(ctx, error):

还是我需要在命令中使用 try except 循环?

英文:

discord.py has it's own error handling, but I need to handle Python error. Example:

def create(a: str, b: str, c: str):
    list = [a, b, c]
    return list

@client.command()
async def test(ctx):
    a = "First"
    b = "Second"
    list = create(a, b, c)
    await ctx.send(list)

on command use i get Command raised an exception: NameError: name 'c' is not defined in terminal, but can i handle it with

@client.event
async def on_error(ctx, error):

or I need to use try except cycle in command?

答案1

得分: 0

问题已关闭。对于此问题的答案:

if isinstance(error, commands.CommandInvokeError):
        if isinstance(error.original, NameError):
            embed.add_field(name="name", value="value", inline=False)
英文:

Question closed. Answer for this one:

if isinstance(error, commands.CommandInvokeError):
        if isinstance(error.original, NameError):
            embed.add_field(name = "name", value = "value", inline = False)

huangapple
  • 本文由 发表于 2023年2月10日 02:33:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75402992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定