简体   繁体   中英

How to add categories to commands discord.py

I am using the discord.py rewrite and I am experimenting with the help command, Right now when I do !help it will just give me every command i want to be able to split the commands up into different categories so that when I do !help it will show the different categories of commands not just the commands Here is some code

@client.command(name='test' , brief='This is the brief description', description='This is the full description')
async def test(ctx):
    await ctx.send('test')

This is what:help looks like right now:

No Category:
    test     This is the brief description

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

I assume i am going to have to add another thing onto the @client.command() Line but i am not sure what i need to add to sort it into categories

To create categories, you'd have to use Cogs . Here's a quick example of a cog (or category):

from discord.ext import commands

bot = commands.Bot(command_prefix='!')

class My_Cog(commands.Cog, name='Your Cog Name'):
    def __init__(self, bot):
        self.bot = bot

    @commands.command(name='test' , brief='This is the brief description', description='This is the full description')
    async def test(ctx):
        await ctx.send('test')

bot.add_cog(My_Cog(bot))
bot.run(token)

In a cog, there are a few modifications compared to the usual:

  • To exploit events, you have to use the @commands.Cog.listener() decorator.
  • To create commands, you have to use the @commands.command() decorator.
  • Every bot or client reference becomes self.bot (or whatever you defined it to).

You can also separate cogs into different files, which is not the case in the example I gave. To separate it into different files, you can create a cogs folder with all your cogs and add this function at the end of every file:

def setup(bot):
    bot.add_cog(My_Cog(bot))

Finally, you can add this to your main file:

from os import listdir

for file in listdir('cogs/'):
    if file.endswith('.py'):
        bot.load_extension(f'cogs.{file[:-3]})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM