首页 > 解决方案 > 命令引发异常:IndexError:无法从空序列 DISCORD.PY 中选择

问题描述

```
async def randomtokick(ctx):
    anychannel = ctx.message.author.voice.channel.id
    voice_channel = client.get_channel(anychannel)
    # Join the voice channel
    member_to_kick: discord.Member = random.choice(voice_channel.members)
    voice_client: discord.VoiceClient = await voice_channel.connect()
    await ctx.send(member_to_kick)
    await member_to_kick.edit(voice_channel = None)
```

我遇到了麻烦,因为机器人显然无法创建语音频道中的成员列表,即使我在与 5 个朋友的频道中尝试过也是如此。任何人都可以帮忙吗?

标签: pythonlistdiscord.pymember

解决方案


看起来 Intents 丢失了,因为代码对我来说很好用。

确保在Discord Developer Portal中为您的应用程序打开它们。

你可以在这里找到它们: 导航到你的应用程序 -> 机器人 -> 向下滚动 -> 打勾

要将它们实现到您的代码中,您可以使用以下内容:

intents = discord.Intents.all() # Imports all the Intents
client = commands.Bot(command_prefix="YourPrefix", intents=intents) # Or whatever you use

或者只是成员意图:

intents = discord.Intents.default() # Imports the default intents
intents.members = True
client = discord.Client(intents=intents) # Or whatever you defined/use

推荐阅读