繁体   English   中英

如何将类别添加到命令 discord.py

[英]How to add categories to commands discord.py

我正在使用 discord.py 重写,我正在尝试使用帮助命令,现在当我执行 !help 时,它只会给我每个命令,我希望能够将命令分成不同的类别,这样当我这样做时 !help 它将显示不同类别的命令而不仅仅是命令这是一些代码

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

这就是:help 现在的样子:

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.

我假设我将不得不在 @client.command() 行中添加另一件事,但我不确定我需要添加什么才能将其分类

要创建类别,您必须使用Cogs 这是一个 cog(或类别)的简单示例:

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)

在齿轮中,与通常相比有一些修改:

  • 要利用事件,您必须使用@commands.Cog.listener()装饰器。
  • 要创建命令,您必须使用@commands.command()装饰器。
  • 每个botclient引用都成为self.bot (或您定义的任何内容)。

您还可以将 cogs 分成不同的文件,这在我给出的示例中不是这种情况。 要将其分成不同的文件,您可以创建一个包含所有 cogs 的cogs文件夹,并在每个文件的末尾添加此 function:

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

最后,您可以将其添加到您的主文件中:

from os import listdir

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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