我用:
import discord
我需要从每个语音通道中获取所有用户的数量,然后获取他们的名字(用户名)。怎么做?
答案 0 :(得分:1)
您需要访问语音通道对象。我建议您使用语音频道的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
答案 1 :(得分:0)
您还可以像这样获得语音通道的所有成员(已更新为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)