让 VoiceChannel.members 和 Guild.members 返回完整列表的问题

问题描述

每当我尝试使用 VoiceChannel.members 或 Guild.members 时,它都不会为我提供适用成员的完整列表.我在这样的文本命令中从上下文中获取 VoiceChannel 和 Guild:

Any time I try to use VoiceChannel.members or Guild.members it does not give me a full list of applicable members. I'm grabbing both the VoiceChannel and the Guild from the context in a text command like this:

@bot.command(name='followme')
async def follow_me(ctx):
    if ctx.author.voice != None:
        guild = ctx.guild
        tracking = ctx.author
        channel = tracking.voice.channel

后来我尝试使用这样的频道:

Later on I've tried to use the channel like this:

for member in channel.members:
            if member.voice.mute != True:
                await member.edit(mute=True)

但是,尽管频道中有其他用户,但它只能找到我的用户.

However, it's only finding my user despite having another user in the channel.

我发现在频道中获得准确代表的唯一方法是使用:

I found that the only way I could get an accurate representation of members in the channel is using:

channel.voice_states.keys()

使用 voice_states,我可以获得准确的成员列表,但只有在我仍然需要操作成员本身时才能通过他们的 ID.所以我尝试了这个:

Using voice_states, I can get an accurate list of members, but only by their IDs when I still need to manipulate the member itself. So I tried this:

for key in channel.voice_states.keys():
            member = guild.get_member(key)
            if member.voice.mute != True:
                await member.edit(mute=True)

然而,公会并没有拉出正确的用户组,尽管验证了所有 ID 都是正确的,但 guild.members 也无法正常工作.

However, the guild isn't pulling the right set of users and despite verifying all the IDs were correct, guild.members is also not working appropriately.

任何关于如何使其正常工作的意见将不胜感激.

Any input on how to get this working properly would be greatly appreciated.


解决方案

截至 10 月 7 日,Discord 已更改其 API,要求机器人声明 网关意图.确保您的 discord.py 至少更新到 1.5 版并启用成员意图:

As of October 7th, Discord has changed their API to require bots to declare gateway intents. Make sure your discord.py is updated to at least version 1.5 and enable the members intent:

import discord
from discord.ext import commands

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

bot = commands.Bot(command_prefix='!', intents=intents)

相关文章