Makiyu
Makiyu

Reputation: 421

How to get a list of subcommands from a command discord.py

(discord.py 1.5.0) I want to get a list of subcommands from a command then display it in an embed. Here's my code:

async def cmd_help(self, ctx, command):
    embed = discord.Embed(title=f"{str(command).upper()} Help!", description=syntax(command), color = 0xf8f8ff)

    if command.parent in self.bot.get_command(command).walk_commands():
      SCmd = ""
      for subcommmand in command.parent:
        SCmd += f"`{subcommmand}` - {subcommmand.description}\n"

      embed.add_field(name='Subcommands:', value=SCmd)


    embed.add_field(name=f'Command Description:', value=command.help or command.description)

    embed.set_footer(text='<> - required | [] - optional')
    await ctx.send(embed=embed)

But it's giving me this error:

Ignoring exception in on_command_error
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 333, in _run_event
    await coro(*args, **kwargs)
  File "/home/runner/KIGM-Discord-Bot/cogs/other/cog_events.py", line 75, in on_command_error
    raise error
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "/home/runner/KIGM-Discord-Bot/cogs/comms/cog_help.py", line 151, in help
    await self.cmd_help(ctx, command)
  File "/home/runner/KIGM-Discord-Bot/cogs/comms/cog_help.py", line 26, in cmd_help
    if command.parent in self.bot.get_command(command).walk_commands():
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1216, in get_command
    if ' ' not in name:
TypeError: argument of type 'Group' is not iterable

I've searched through the docs and got no things related to getting a list of subcommands, only getting the parents of the subcommand's. Anyone wanna help?


EDIT February 2021

I have already fixed this myself by doing this:

if isinstance(command, Group):  # check if it has subcommands
    SCmd = ""  # make an empty string to add the subcommands on
    for subcommmand in command.walk_commands():  # iterate through all of the command's parents/subcommands

        if subcommmand.parents[0] == command:  # check if the latest parent of the subcommand is the command itself
            SCmd += "**•  {0.name}**\n".format(subcommmand)  # then add it in the string if it is.
        else:  # the else statement is optional.
            continue
            
    embed.add_field(name='Subcommands', value=SCmd, inline=False)

Upvotes: 3

Views: 2014

Answers (2)

Makiyu
Makiyu

Reputation: 421

As someone suggested, I should answer my question, so here it is!

if isinstance(command, commands.Group):  # check if it has subcommands
    SCmd = ""  # make an empty string to add the subcommands on
    for subcommmand in command.walk_commands():  # iterate through all of the command's parents/subcommands

        if subcommmand.parents[0] == command:  # check if the latest parent of the subcommand is the command itself
            SCmd += "**•  {0.name}**\n".format(subcommmand)  # then add it in the string if it is.
        else:  # the else statement is optional.
            continue
            
    embed.add_field(name='Subcommands', value=SCmd, inline=False)

since that's very specific, you can just convert it to a list by list comprehension:

[subcommand for subcommmand in command.walk_commands() if subcommmand.parents[0] == command]

I haven't tested that yet, so if there's a problem, just comment on this answer!

Upvotes: 3

Bluenix
Bluenix

Reputation: 429

The command parameter you have is already a commands.Group, you should change line 26 to be

if command.parent in command.walk_commands():

Though I think you have your if statement and for loop mixed up. You should look over those.

Upvotes: 0

Related Questions