python - 命令引发异常: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 个朋友的频道中尝试过也是如此。任何人都可以帮忙吗?
解决方案
看起来 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
推荐阅读
- python - win32com DispatchEx 返回无效的类字符串
- android - startIntentSenderForResult 已弃用,我想在我的片段中调用 onActivityResult 回调
- git - Git状态显示文件在本地已删除,即使它没有被删除
- javascript - 错误 [ERR_UNSUPPORTED_ESM_URL_SCHEME]:默认 ESM 加载器仅支持文件和数据 URL - Vue 3
- database - 如何获取文档 MangoDB&Mongoose 上的特定字段并聚合一些字段?
- email - 获取 Mailchimp 为单个受众管理多个注册表单
- list - 如何在方案/球拍上做一个乘法表
- c# - 我们可以在 EF Query Where 子句中使用 ValueObjects 吗?
- algorithm - 如何将此算法放入编程语言(Python)中
- java - 使用 MAVEN 在 Databricks Spark Scala AWS 中解析 XML - DailyMed 的 HL7 V3 文件