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

2022-01-15 00:00:00 python discord.py

问题描述

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

I have the problem, if I will get all members as an object of Meber of a specific Voice-channel. I only get a empty list back.

这是我的代码:

@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])

我该如何解决这个问题?

How can I fix this Problem?


解决方案

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

You have to enable Intents.members like this before bot = ... :

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

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

and update the bot = .. to include the argument intents=intents.

您还必须在您的应用程序站点中启用不和谐的成员意图(

Also you have to enable member intents in your application site in discord (go here, click on your bot, click on bot at the sidebar and enable them.):

参考资料:

discord.Intents.members

相关文章