使用discord.py权限模块遇TypeError:缺少coro参数求助
Discord.py 权限错误排查:TypeError: error() missing 1 required positional argument: 'coro'
问题场景
在使用discord.py实现踢人命令的权限校验时,触发missing 1 required positional argument: 'coro'错误,问题集中在@kick.error()装饰器部分。
完整代码
@client.command() @has_permissions(manage_roles=True, kick_members=True) async def kick(ctx, member : discord.Member, *, reason=None): await member.kick(reason=reason) @kick.error() async def kick_error(error, ctx, ): if isinstance(error, MissingPermissions): text = "Sorry {}, you do not have permissions to do that!".format(ctx.message.author) await client.send_message(ctx.message.channel, text)
报错信息
@kick.error() TypeError: error() missing 1 required positional argument: 'coro'
问题原因及修复方案
- 装饰器语法错误:
@kick.error()多了括号,discord.py的命令错误处理装饰器不需要调用括号,正确写法是@kick.error。 - 参数顺序错误:错误处理函数的参数顺序应为
(ctx, error),原代码把error放在了前面,导致参数匹配失败。 - 过时API调用:
client.send_message是discord.py v1.x之前的写法,新版应使用ctx.send()简化调用。
修正后的完整代码
@client.command() @has_permissions(manage_roles=True, kick_members=True) async def kick(ctx, member : discord.Member, *, reason=None): await member.kick(reason=reason) @kick.error async def kick_error(ctx, error): if isinstance(error, MissingPermissions): text = f"Sorry {ctx.message.author}, you do not have permissions to do that!" await ctx.send(text)
内容的提问来源于stack exchange,提问作者Github.io.user
相关产品推荐
相关产品推荐

