How does a discord bot handle events from multiple servers

好久不见. 提交于 2020-07-10 09:34:29

问题


I'm developing a discord bot using discord.py (rewrite branch) for my servers and I need to invite the bot to multiple servers and use it simultaneously.

My question is:

Do I need to set up a new thread for every server or does the bot queue events and handle them one by one? if it does queue them, should I just use that or use separate threads?

Sorry if this is a noobish question but I'm fairly new to discord.py and I don't really understand how it works just yet.

Thanks for reading


回答1:


No, you just need to define the callbacks (mainly using 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.

If your bot is used very widely (hundreds of servers) you will have to start using sharding, which is a process of automatically splitting the traffic between multiple instances of the same bot. You can look at the documentation for the AutoShardedClient to see how that would work.




回答2:


Multiprocesses, threads or queues can all be used to approach this issue each with their respective advantages and disadvantages . Personally I would use threads as the events that need to take place on each server are independent of each other mostly.



来源:https://stackoverflow.com/questions/57550170/how-does-a-discord-bot-handle-events-from-multiple-servers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!