【问题标题】:List the Commands in a discord.py Cog列出 discord.py Cog 中的命令
【发布时间】:2021-06-29 04:00:21
【问题描述】:

使用 discord.py,您可以列出机器人的命令。这是最好的例证:

x = []
for y in client.commands:
    x.append(y.name)
print(x)

如何使用特定的 cog 做到这一点?

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    您可以使用Command.cog 检查该命令属于哪个齿轮。请注意,如果该命令不属于某个 cog,则这将是 None。

    cog.py

    from discord.ext import commands
    
    class Test(commands.Cog):
        def __init__(self, bot):
            self.bot = bot
    
        @commands.command()
        async def foo(self, ctx):
            await ctx.send('bar')
    
    def setup(bot):
        bot.add_cog(Test(bot))
    

    bot.py

    from discord.ext import commands
    
    client=commands.Bot(command_prefix='!')
    
    client.load_extension('cog')
    
    @client.command()
    async def ping(ctx):
        await ctx.send('pong')
    
    x = []
    for y in client.commands:
        if y.cog and y.cog.qualified_name == 'Test':
            x.append(y.name)
    print(x)
    
    client.run('token')
    

    【讨论】:

    • 谢谢!现在是早上 5 点,所以我会在早上试试这个。感谢您的反馈!
    • 非常感谢,我什么都试过了。不知道有一个属性叫qualified_name
    猜你喜欢
    • 2022-01-24
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    相关资源
    最近更新 更多