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

如何在Discord Python API中配置Intents?代码参数缺失求助

解决Discord.py中intents参数缺失的问题

你的代码里discord.Client(intents=)需要配置对应的权限集(Intents),Discord API要求必须显式声明客户端需要的权限,否则无法正常调用相关接口。针对你给好友发消息的需求,配置方式如下:

步骤1:配置Intents对象

首先创建Intents实例,启用默认权限并补充消息内容权限(发送消息需要该权限):

intents = discord.Intents.default()
intents.message_content = True

步骤2:修改客户端初始化与事件监听

将on_connect替换为on_ready(确保客户端完全初始化后再操作好友列表),同时完善异常捕获逻辑:

完整修改后的代码

import discord

# 配置必要的Intents
intents = discord.Intents.default()
intents.message_content = True

client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print(f"已登录为 {client.user.name}")
    # 遍历好友列表发送消息
    for user in client.user.friends:
        try:
            await user.send("MESSAGE")
            print(f"已发送给: {user.name}")
        except Exception as e:
            # 打印具体错误信息便于排查
            print(f"无法发送给 {user.name}: {str(e)}")

# 替换为你的用户令牌,注意不要泄露
client.run("你的令牌", bot=False)

关键说明

  • discord.Intents.default()包含了基础的客户端权限,在此基础上开启message_content是因为发送消息需要访问消息内容的权限。
  • 使用on_ready而非on_connect:on_connect仅表示连接成功,但客户端的用户数据(如好友列表)可能未加载完成,on_ready触发时所有初始化操作已完成,能确保正确获取好友列表。
  • 避免空except:捕获具体异常并打印错误信息,可快速排查无法发送的原因(如对方屏蔽、令牌权限不足等)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 04:25:17