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

使用discord.py 2.3.2创建Discord机器人时遇AttributeError错误

解决discord.py中AttributeError: 'method' object has no attribute 'message_content'的问题

错误根源

核心问题是未正确实例化Intents对象:

intents: Intents = Intents.default  # 错误:这是类方法,不是实例

Intents.default是discord.py的类方法,必须加括号调用才能生成Intents实例。不加括号的话,变量intents存储的是方法本身而非Intents对象,自然不存在message_content属性。

修复步骤

  1. 修正代码中的Intents实例化
    把错误行改为:
intents: Intents = Intents.default()  # 正确:调用方法生成实例
intents.message_content = True  # 现在可正常设置该属性
  1. 开启Discord开发者后台对应权限
    discord.py 2.x版本要求必须手动开启Message Content Intent:
  • 打开机器人应用页面,进入「Bot」标签
  • 找到「Privileged Gateway Intents」区域
  • 开启「Message Content Intent」开关并保存

之前尝试无效的原因

  • 改成intents.messages没用:messages是旧版discord.py的属性,v2.x已改用message_content,且核心问题是未拿到Intents实例,改属性名无法解决本质问题
  • 重装库没用:错误是代码逻辑问题,与库安装状态无关

修正后的关键代码段

# Step 1: Bot Setup - activate intents
intents: Intents = Intents.default()  # 修复:添加括号
intents.message_content = True #NOQA
client: Client = Client(intents=intents)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 12:33:10