我有一个不和谐的 py 机器人。是否有可能以某种方式使其可以添加到 2 个不同的服务器并异步工作?(现在,当他从第一台服务器发出命令时,他开始在另一台服务器上播放音乐)这是部分代码:
class MyBot(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
self.connect = None
self.voice_channel = None
@commands.command()
async def join(self, ctx):
self.voice_channel = discord.utils.get(ctx.guild.voice_channels, name=ctx.message.author.voice.channel.name)
if not self.connect:
self.connect = await self.voice_channel.connect()
@commands.command()
async def leave(self, ctx):
if self.connect:
await self.connect.disconnect()
del self.connect
你应该为所有服务器做 db 。学习python和数据库。 Python,数据库介绍
过了一会儿,我想出了这个结构:
初始化机器人时,我将其所有服务器添加到 {server: bot object that will process the command} 字典。结果是这样的:
不知道我做的对不对,不过这个方法看起来很合逻辑也很方便。