You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python开发Discord机器人on_message异常导致指令无响应求助

问题原因

你的代码触发异常的核心原因是on_message事件中写错了变量名:

# 错误写法
if input.content.startswith('.ltt inspire'):

input是Python的内置控制台输入函数,本身没有content属性,调用input.content会直接抛出属性不存在的异常,导致整个on_message事件中断,所有指令都无法正常响应。

修复方案

把上述代码里的input替换为事件传入的message对象即可,修复后对应代码段如下:

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('.ltt help'):
        await message.channel.send('hello')
    
    # 此处已修改input为message
    if message.content.startswith('.ltt inspire'):
      quote = get_quote()
      await message.channel.send(quote)

补充排查建议

后续开发时可以在事件函数内增加异常捕获逻辑,打印完整错误信息,能大幅提升定位问题的效率:

@client.event
async def on_message(message):
    try:
        # 原有逻辑写在这里
    except Exception as e:
        print(f"触发异常:{repr(e)}")

内容的提问来源于stack exchange,提问作者Elytra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 20:15:05