首页 > 解决方案 > guild.members 在 discord.py 中无法正常工作

问题描述

我正在尝试编写一个 Discord 机器人,我需要获取一个公会所有成员的列表。我为此使用了 guild.members,但这会返回一个仅包含机器人本身的列表。

这是我进行的方式:

class MyClient(discord.client):

    async def on_ready(self):
        print("The bot is ready!")

    async def on_message(self, message):

        if message.content.startswith(<name of the command>):
            <code of the command>

        if message.content.startswith(<name of the command>):
            <code of the command>

    async def <event>(self, <parameters>):
        <code>

client = MyClient()
client.run("<token>")

有谁知道为什么会这样,以及如何解决?

标签: discord.py

解决方案


这可能是因为您没有启用intents。默认情况下,Discord 会限制您获取某些信息。要更改它:

  1. 前往Discord 开发者门户,选择您的应用,前往“Bot”
  2. “Privileged Gateway Intents”下,您将看到“Server Members Intent” ——启用它。
  3. 然后在您的代码中添加以下内容:
intents = discord.Intents.default()
intents.members = True

client = commands.Bot(command_prefix="!", intents=intents) #add intents=intents to your client

现在您应该也可以与其他成员一起获得列表。

编辑:

改变:

client = MyClient()

至:

client = MyClient(command_prefix="!", intents=intents)

推荐阅读