discord bot 如何处理来自多个服务器的事件

How does a discord bot handle events from multiple servers

我正在为我的服务器开发一个使用 discord.py(重写分支)的 discord 机器人,我需要将机器人邀请到多个服务器并同时使用它。

我的问题是:

我是否需要为每个服务器设置一个新线程,或者机器人是否对事件进行排队并一个一个地处理它们?如果它确实将它们排队,我应该只使用它还是使用单独的线程?

抱歉,如果这是一个菜鸟问题,但我对 discord.py 还很陌生,我还不太了解它是如何工作的。

感谢阅读

多进程、线程或队列都可以用来解决这个问题,各有优缺点。我个人会使用线程,因为需要在每台服务器上发生的事件大多彼此独立。

不,你只需要定义回调(主要使用Client.event and Bot.command) that the bot will execute when something happens. All of the logic for listening for events happening on the servers has been taken care of for you. See the examples directory in the discord.py project for some examples

如果您的 bot 使用非常广泛(数百台服务器),您将不得不开始使用分片,这是一个在同一 bot 的多个实例之间自动拆分流量的过程。您可以查看 AutoShardedClient 的文档以了解其工作原理。