我如何使用 class in discord.py 重写中的方法创建命令?

How could I create commands using methods from a class in discord.py rewrite?

如何使用 class 添加命令,如下所示:

class BotCreater:
    from discord.ext import commands

    def __init__(self, token, bot_type):
        self.token = token
        self.bot = self.commands.Bot(command_prefix="!")
        self.bot.remove_command('help')

    def command(self):
        async def command(ctx):
            await ctx.send("Hello")

    def run(self):
        self.bot.run(self.token)

所以我可以调用 bot_thing = BotCreater(token)bot_thing.command()bot_thing.run(),然后它会 运行 机器人使用该命令。

您缺少向机器人注册协程的装饰器:

def command(self):
    @self.bot.command()
    async def command(ctx):
        await ctx.send("Hello")

这真的很奇怪set-up。您可能会查看 cogs,这可能是一种更简单的方法来执行您想要的操作。