打印语音频道成员列表

Print the list of members in a voice channel

我正在编写一个 discord 机器人,我需要一个可以踢掉我频道中所有成员的功能。我写了这段代码:

@client.command()
async def separaci(ctx):
    canale = ctx.message.author.voice.channel
    utenti = canale.members #This return an empty list
    for utente in utenti:
        await utente.edit(voice_channel = None)

我不知道为什么 canale.members return 一个空列表。你能帮助我吗?谢谢你:)

您必须启用成员意图,还要确保在 developer portal

中启用它们
intents = discord.Intents.default()
intents.members = True 

client = commands.Bot(command_prefix='', intents=intents)

@client.command()
async def separaci(ctx):
    channel = ctx.author.voice.channel
    members = channel.members

试试这个:

@client.command()
async def separaci(ctx):
    if ctx.author.voice: # if the author is connected to a voice channel
        canale = ctx.message.author.voice.channel
        utenti = canale.members #This return an empty list
        for utente in utenti:
            await utente.edit(voice_channel = None)
        await ctx.send("Kicked all the members from the voice channel!")
    else:
         await ctx.send("You need to be in a voice channel!")
         return

注意:

  • 使用此命令时您需要在语音频道中。
  • 确保机器人有权断开语音通道中存在的成员。
  • 确保您在 developer portal 中启用了 members 意图。