Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

python - Issues with getting VoiceChannel.members and Guild.members to return a full list

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()

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)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...