无法遍历服务器不和谐 API 的成员

时间:2021-01-07 19:13:22

标签: api loops discord.py

import discord
import asyncio
from discord.ext import commands

client = commands.Bot(command_prefix=':')
token = ''

@client.event
async def on_ready():
    print('BOT ONLINE')

@client.event
async def on_message(message):
    channel = message.channel
    if message.content.startswith('/'):
        if message.content.startswith("/users"):
            # FOR LOOP IN QUESTION ---------------
            for guild in client.guilds:
                for member in guild.members:
                    print(member)  # or do whatever you wish with the member detail

client.run(token)


print("Bot Finished")

当我运行此代码时,它返回的只是机器人名称两次。服务器有两个成员,我和机器人。我需要遍历服务器的每个成员。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您只是没有启用 intents.members

intents = discord.Intents.default()
intents.members = True

client = commands.Bot(command_prefix=":", intents=intents)

还要确保在 developer portal

中启用它们

参考: