discord.py - 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>")
有谁知道为什么会这样,以及如何解决?
解决方案
这可能是因为您没有启用intents。默认情况下,Discord 会限制您获取某些信息。要更改它:
- 前往Discord 开发者门户,选择您的应用,前往“Bot”。
- 在“Privileged Gateway Intents”下,您将看到“Server Members Intent” ——启用它。
- 然后在您的代码中添加以下内容:
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)
推荐阅读
- webpack - Workbox - 如何让 webpack 转译 service worker 文件?
- python - 为什么在 Python 2 中使用 input() 时会出现 NameError?
- sql - 只有所有值都相等时对值进行分组
- xslt - XSLT:谓词内 key() 的行为
- python - 重采样错误:ValueError:无法使用方法或限制重新索引非唯一索引
- java - 自动生成代码以动态创建 Java 对象(反射)
- python - SQLite3 在第二个查询中绑定的数量不正确
- python-3.x - 是否有一个名为 document_base 的包?
- python - Pip3 和 python 不同步 | ModuleNotFoundError:没有名为“numpy”的模块`
- python - 嵌套python类(或django外键)的正确方法。*概念/工程问题*