簡體   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