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

Discord.py中member.status始终显示离线的问题解决咨询

Discord用户状态显示离线问题修复

问题

我编写了一个Discord用户信息命令,但所有用户(包括机器人自身)的状态都显示为离线,代码如下:

import discord
from discord import app_commands
import datetime

@bot.tree.command(name="user", description="Shows some informations about the mentioned user.")
async def user(interaction: discord.Interaction, member: discord.Member=None):
    if member is None:
        member = interaction.user
    roles = [role for role in member.roles if role.name != "@everyone"]
    embed = discord.Embed(title=f"Details about the user, {member.name}", color=0xdaddd8, timestamp=datetime.datetime.utcnow())
    embed.set_thumbnail(url=member.avatar)
    embed.add_field(name="👤 Name", value=f"{member.name}#{member.discriminator}")
    embed.add_field(name="🏷️ Nickname", value=member.display_name)
    embed.add_field(name="🆔 User ID", value=member.id)
    embed.add_field(name="📆 Created at", value=member.created_at.strftime("%D \n%I:%M %p"))
    embed.add_field(name="👋🏻 Joined at", value=member.joined_at.strftime("%D \n%I:%M %p"))
    embed.add_field(name="🟢 Status", value=member.status)  # 此部分无法正常工作
    embed.add_field(name="❤️‍🔥 Top role", value=member.top_role.mention)
    bot_status = "Yes, it is" if member.bot else "No, They aren't"
    embed.add_field(name="🤖 Bot?", value=bot_status)
    embed.set_footer(text=interaction.user.name, icon_url=interaction.user.avatar)
    await interaction.response.send_message(embed=embed)

原因与修复步骤

核心原因

要获取用户的在线状态,机器人需要启用Presence Intent和Server Members Intent,同时在Discord开发者后台开启对应权限,否则无法获取状态数据,默认返回离线。

具体修复

  1. 配置机器人Intents
    初始化机器人时,必须开启成员和状态意图:

    import discord
    from discord import commands
    
    intents = discord.Intents.default()
    # 开启成员缓存意图
    intents.members = True
    # 开启状态读取意图
    intents.presences = True
    
    # 如果使用 commands.Bot(支持前缀命令+斜杠命令)
    bot = commands.Bot(command_prefix="!", intents=intents)
    # 如果使用 discord.Bot(仅支持斜杠命令)
    bot = discord.Bot(intents=intents)
    
  2. 在Discord开发者后台开启权限
    登录Discord开发者后台,找到你的机器人应用,进入「Bot」页面,在「Privileged Gateway Intents」区域,开启Server Members Intent和Presence Intent。这一步是必须的,仅代码设置intents不会生效。

  3. 优化状态显示文本
    原代码直接输出member.status会返回枚举对象(如<Status.online: 'online'>),可以转换成更友好的显示:

    status_text = {
        discord.Status.online: "🟢 在线",
        discord.Status.idle: "🌙 空闲",
        discord.Status.dnd: "⛔ 请勿打扰",
        discord.Status.offline: "⚫ 离线",
        discord.Status.invisible: "⚫ 隐身"
    }
    embed.add_field(name="🟢 状态", value=status_text.get(member.status, "⚫ 离线"))
    
  4. 权限检查
    确保机器人在目标服务器拥有「查看成员」权限,否则无法获取用户的状态和成员数据。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 20:41:06