如何用discord bot播放歌曲列表(不和.py)

2024-05-19 19:28:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试过创建一个字典,并将.create_ytdl_player()实例的列表存储到每个discord服务器id

我只需要知道如何让球员们在上一场比赛结束后继续比赛。在

我想我必须使用.is_ngplaying()或.is_done(),但我不知道如何使用它们。有人能帮忙吗?在


Tags: 实例服务器id列表字典iscreateplayer
1条回答
网友
1楼 · 发布于 2024-05-19 19:28:33

我将用适用于单个实例的代码来回答这个问题(尽管通过从字典中获取正确的播放器和voice_channel对象来编辑多个实例应该不难)。在

你必须首先建立一个队列来存储你的播放器将播放你的对象的网址。我假设您还应该创建一个队列字典来存储不同服务器的不同url。在

为了帮助管理stream_player工作流,首先在最外层声明一个voice和player对象。在

^{1}$

在机器人加入语音通道后,应设置语音对象:

^{pr2}$

然后,我们必须创建两个函数,因为Python不支持async lamdas,而管理流播放器只能从一个异步函数完成。每当用户键入相关命令时,bot应该调用play_music函数:

#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()

play_next函数将在流播放器完成歌曲后从终结器调用,并将再次调用上述函数,但不带url参数。在

def play_next(client, message):
    asyncio.run_coroutine_threadsafe(play_music(client, message), client.loop)

相关问题 更多 >