【问题标题】:How to get the sum and the names of all the users from all voice channels Disocrd?如何从所有语音通道 Disocrd 中获取所有用户的总和和名称?
【发布时间】:2018-01-04 15:06:22
【问题描述】:

我用:

import discord

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

【问题讨论】:

标签: python discord discord.py


【解决方案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 中打开开发者模式

【讨论】:

  • ctx.message.server.channels 对我不起作用(“消息”中没有“服务器”这样的字段)
【解决方案2】:

您还可以像这样获取语音频道的所有成员(针对 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 2021-11-18
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2014-01-22
    相关资源
    最近更新 更多