Bot 和 Client 有什么区别?

What are the differences between Bot and Client?(Bot 和 Client 有什么区别?)

本文介绍了Bot 和 Client 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了一些关于如何制作 Discord Python Bot 的示例,并且我已经看到 clientbot 几乎可以互换使用,我正在无法找到你什么时候会使用哪一个.

I've been going through some examples on how to make a Discord Python Bot and I've been seeing client and bot being used almost interchangeably and I'm not able to find when you would use which one when.

例如:

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 而机器人等待 prefix 命令.

I'm beginning to think they have very similar qualities and can do the same things but is a personal preference to go with a client vs. a bot. However they do have their differences where clients have an on_message while bots wait for a prefix command.

有人可以澄清clientbot之间的区别吗?

Can someone please clarify the difference between client and bot?

推荐答案

Tl;dr

只需使用 commands.Bot.

BotClient 的扩展版本(它处于子类 关系中).IE.它是启用了命令的客户端的扩展,因此是子目录 ext/commands 的名称.

Bot is an extended version of Client (it's in a subclass relationship). Ie. it's an extension of Client with commands enabled, thus the name of the subdirectory ext/commands.

Bot 类继承了 Client 的所有功能,这意味着您可以使用 ClientBot 也可以.最引人注目的新增功能是命令驱动(@bot.command()),而使用 Client 时您必须手动处理事件.Bot 的一个缺点是您必须通过查看示例或源代码来学习其他功能,因为命令扩展没有太多文档记录.UPD:现在记录在这里.

The Bot class inherits all the functionalities of Client, which means that everything you can do with Client, Bot can do it too. The most noticeable addition was becoming command-driven (@bot.command()), whereas you would have to manually work with handling events when using Client. A downside of Bot is that you will have to learn the additional functionalities from looking at examples or source codes since the commands extension isn't much documented. UPD: Now it is documented here.

如果你只是想让你的机器人接受命令并处理它们,那么使用 Bot 会容易得多,因为所有的处理和预处理都是为你完成的.但是,如果您渴望编写自己的句柄并使用 discord.py 做疯狂的特技,那么请务必使用基础 Client.

If you simply want your bots to accept commands and handle them, it would be a lot easier to work with the Bot since all the handling and preprocessing are done for you. But if you're eager to write your own handles and do crazy stunts with discord.py, then by all means, use the base Client.

如果您不知道如何选择,我建议您使用 commands.Bot,因为它更容易使用,并且除了 Client 已经可以了.请记住,您不需要两者.

In case you're stumped by which to choose, I recommend you to use commands.Bot since it's a lot easier to work with and it's in addition of everything Client can already do. And please remember that you do not need both.

错误:

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

# do stuff with bot

正确:

bot = commands.Bot(".")

# do stuff with bot

这篇关于Bot 和 Client 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Bot 和 Client 有什么区别?

基础教程推荐