#pass the url into here when a user calls the bot
async def play_music(client, message, url=None):
if url is None:
#function is being called from after (this will be explained in the next function)
if queue.size() > 0:
#fetch from queue
url = queue.dequeue()
else:
#Unset stored objects, also possibly disconnect from voice channel here
self.player = None
self.voice = None
return
if self.player is None:
#no one is using the stream player, we can start playback immediately
self.player = await self.voice.create_ytdl_player(url, after=lambda: play_next(client, message))
self.player.start()
else:
if self.player.is_playing():
#called by the user to add a song
queue.enqueue(url)
else:
#this section happens when a song has finished, we play the next song here
self.player = await self.voice.create_ytdl_player(url, after=lambda: play_next(client, message))
self.player.start()
我将用适用于单个实例的代码来回答这个问题(尽管通过从字典中获取正确的播放器和voice_channel对象来编辑多个实例应该不难)。在
你必须首先建立一个队列来存储你的播放器将播放你的对象的网址。我假设您还应该创建一个队列字典来存储不同服务器的不同url。在
为了帮助管理stream_player工作流,首先在最外层声明一个voice和player对象。在
^{1}$在机器人加入语音通道后,应设置语音对象:
^{pr2}$然后,我们必须创建两个函数,因为Python不支持async lamdas,而管理流播放器只能从一个异步函数完成。每当用户键入相关命令时,bot应该调用play_music函数:
play_next函数将在流播放器完成歌曲后从终结器调用,并将再次调用上述函数,但不带url参数。在
相关问题 更多 >
编程相关推荐