首页 > 解决方案 > 获取命令名称

问题描述

我希望机器人获取命令名称,但它给出了错误。我不知道为什么它出错了。请帮我。

错误:未定义的变量“testcommand1”

@commands.command(
    name='test'
)
async def testcommand1(self, ctx):
    json = {
        '_id': ctx.message.id,
        'command': str(testcommand1.name),
        'guild_id': str(ctx.guild.id),
    }
    await ctx.send(json)

标签: pythondiscord.py

解决方案


您在 Cog 中使用命令,简单地获取命令,self.testcommand.name 或者您可以从上下文中获取命令,如下所示ctx.command

command = ctx.command
name = command.name

推荐阅读