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

Issues with getting VoiceChannel.members and Guild.members to return a full list(让 VoiceChannel.members 和 Guild.members 返回完整列表的问题)

本文介绍了让 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)

这篇关于让 VoiceChannel.members 和 Guild.members 返回完整列表的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:让 VoiceChannel.members 和 Guild.members 返回完整列表的问题

基础教程推荐