Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python (discord.py)获取特定语音频道中所有成员的列表_Python_Python 3.x_Discord_Discord.py - Fatal编程技术网

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

Python (discord.py)获取特定语音频道中所有成员的列表,python,python-3.x,discord,discord.py,Python,Python 3.x,Discord,Discord.py,因此,我正试图使用python中的discord.py库为我的raid discord编写一个raid bot。这个脚本应该在语音频道中形成一个成员列表,以便进行突袭。由于某些原因,此脚本无法工作。每当打印memid时,它只打印一个空列表 如果有人熟悉discord.py并能告诉我为什么它不起作用,请告诉我。这真的让我很烦恼,我已经尽我所能去解决它了 #find raiding voice_channel = discord.utils.get(ctx.message.serve

因此,我正试图使用python中的discord.py库为我的raid discord编写一个raid bot。这个脚本应该在语音频道中形成一个成员列表,以便进行突袭。由于某些原因,此脚本无法工作。每当打印memid时,它只打印一个空列表

如果有人熟悉discord.py并能告诉我为什么它不起作用,请告诉我。这真的让我很烦恼,我已经尽我所能去解决它了

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

你的问题没有什么可谈的。我相信您的问题在于您提供给
utils.get(…)
id
不是正确的语音频道id。这可能就是为什么你总是得到一个空列表的原因

当前在此语音中的
成员的列表
频道如果
type
不是
ChannelType.voice
,则始终为空 数组

如果您不能完全确定语音频道的实际
id
,我建议您按名称和类型(
discord.ChannelType.voice
)进行搜索:


如果您知道频道id,我建议使用

voice_channel = client.get_channel(channel_id)
而是()。如果您正在使用,还可以使用:

voice_client = ctx.guild.get_channel(channel_id)

如果您要查找的频道在上下文公会()中。

如果您知道频道的id,您可以这样做。为我工作: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

我也面临同样的问题<代码>语音频道。成员
有时会返回空列表或不完整列表

文件说:

voice\u状态
返回在此频道中具有语音状态的成员ID的映射。 注意:当成员缓存不可用时,此功能有意降低级别以替换成员。

我想不能信任
成员
一致地返回准确的已连接成员列表

我用以下代码解决了这个问题:

member_ids = voice_channel.voice_states.keys()
您需要启用“服务器成员意图:
如果您的bot跟踪服务器成员或下载整个成员列表,您可能需要服务器成员来接收成员事件和成员列表。”在bot选项卡上的discord developer页面上。

我将尝试以这种方式搜索频道,然后返回给您。这很奇怪,因为我上面发布的代码运行了几次命令,但过了一段时间就坏了,从此就无法正常工作。如果你认为有帮助的话,我可以用剩下的代码编辑原始帖子。对不起,我应该在哪里尝试这些代码?我在@client.event\n async def on_ready()中编写了它。但是没有在语音频道中显示任何人我很确定它应该在\u ready()上的
下工作,但是如果你把它放在\u ready()
上的
下,用户必须在启动机器人之前进入语音频道。您可以将其设置为循环,将其转换为命令,或使用另一个事件引用将其放入代码中。现在,意图是在Discord API中实现的,因此,如果有时无法看到成员,您必须注意这一点。资料来源:
member_ids = voice_channel.voice_states.keys()