nextcord.py工单系统运行setup命令出现Channel "None" not found报错求解
问题根源
- 参数顺序错误:Cog类下被
commands.command()装饰的异步函数,固定参数顺序为第一个参数是类实例self,第二个参数是nextcord传入的命令上下文Context,你额外插入的client参数属于错位参数,导致框架无法正确解析后续定义的message、category参数,分类转换器找不到合法输入就抛出ChannelNotFound。另外Cog实例本身可在初始化时绑定client,直接通过self.client访问即可,不需要把client作为命令参数传入。 - 参数转换逻辑疏漏:
nextcord.CategoryChannel类型转换器仅能识别服务器内存在的分类提及(如@工单分类)、分类ID纯数字输入,如果你输入的ID不存在、不属于当前服务器,都会抛出转换失败的报错。
解决方案
修正后的代码如下:
@commands.command() async def setup(self, ctx: commands.Context, message: nextcord.Message = None, category: nextcord.CategoryChannel = None): # 参数为空时给用户明确的用法提示 if message is None or category is None: await ctx.send("请使用正确格式:`!setup <工单消息ID> <工单分类ID/提及分类>`") return # 从Cog实例绑定的client访问配置字典 self.client.ticket_configs[ctx.guild.id] = [message.id, message.channel.id, category.id] async with aiofiles.open("ticket_configs.txt", mode="r") as file: data = await file.readlines() async with aiofiles.open("ticket_configs.txt", mode="w") as file: await file.write(f"{ctx.guild.id} {message.id} {message.channel.id} {category.id}\n") for line in data: if int(line.split(" ")[0]) != ctx.guild.id: await file.write(line) await message.add_reaction(u"\U0001F3AB") await ctx.send("Ticket system successfully configured!") # 可选:添加命令错误处理,避免转换报错直接抛到终端 @setup.error async def setup_error(self, ctx: commands.Context, error: commands.CommandError): if isinstance(error, commands.ChannelNotFound): await ctx.send("错误:你输入的分类不存在,请检查ID/提及是否正确") elif isinstance(error, commands.MessageNotFound): await ctx.send("错误:你输入的消息ID不存在,请检查是否输入正确") else: # 其他错误打印到终端方便排查 raise error
使用说明
运行命令时请按格式输入,例如你的命令前缀是!,工单触发消息ID是123456,工单分类ID是789012,就输入!setup 123456 789012,也可以输入!setup 123456 @工单分类完成配置。
内容的提问来源于stack exchange,提问作者binds
相关产品推荐
相关产品推荐

