Bot和Client有什么区别? [英] What are the differences between Bot and Client?
问题描述
我一直在研究一些有关如何制作Discord Python Bot的示例,并且已经看到 client
和 bot
几乎可以互换使用,我无法找到何时使用哪个时间。
例如:
client = discord.Client()
@ client.event
异步定义on_message(message):
#我们不这样做希望机器人在message.author == client.user:
时回复自己
如果message.content.startswith('$ guess'):$ b $返回
b等待client.send_message(message.channel,'猜一个1到10之间的数字')
defgues_check(m):
返回m.content.isdigit()
@ client.event
异步定义on_ready():
print('登录为')
print(client.user.name)
print(client。 user.id)
print('------')
client.run('token')
vs。
bot =命令.Bot(comm and_prefix ='?',description = description)
@ bot.event
异步定义on_ready():
print('登录为')
print(bot.user。名称)
print(bot.user.id)
print('------')
@ bot.command()
异步定义添加(左:int,右:int):
将两个数字加在一起。
等待bot.say(左+右)
bot.run( 'token')
我开始认为它们具有非常相似的品质并且可以做相同的事情但是,个人偏好与客户端还是机器人一起使用。但是,它们之间确实存在差异,即客户端具有 on_message
,而漫游器则等待 prefix命令
。
有人可以澄清客户端
和 bot
之间的区别吗?
Tl; dr
只需使用命令。 Bot
。
Bot
是 Client
的扩展版本(属于 subclass 关系)。就是它是Client的扩展,启用了命令,因此是子目录 ext / commands
的名称。
Bot
类继承了 Client
的所有功能,这意味着您可以使用 Client
, Bot
进行的所有操作。最引人注目的增加是由命令驱动的( @ bot.command()
),而使用 Client时,您必须手动处理事件
。 Bot
的缺点是,由于命令扩展没有太多文档,您必须通过查看示例或源代码来学习其他功能。
如果您只希望您的机器人接受命令并处理它们,那么使用 Bot
会容易得多,因为所有处理和预处理已为您完成。但是,如果您渴望编写自己的句柄并使用discord.py做疯狂的特技,则一定要使用基本的 Client
。
万一您无法选择,建议使用命令。Bot
,因为它使用起来要容易得多,并且它是客户端
已经可以做的所有事情。并且请记住,您不需要两者。
错误:
client = discord.Client()
bot = commands.Bot(。)
#用bot $做东西b $ b
正确:
bot =命令。Bot(。)
$ p $做东西p>
#用机器人
I've been going through some examples on how to make a Discord Python Bot and I've been seeing
client
andbot
being used almost interchangeably and I'm not able to find when you would use which one when.For example:
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')
vs.
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')
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 aprefix command
.Can someone please clarify the difference between
client
andbot
?解决方案Tl;dr
Just use
commands.Bot
.
Bot
is an extended version ofClient
(it's in a subclass relationship). Ie. it's an extension of Client with commands enabled, thus the name of the subdirectoryext/commands
.The
Bot
class inherits all the functionalities ofClient
, which means that everything you can do withClient
,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 usingClient
. A downside ofBot
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.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 baseClient
.
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 everythingClient
can already do. And please remember that you do not need both.WRONG:
client = discord.Client() bot = commands.Bot(".") # do stuff with bot
CORRECT:
bot = commands.Bot(".") # do stuff with bot
这篇关于Bot和Client有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!