【问题标题】:Print error code doesn't work (PyCharm, Python 3.8)打印错误代码不起作用(PyCharm,Python 3.8)
【发布时间】:2021-04-20 07:21:55
【问题描述】:

我在 Stackoverflow 中找到了这段代码,它会打印任何错误消息。但它不会打印错误。只是说是否相关,PyCharm 说 Bot 没有属性 AppInfo。

代码:

@bot.event
async def on_error(event, *args, **kwargs):
    embed = discord.Embed(title=':x: Event Error', colour=0xe74c3c)
    embed.add_field(name='Event', value=event)
    embed.description = '```py\n%s\n```' % traceback.format_exc()
    embed.timestamp = datetime.datetime.utcnow()
    await bot.AppInfo.owner.send(embed=embed)

【问题讨论】:

    标签: discord.py python-3.8 args keyword-argument


    【解决方案1】:

    bot 类默认没有AppInfo 属性。要创建它,请在您的 on_ready() 事件中包含该属性:

    @bot.event
    async def on_ready():
        if not hasattr(bot, 'AppInfo'):
            bot.AppInfo = await bot.application_info()
    

    这样做,它在您的on_error 事件中使用时将可用。

    【讨论】:

    • 但它仍然不打印发送错误。我尝试做一个简单的错误,其中命令不在代码中,但机器人不发送消息。
    • 您实际上是如何尝试触发错误的?
    • 然后出现错误: is not found
    • 尝试在您的on_error 事件中等待await bot.application_info()
    【解决方案2】:

    我发现了另一个 Stackoverflow 问题 (Discord.py-Rewrite Sending an Error message when there is an unknown command or other error) 有答案。看起来,没有必要像我的代码那样使命令复杂化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2015-09-05
      • 2022-01-12
      • 2015-07-16
      相关资源
      最近更新 更多