Discord.py Rewrite gathering list of all commands

Viewed 11107

I'm trying to get a list of all the commands within my Discord bot in rewrite. I am using Python 3.6 to write this

I have tried to print a list of the commands by doing print(bot.commands) This only provided me with the following return:

{<discord.ext.commands.core.Command object at 0x00000209EE6AD4E0>, <discord.ext.commands.core.Command object at 0x00000209EE6AD470>}

I expect the usual output to be clear(), as that is the only command that I have programmed within the bot so far, the command works as expected. But it only prints the above

4 Answers

I think you're looking for something like this.

@bot.command(name="help", description="Returns all commands available")
async def help(ctx):
    helptext = "```"
    for command in self.bot.commands:
        helptext+=f"{command}\n"
    helptext+="```"
    await ctx.send(helptext)

Those are the Command objects that your bot has. The reason there are two is because your bot has a built-in help command that does exactly what you're looking for. If your clear command was

@bot.command()
async def clear(ctx):
    "A command for clearing stuff"
    pass

then running !help would give you the output

 No Category:
  help  Shows this message.
  clear A command for clearing stuff

Type !help command for more info on a command.
You can also type !help category for more info on a category.

Im really late but I wanted to share this.

Adding onto akiva and damaredayo's response, here is a basic command that will get all commands in cogs and main files.

@bot.command()
async def commandcount(ctx):
  counter = 0
  for command in bot.commands:
    counter += 1
  await ctx.send(f"There are `{counter}` commands!")
Related