Bot和Client有什么区别?

m17*_*1vw 4 python discord discord.py

我一直在研究一些有关如何制作Discord Python Bot的示例,并且已经看到client并且bot几乎可以互换使用,而我找不到要在何时使用哪个。

例如:

client = discord.Client()
@client.event
async def on_message(message):
    # we do not want the bot to reply to itself
    if message.author == client.user:
        return

    if message.content.startswith('$guess'):
        await client.send_message(message.channel, 'Guess a number between 1 to 10')

    def guess_check(m):
        return m.content.isdigit()

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

client.run('token')
Run Code Online (Sandbox Code Playgroud)

bot = commands.Bot(command_prefix='?', description=description)
@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')

@bot.command()
async def add(left : int, right : int):
    """Adds two numbers together."""
    await bot.say(left + right)

bot.run('token')
Run Code Online (Sandbox Code Playgroud)

我开始认为它们具有非常相似的特质,可以做相同的事情,但是个人偏爱与客户端还是机器人。但是它们确实存在差异,客户端on_message机器人会等待一会儿prefix command

可有人请澄清之间的区别clientbot

abc*_*ccd 5

l

只需使用commands.Bot


Bot是的扩展版本Client(处于子类关系中)。就是 它是Client的扩展,其中启用了命令,因此是子目录的名称ext/commands

Bot类继承了所有的功能Client,这意味着一切,你可以做ClientBot可以做到这一点。最引人注目的增加是变成了命令驱动(@bot.command()),而使用时,您将不得不手动处理事件Client。缺点Bot是您将不得不通过查看示例或源代码来学习其他功能,因为命令扩展没有太多文档说明。

如果您只希望您的机器人接受命令并处理它们,则使用它会容易得多,Bot因为所有处理和预处理都已为您完成。但是,如果您渴望编写自己的句柄并使用discord.py做疯狂的特技,则一定要使用base Client


如果您对选择的选择感到困惑,我建议您使用commands.Bot它,因为它使用起来容易得多,而且它Client已经可以做所有的事情。并且请记住,您不需要两者

错误:

client = discord.Client()
bot = commands.Bot(".")

# do stuff with bot
Run Code Online (Sandbox Code Playgroud)

正确:

bot = commands.Bot(".")

# do stuff with bot
Run Code Online (Sandbox Code Playgroud)