【问题标题】:Why error handling doesn't work in repl but in VSCode it does? discord.py为什么错误处理在 repl 中不起作用,但在 VSCode 中起作用?不和谐.py
【发布时间】:2021-10-31 09:03:23
【问题描述】:

我正在尝试处理我的 discord 机器人的本地命令的错误,我收到以下错误 discord.ext.commands.errors.CommandInvokeError: Command raise an exception: AttributeError: 'Command' object has no attribute' MissingRequiredArgument' 这只发生在我在 repl 而不是在 VSCode 中执行代码时。我还尝试了以下

commands.errors.MissingRequiredArgument
@youtube.error
async def youtube_error(ctx,error):
    if isinstance(error, commands.MissingRequiredArgument):
        await ctx.send("Some text")
@tts.error
async def tts_error(ctx,error):
    if isinstance(error, commands.MissingRequiredArgument):
        await ctx.send("Some text")

【问题讨论】:

    标签: python exception error-handling discord.py read-eval-print-loop


    【解决方案1】:

    您应该发送完整的代码,但如果我理解得很好,问题是在您的文件中存在一个名为commandsCommand 对象,因此MissingRequiredArgument 被repl 解释为命令对象属性,即显然不存在。
    您的代码中应该有这样的内容:

    @Bot.command()
    async def commands(ctx, *args):
        ...
    

    Repl 与 VS 代码

    Repl 适合初学者/初学者,Visual Studio Code 不适合。
    VS Code 的解释器可能理解 `commands` 没有引用您的命令,因此它不会引发任何错误。

    【讨论】:

    • 我使用完整的扩展解决了:discord.ext.commands.errors.MissingRequiredArgument 感谢您的帮助
    猜你喜欢
    • 2022-01-04
    • 2020-08-17
    • 2020-03-10
    • 2019-11-04
    • 2022-01-03
    • 2021-09-27
    • 1970-01-01
    • 2021-01-06
    • 2020-07-02
    相关资源
    最近更新 更多