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

Python开发Discord Bot遇'intents'参数缺失错误的修复方法

修复Discord Bot初始化的Intents报错

问题根源

Discord.py 2.0+版本强制要求初始化discord.Client()时必须传入**intents(网关意图)**参数——这是Discord API的硬性要求,用来管控Bot能接收的事件数据。你的代码没指定这个参数,所以触发了TypeError。

修复流程

1. 在Discord开发者后台开启必要意图

  • 打开你的Bot对应的Discord开发者后台页面,进入Bot标签页
  • 找到Privileged Gateway Intents区域,开启Message Content Intent(你的Bot要读取消息内容响应指令,这个必须开),其他如Server Members Intent这类如果用不到可以暂时不开启

2. 修改代码中的Client初始化逻辑

更新代码,先创建配置好的Intents对象,再传给discord.Client():

import discord
import os

# 初始化Intents,启用消息内容权限
intents = discord.Intents.default()
intents.message_content = True  # 必须开启,否则无法读取消息文本

client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

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

    if message.content.startswith('$hello'):
        await message.channel.send('Hello!')

client.run(os.getenv('DiscordBotToken'))

关键提示

  • discord.Intents.default()会启用基础的常用意图,但message_content默认是关闭状态,必须手动设为True,否则Bot拿不到消息内容,自然没法响应指令。
  • 后续如果需要处理成员变动、用户状态这类事件,要在后台和代码里同步开启对应的intents。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 22:45:38