子命令问题:ctx.invoked_subcommands 始终为 None

编辑:已找到解决方案

尝试将子命令的名称更改为_enabled_disabled并传递name="enabled" name="disabled"给命令装饰器。这是一个猜测,但有时 Bot 内部使用的名称在用作命令名称时无法正常工作。- 帕特里克·豪

我尝试清理一些代码并尝试将子命令实现到模块中,但我遇到了一个似乎无法解决的问题。首先,这里是代码:

@commands.group()

@commands.has_permissions(administrator=True)

async def autorole(self, ctx):

    exists = dbinteraction.dbexec("SELECT role from autorole WHERE server_id = {}".format(ctx.guild.id))

    if ctx.invoked_subcommand is None:

        em = None

        if (exists == None):

            em = discord.Embed(title="Autorole is disabled for this guild.", color=discord.Color(0xff0000))

        else:

            em = discord.Embed(title="Autorole is enabled for this guild.", color = discord.Color(0x32ff00))

            rol = discord.utils.get(ctx.guild.roles, id=exists)

            em.add_field(name="Current role:", value=rol.mention)

        await ctx.send(embed=em)


@autorole.command()

@commands.has_permissions(administrator=True)

async def enabled(self, ctx, role: discord.Role=None):

    """Defines a role that will be applied to all new members, format: autorole (enabled/disabled) [role]"""

    exists = dbinteraction.dbexec("SELECT role from autorole WHERE server_id = {}".format(ctx.guild.id))

    print('status, role : {} {}'.format("enabled", role.id))

    try:

        if role==None:

            await ctx.send("No role provided")

        else:

            if (exists!=None):

                dbinteraction.dbexec("UPDATE autorole SET role = {}".format(role.id))

            else:

                dbinteraction.dbexec("INSERT INTO autorole VALUES({},{})".format(ctx.guild.id, role.id))

                em = discord.Embed(title="", color= discord.Color(0x32ff00))

                em.add_field(name="Autorole enabled", value="Current role: {}".format(role.mention))

                await ctx.send(embed=em)


    except (Exception) as e:

        print(e)



无论我尝试什么,以下代码始终执行默认命令(之后的if ctx.invoked_subcommand is None:),我正在使用重写


慕的地6264312
浏览 265回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python