Nextcord机器人创建语音频道报错,求参数错误解决方案及指定分类方法
解决方案
1. 修复参数传递错误
你遇到的TypeError是因为Guild.create_voice_channel()不接受直接传位置参数的user_limit,该参数需要以关键字参数形式传入。原代码直接把int(user_limit)作为第二个位置参数传递,不符合方法定义要求。
修改后的基础修复代码:
##TEST CREATE VC @bot.command(name="createvoice") async def createvoice(ctx, name = "Voice Channel", user_limit = 5): guild = ctx.guild # 简化写法,与ctx.message.author.guild效果一致 # 用关键字参数传递user_limit await guild.create_voice_channel(name, user_limit=int(user_limit))
2. 添加指定分类的功能
要实现将频道放入指定分类,需在命令参数中添加可选的CategoryChannel类型参数,创建频道时通过关键字参数传入分类信息。
完整功能代码:
##TEST CREATE VC import nextcord from nextcord.ext import commands @bot.command(name="createvoice") # 添加可选的分类参数,类型指定为nextcord.CategoryChannel async def createvoice(ctx, name = "Voice Channel", user_limit = 5, category: nextcord.CategoryChannel = None): guild = ctx.guild # 构造频道创建的关键字参数集合 channel_options = { "user_limit": int(user_limit) } if category: channel_options["category"] = category await guild.create_voice_channel(name, **channel_options)
使用示例:
- 默认创建:
.createvoice→ 创建名为"Voice Channel"、用户上限5的语音频道 - 指定名称和人数:
.createvoice testname 10→ 创建名为testname、上限10的频道 - 指定分类:
.createvoice gaming 8 游戏分类→ 创建名为gaming、上限8,且归属“游戏分类”的频道(分类名称需准确,也可传入分类ID)
内容的提问来源于stack exchange,提问作者0minutes
相关产品推荐
相关产品推荐

