You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 06:42:02