Discord.py等级系统报MissingRequiredArgument: ctx参数缺失问题求助
报错原因排查与解决方案
根因
你在Cog类内部定义指令时误用了@bot.command()装饰器,Cog类内的指令必须使用@commands.command()装饰器。@bot.command()是全局指令装饰器,作用在类方法上时,discord.py不会自动将类实例的self作为第一个参数注入,会出现参数错位:方法定义的第一个参数self被框架识别为需要传入的ctx参数,运行时缺少对应参数就会抛出ctx is a required argument that is missing的错误。
修复方法
将代码中level方法的装饰器替换即可:
- @bot.command(brief="Displays the user's level and experience.") + @commands.command(brief="Displays the user's level and experience.")
可选注意事项
- 确认
__init__方法前后为双下划线,避免类初始化异常:def __init__(self, client): - 确认头部已导入对应模块:
from discord.ext import commands
内容的提问来源于stack exchange,提问作者Vanya Briggs
相关产品推荐
相关产品推荐

