机器人和客户端有什么区别?

24

我一直在学习如何制作Discord Python机器人的例子,我看到 clientbot 几乎可以互换使用,但我无法找到在何时使用哪个。

例如:

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

对抗。

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

我开始认为它们具有非常相似的特点,并且可以做相同的事情,但是选择与客户端还是机器人在于个人喜好。然而,它们确实存在差异,其中客户端具有on_message,而机器人则等待前缀命令

请问有人能澄清一下clientbot之间的区别吗?

1个回答

49

简介

只需使用commands.Bot


BotClient的扩展版本(它们之间是一个子类关系)。也就是说,它是启用了命令的Client的扩展,因此才有目录ext/commands的名称。

Bot类继承了Client的所有功能,这意味着您可以使用Client做的任何事情,Bot也可以做到。最明显的增加是变成了基于命令的(@bot.command()),而在使用Client时必须手动处理事件。 Bot的缺点是您将不得不通过查看示例或源代码来学习附加功能,因为命令扩展没有太多文档。更新:现在已经在这里记录了:here

如果您只想让您的机器人接受并处理命令,那么使用Bot会更容易,因为所有处理和预处理都已为您完成。但是,如果您渴望编写自己的处理程序并在discord.py中尝试疯狂的特技,请务必使用基本的Client


如果您不知道该选择哪一个,我建议您使用commands.Bot,因为它更易于使用,并且除了Client已经可以做到的一切之外还有更多功能。请记住,您不需要同时使用两者

WRONG:

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

# do stuff with bot

正确:

bot = commands.Bot(".")

# do stuff with bot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接