discord.py获得在线会员

发布于 2025-02-11 21:05:01 字数 320 浏览 2 评论 0原文

我正在尝试通过此代码获得所有在线会员(带有机器人):

@client.command() async def printstats(ctx):
    #define the variables
    oc = 0
    for user in ctx.guild.members:
        if user.status != discord.Status.offline:
            oc+=1

但是,尽管我得到的一切都是1号,即使在线有200名成员。 我已经搜索了整个Internet AMD无法找到有效的解决方案。请你帮助我好吗?

I'm trying to get all online members (with Bots) via this Code:

@client.command() async def printstats(ctx):
    #define the variables
    oc = 0
    for user in ctx.guild.members:
        if user.status != discord.Status.offline:
            oc+=1

However, all as I get a result, is the number 1, even though there are 200 members online.
I already searched through the whole internet amd couldn't find a solution that worked. Could you please help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘过的浮云 2025-02-18 21:05:01

您之所以存在此问题,是因为您的机器人没有使用存在意图,因此,机器人只能将自己视为在线,而没有其他用户,因此值为1。要解决此问题,您需要:

  • 启用存在意图和服务器成员 然后,在开发人员门户中
  • ,请确保在您的代码中启用此意图:
intents = discord.Intents()
intents.presences, intents.members = True, True
bot = commands.Bot(command_prefix="?", intents=intents) # whatever your prefix is etc, or client = discord.Client(intents=intents)

You have this issue because your bot is not using the presence intent, hence the bot can only see itself as online, and no other users, hence the value of 1. To fix this, you need to:

  • Enable the presence intent and server members in the developer portal
  • Then make sure to enable this intent in your code like so:
intents = discord.Intents()
intents.presences, intents.members = True, True
bot = commands.Bot(command_prefix="?", intents=intents) # whatever your prefix is etc, or client = discord.Client(intents=intents)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文