如何使用discord.py获取语音频道的所有成员

时间:2021-04-28 08:03:17

标签: python discord.py

我有问题,如果我将所有成员作为特定语音频道的成员的对象。我只得到一个空列表。

这是我的代码:

@bot.command(name='random')
async def nine_nine(ctx, amount_of_groups: int):
    this_guild = ctx.guild
    sender = ctx.message.author
    #Es wird geschaut, in welchem Channel sich der Schreiber befindet
    try:
        voice_channel = ctx.message.author.voice.channel
        print(voice_channel)
    except AttributeError:
        await ctx.send("Du bist in keinem Voice-Channel")
        return
    #Alle anderen Mitglieder aus dem Kanal werden ermittelt und in eine Liste geschrieben
    this_category = voice_channel.category

    member_list = voice_channel.members # <-- This only returns an empty List

    print(member_list)
    print(this_category)
    #Die Mitglieder werden gemischt
    random.shuffle(member_list)
    #Die Mitglieder werden in gleichgroße Gruppen aufgeteilt
    avg = len(member_list) / float(amount_of_groups)
    out = []
    last = 0.0

    while last < len(member_list):
        out.append(member_list[int(last):int(last + avg)])
        last += avg
    print(out)
    for group in range(0, amount_of_groups):
        new_channel = await this_guild.create_voice_channel(name=f'Gruppe{group+1}', category=this_category)
        for member in range(len(out[group])):
            try:
                await out[group][member].move_to(new_channel)
            except:
                print("F")
            print(out[group][member])

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您必须在 Intents.members 之前像这样启用 bot = ...

intents = discord.Intents.default()
intents.members = True

并更新 bot = .. 以包含参数 intents=intents

此外,您还必须在不和谐的应用程序站点中启用成员意图(go here,单击您的机器人,单击侧边栏中的 bot 并启用它们。): Enable member intents

参考文献:

discord.Intents.members

答案 1 :(得分:0)

您是否启用了意图?如果没有,请转到 discord.com/developers/dashboard,选择您的机器人并启用意图。之后在您的代码中启用意图:

intents = discord.Intents.default()
intents.Member = True
bot = commands.Bot(command_prefix="yourprefix", intents=intents)