首页 > 解决方案 > 列出 discord.py Cog 中的命令

问题描述

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

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

一个特定的齿轮将如何做到这一点?

标签: pythonpython-3.xdiscorddiscord.py

解决方案


您可以检查该命令属于哪个 cog Command.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))

僵尸软件

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')

推荐阅读