简体   繁体   English

(discord.py) 获取特定语音频道中所有成员的列表

[英](discord.py) Getting a list of all of the members in a specific voice channel

So I'm attempting to write a raiding bot for my raiding discord using the discord.py library in python.所以我正在尝试使用 python 中的 discord.py 库为我的突袭不和谐编写一个突袭机器人。 This scripts is supposed to be forming a list of members in the voice channel for raiding.这个脚本应该在语音通道中形成一个成员列表以进行突袭。 For some reason, this script is not working.出于某种原因,此脚本不起作用。 Whenever memids is printed, it just prints an empty list.每当打印 memids 时,它只会打印一个空列表。

If anyone is familiar with discord.py and could tell me why this isn't working, please do.如果有人熟悉 discord.py 并且可以告诉我为什么这不起作用,请这样做。 It's really troubling me and I have tried everything in my knowledge to fix it.这真的让我很困扰,我已经尝试了我所知道的一切来解决它。

#find raiding
        voice_channel = discord.utils.get(ctx.message.server.channels, id = '440014722587426816')

        #finds the members
        members = voice_channel.voice_members

        memids = []

        for member in members:
            memids.append(member.id)

        print(memids)

If you know the id of the channel you can do it this way.如果您知道频道的 id,您可以这样做。 Works for me :D对我有用:D

channel = client.get_channel(1234567890) #gets the channel you want to get the list from

members = channel.members #finds members connected to the channel

memids = [] #(list)
for member in members:
    memids.append(member.id)

print(memids) #print info

I faced the same problem.我遇到了同样的问题。 voice_channel.members would return either empty or incomplete lists some times. voice_channel.members会返回空或不完整的列表。

The docs say:文档说:

voice_states Returns a mapping of member IDs who have voice states in this channel. voice_states返回在此频道中具有语音状态的成员 ID 的映射。 Note: This function is intentionally low level to replace members when the member cache is unavailable.注意:这个功能是故意低级的,当成员缓存不可用时替换members https://discordpy.readthedocs.io/en/latest/api.html#voicechannel https://discordpy.readthedocs.io/en/latest/api.html#voicechannel

I guess the members can't be trusted to return accurate connected member list consistently.我想members不能被信任一致地返回准确的连接成员列表。

I solved this problem with the following code:我用以下代码解决了这个问题:

member_ids = voice_channel.voice_states.keys()

There isn't much to go on from your question.你的问题没有什么可谈的。 I believe your problem is that the id that you provided to utils.get(...) isn't the correct id of the voice channel.我相信你的问题是, id ,你提供给utils.get(...)不是语音信道的正确的ID。 This is probably the reason to why you're always getting an empty list.这可能就是为什么你总是得到一个空列表的原因。

voice_members

A list of Members that are currently inside this voice channel.当前在此语音频道内的Members列表。 If type is not ChannelType.voice then this is always an empty array .如果type不是ChannelType.voice那么这总是一个空数组

If you're not fully sure about the voice channel's actual id , I suggest you search by the name and type ( discord.ChannelType.voice ):如果您不完全确定语音频道的实际id ,我建议您按名称和类型( discord.ChannelType.voice )进行搜索:

voice_channel = discord.utils.get(ctx.message.server.channels, name="channelname", type=discord.ChannelType.voice)

You need to enable "SERVER MEMBERS INTENT: If your bot tracks server members or downloads the entire member list, you may need the server members intent to receive member events and the member list."您需要启用“服务器成员意图:如果您的机器人跟踪服务器成员或下载整个成员列表,您可能需要服务器成员意图接收成员事件和成员列表。” on the discord developer page on the bot tab.在机器人选项卡上的不和谐开发者页面上。

I you know the channel id, I suggest using我知道频道 ID,我建议使用

voice_channel = client.get_channel(channel_id)

instead ( documentation here ).相反( 此处的文档)。 If you're using discord.py-rewrite , you can also use:如果您使用的是discord.py-rewrite ,您还可以使用:

voice_client = ctx.guild.get_channel(channel_id)

if the channel you're looking for is in the context guild ( documentation here ).如果您要查找的频道位于上下文公会中( 此处为文档)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM