您应该使用after
的参数FFmpegPCMAudio
功能。此参数采用可调用函数作为参数,并在当前歌曲结束时执行它。
song_queue = []
source = sound + '.mp3'
soung_queue.append(source)
if not vc.is_playing():
vc.play(discord.FFmpegPCMAudio(source=source, after=lambda e: play_next(ctx))
await ctx.send("Now playing...")
else:
await ctx.send('Song queued')
- 最后,您必须创建一个
play_next()
功能 :
import asyncio
def play_next(ctx, source):
if len(self.song_queue) >= 1:
del self.song_queue[0]
vc = get(self.bot.voice_clients, guild=ctx.guild)
vc.play(discord.FFmpegPCMAudio(source=source, after=lambda e: play_next(ctx))
asyncio.run_coroutine_threadsafe(ctx.send("No more songs in queue."), self.bot.loop)
您可以命名play_next()
发挥你想要的任何功能。
如果您希望机器人因播放声音的时间太长而断开连接,请更改play_next()
函数:
import asyncio
def play_next(ctx, source):
vc = get(self.bot.voice_clients, guild=ctx.guild)
if len(self.song_queue) >= 1:
del self.song_queue[0]
vc.play(discord.FFmpegPCMAudio(source=source, after=lambda e: play_next(ctx))
else:
asyncio.sleep(90) #wait 1 minute and 30 seconds
if not vc.is_playing():
asyncio.run_coroutine_threadsafe(vc.disconnect(ctx), self.bot.loop)
asyncio.run_coroutine_threadsafe(ctx.send("No more songs in queue."), self.bot.loop)