首页 > 解决方案 > 运行“commands.group()”的 discord.py 在针对第二个命令时发送帮助命令

问题描述

很难得到完美的标题,但差不多。得到它!!changelog是主要命令,这将发送所有可用命令的列表,但我得到的问题是。每当我运行!!changelog message <text>命令时,都会显示预设的“帮助”消息。

图片在这里: 在此处输入图像描述

每当发送消息时,我只希望它说“您的消息已发送”之类的内容

这是我的代码:

@commands.group(invoke_without_command=True)
async def changelog(self, ctx):
    await ctx.send('Available Setup Commands: \nSet Channel: <#channel>\nChangelog Message: <message>')

@changelog.command()
async def channel(self, ctx, channel: discord.TextChannel):
    if ctx.message.author.guild_permissions.administrator:
        db = sqlite3.connect('main.sqlite')
        cursor = db.cursor()
        cursor.execute(
            f'SELECT channel_id FROM main WHERE guild_id = {ctx.guild.id}')
        result = cursor.fetchone()
        if result is None:
            sql = ('INSERT INTO main(guild_id, channel_id) VALUES(?,?)')
            val = (ctx.guild.id, channel.id)
            await ctx.send(f'Channel has been set to {channel.mention}')
        elif result is not None:
            sql = ('UPDATE main SET channel_id = ? WHERE guild_id = ?')
            val = (channel.id, ctx.guild.id)
            await ctx.send(f'Channel has been updated to {channel.mention}')
        cursor.execute(sql, val)
        db.commit()
        cursor.close()
        db.close()

@changelog.command()
async def message(self, ctx, *, text):
    if ctx.message.author.guild_permissions.manage_messages:
        est = timezone('EST')
        db = sqlite3.connect('main.sqlite')
        cursor = db.cursor()
        cursor.execute(f'SELECT channel_id FROM main WHERE 1')
        channel = cursor.fetchone()
        channel_id = self.client.get_channel(int(channel[0]))
        message = text.capitalize()
        embed = discord.Embed(
            title="Changelog", description=f"● {message}", color=0)
        embed.set_footer(
            text=f'Published: {datetime.now(est).strftime("%Y-%m-%d %H:%M:%S")}')
        await channel_id.send(embed=embed)
        cursor.close()
        db.close()

标签: discord.pydiscord.py-rewrite

解决方案


你可以ctx.invoked_subcommand用来检查你是否在你的组中运行一个子命令:

async def changelog(self, ctx):
    if ctx.invoked_subcommand is None:
        await ctx.send('Available Setup Commands: \nSet Channel <#channel>\nChangelog Message: <message>')

推荐阅读