discord.guild.成员工作不正常



我试图制作一个discord机器人,但看起来guild.members不起作用。这是我的代码:

import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
client = discord.Client()
@client.event
async def on_ready():
for guild in client.guilds:
if guild.name == GUILD:
break
print(
f'{client.user} is connected to the following guild:n'
f'{guild.name}(id: {guild.id})n'
)
members = 'n - '.join([member.name for member in guild.members])
print(f'Guild Members:n - {members}')
client.run(TOKEN)

根据教程(https://realpython.com/how-to-make-a-discord-bot-python/#how-使开发人员门户中的不一致(,输出应该是

RealPythonTutorialBot#9643 is connected to the following guild:
RealPythonTutorialServer(id: 571759877328732195)
Guild Members:
- aronq2
- RealPythonTutorialBot

但我的输出缺少除机器人本身之外的所有成员。我确实复制粘贴了教程中的代码,但输出仍然相同。

有人知道出了什么问题吗?

提前感谢!

更新(最新代码(

import os
import discord
from dotenv import load_dotenv
from discord.ext import commands
from discord.ext.commands import Bot
intents = discord.Intents()
intents.members = True
bot = commands.Bot(command_prefix='!', intents =intents)
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
client = discord.Client()

@client.event
async def on_ready():
for guild in client.guilds:
if guild.name == GUILD:
break
print(
f'{client.user} is connected to the following guild:n'
f'{guild.name}(id: {guild.id})n'
)
members = 'n - '.join([member.name for member in guild.members])
print(f'Guild Members:n - {members}')
print(intents.members)
client.run(TOKEN)

尝试

from discord.ext import commands
from discord.ext.commands import Bot
intents = discord.Intents()
intents.members = True
bot = commands.Bot(command_prefix='!', intents =intents)

最新更新