Python开发Discord bot时shutdown命令提示缺失必选参数问题求助
问题根源
你的Cog命令函数参数顺序写错了,且额外传入了不必要的bot参数:
- 所有Cog内的命令方法,第一个固定参数是
self,第二个固定是上下文ctx,不需要手动加bot参数 - 类初始化时已经把bot实例存入了
self.bot属性,直接调用这个属性即可 - 你当前写的参数是
self, bot, ctx,框架会把bot识别为需要用户输入的第一个参数,ctx识别为第二个需要输入的参数,你调用命令时没有传任何参数,就会触发ctx缺失的报错
修正后的代码
from discord.ext import commands class AdminCommands(commands.Cog): def __init__(self, bot): self.bot = bot @commands.Cog.listener() async def on_ready(self): print("kill loaded!") @commands.command(name="shutdown", help= "For owner only! Shuts down the bot.") @commands.is_owner() async def kill(self, ctx): await ctx.send("Shutting down now, see you later!") await self.bot.close() def setup(bot): bot.add_cog(AdminCommands(bot))
额外说明
你提到ping命令结构一致还能运行,大概率是ping命令的参数只写了self, ctx,和shutdown的参数结构并不一致,可自行核对。
内容的提问来源于stack exchange,提问作者BloomNobly
相关产品推荐
相关产品推荐

