Subcommands in Python Bot

How to create sub commands in python bot.

   @bot.group(pass_context=True)
        async def First(ctx):
            if ctx.invoked_subcommand is None:
                await bot.say('Invalid sub command passed...')

        @First.command(pass_context=True)
        async def Second(ctx):
            msg = 'Finally got success {0.author.mention}'.format(ctx.message)
            await bot.say(msg)
1

2 Answers

You need to make Second a group too.

@bot.group(pass_context=True)
async def First(ctx):
    if ctx.invoked_subcommand is None:
        await bot.say('Invalid sub command passed...')

@First.group(pass_context=True)
async def Second(ctx):
    if ctx.invoked_subcommand is Second:
        await bot.say('Invalid sub command passed...')

@Second.command(pass_context=True)
async def Third(ctx):
    msg = 'Finally got success {0.author.mention}'.format(ctx.message)
    await bot.say(msg)
5

Subcommands are commands that are part of a group. To make a group, use this: (replace client with whatever you call your client/bot)

@client.group(invoke_without_command=True)
async def parent_command(ctx):
    await ctx.send("Parent command!")

To make a command for that group, do something like this:

@parent_command.command()
async def child_command(ctx):
    await ctx.send("Child command!")

invoke_without_command=True will make it so that when you run a subcommand, the parent command will not invoke, or run (trigger if you call it that ;) )

Also you can make subgroups, just read the answer above for better explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest