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

Discord机器人无法读取用户活动仅在线,请求技术排查协助

Discord机器人无法读取用户活动的修复方案

你的机器人只能识别在线状态、无法读取用户活动,主要是Intents权限配置不全,同时代码里的活动判断逻辑有瑕疵,下面是具体修复步骤:

1. 配置正确的Intents

Discord API要求明确启用成员和状态相关的意图,才能获取用户活动数据。修改代码开头的Intents初始化部分:

import discord

# 启用默认意图,并额外开启成员和状态权限
intents = discord.Intents.default()
intents.members = True
intents.presences = True

client = discord.Client(intents=intents)

2. 在Discord开发者平台开启特权意图

仅代码设置还不够,必须在Discord开发者后台开启对应的特权权限:

  • 打开Discord开发者平台,找到你的机器人应用
  • 进入「Bot」页面,找到「Privileged Gateway Intents」区域
  • 开启「Server Members Intent」和「Presence Intent」
  • 保存设置

3. 优化活动判断逻辑

直接将after.activity转字符串判断游戏名称容易出错,因为活动对象的字符串形式包含额外信息,建议改用activity.name:

@client.event
async def on_member_update(before, after):
    if after.activity is not None:
        if after.activity.name == 'League of Legends':
            await after.send("Chuj ci w dupe, wylacz ligusie, ten no")
        elif isinstance(after.activity, discord.Spotify):
            await after.send("Ale muzyczka gra ni")
            with open('funk.gif', 'rb') as file:
                await after.send(file=discord.File(file, 'funk.gif'))

另外代码里user = after属于冗余操作,直接使用after即可。

完成以上操作后重启机器人,就能正常读取用户的活动状态了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 14:25:19