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...
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)
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)
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.