如何从所有语音频道Discord中获取所有用户的总和和名称?

How to get the sum and the names of all the users from all voice channels Disocrd?

我使用:

import discord

我需要从每个语音通道获取所有用户的数量,然后获取他们的姓名(用户名)。怎么做?

您需要访问语音通道对象。我建议您使用语音频道的 ID。该命令可能如下所示:

@client.command(pass_context = True)
async def vcmembers(ctx, voice_channel_id):
    #First getting the voice channel object
    voice_channel = discord.utils.get(ctx.message.server.channels, id = voice_channel_id)
    if not voice_channel:
        return await client.say("That is not a valid voice channel.")

    members = voice_channel.voice_members
    member_names = '\n'.join([x.name for x in members])

    embed = discord.Embed(title = "{} member(s) in {}".format(len(members), voice_channel.name),
                          description = member_names,
                          color=discord.Color.blue())

    return await client.say(embed = embed)

并且会像这样工作:

其中末尾的数字是频道ID。如果您不知道如何获取频道 ID,请右键单击频道并单击复制 ID。

如果您看不到副本 ID,请在 Settings > Appearance > Developer Mode

中打开开发者模式

您还可以像这样获取语音频道的所有成员(针对 discord.py 版本 1.0.0+ 进行了更新):

@client.command(brief="returns a list of the people in the voice channels in the server",)
async def vcmembers(ctx):
    #First getting the voice channels
    voice_channel_list = ctx.guild.voice_channels

    #getting the members in the voice channel
    for voice_channels in voice_channel_list:
        #list the members if there are any in the voice channel
        if len(voice_channels.members) != 0:
            if len(voice_channels.members) == 1:
                await ctx.send("{} member in {}".format(len(voice_channels.members), voice_channels.name))
            else:
                await ctx.send("{} members in {}".format(len(voice_channels.members), voice_channels.name))
            for members in voice_channels.members:
                #if user does not have a nickname in the guild, send thier discord name. Otherwise, send thier guild nickname
                if members.nick == None:
                    await ctx.send(members.name)
                else:
                    await ctx.send(members.nick)