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

基于类的Discord Bot .hello命令报错:缺少ctx参数求助

问题:Discord Bot中.hello命令触发TypeError缺少ctx参数

编写了基于类的Discord Bot实现简单hello功能,代码启动正常,使用.help命令能看到hello选项,但调用.hello命令时无响应且触发TypeError,提示缺少必需的ctx参数。

代码

import discord 
from discord.ext import commands

class Bot(commands.Bot):
    async def on_ready(self):
        print(f"bot {self.user.display_name} started succesfully!!")

    @commands.command()
    async def hello(self, ctx):
        await ctx.send(f'hello {ctx.author.display_name}')

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.add_command(self.hello)

intents= discord.Intents.default()
intents.message_content= True
bot= Bot(command_prefix= '.', intents= intents)

tokn= 'MTE5MzU5NDM5ODI0NDYxODI3MA.GpvNRT.T1kNBXpP3egZ_g3a-8hOhYrmzzPIMtYGSOdPro'

bot.run(tokn) 

报错信息

Traceback (most recent call last):
  File "C:\Users\risha\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 235, in wrapped      
    ret = await coro(*args, **kwargs)
TypeError: Bot.hello() missing 1 required positional argument: 'ctx'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\risha\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\bot.py", line 1350, in invoke       
    await ctx.command.invoke(ctx)
  File "C:\Users\risha\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 1029, in invoke      
    await injected(*ctx.args, **ctx.kwargs)  # type: ignore
  File "C:\Users\risha\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 244, in wrapped      
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: Bot.hello() missing 1 required positional argument: 'ctx'

问题原因

问题出在手动调用self.add_command(self.hello)。当用@commands.command()装饰类成员方法时,装饰器会把方法包装成Command对象,但直接传递self.hello时,没有正确绑定Bot实例,导致调用时无法自动传入self(Bot实例),进而把原本应该是self的位置当成了ctx参数,最终出现缺少ctx的错误。

解决方案

方案1:移除手动添加命令的代码(推荐)

@commands.command()装饰器会自动将方法注册为Bot的命令,无需手动调用add_command。修改后的代码:

import discord 
from discord.ext import commands

class Bot(commands.Bot):
    async def on_ready(self):
        print(f"bot {self.user.display_name} started succesfully!!")

    @commands.command()
    async def hello(self, ctx):
        await ctx.send(f'hello {ctx.author.display_name}')

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 移除手动添加命令的代码

intents= discord.Intents.default()
intents.message_content= True
bot= Bot(command_prefix= '.', intents= intents)

tokn= '你的Bot令牌'
bot.run(tokn) 

方案2:手动添加时使用copy()获取绑定后的命令

如果需要保留手动添加命令的逻辑,使用self.hello.copy()创建绑定了当前Bot实例的Command副本:

def __init__(self, **kwargs):
    super().__init__(**kwargs)
    self.add_command(self.hello.copy())

内容的提问来源于stack exchange,提问作者risav68111

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 08:25:07