【问题标题】:What are the differences between Bot and Client?Bot 和 Client 有什么区别?
【发布时间】:2018-12-16 12:02:56
【问题描述】:

我已经浏览了一些关于如何制作 Discord Python Bot 的示例,并且我看到 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,而机器人等待prefix command

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

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    Tl;博士

    只需使用commands.Bot


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

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

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


    如果您不知道如何选择,我建议您使用commands.Bot,因为它更容易使用,而且它是Client 已经可以做的所有事情的补充。请记住,您不需要两者

    错误:

    client = discord.Client()
    bot = commands.Bot(".")
    
    # do stuff with bot
    

    正确:

    bot = commands.Bot(".")
    
    # do stuff with bot
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-09
      • 2017-08-31
      • 2020-03-30
      相关资源
      最近更新 更多