好的,所以我有一个音乐齿轮和包括播放器的音乐命令。但是播放器无法正常工作:实际上,当我第一次使用play
命令时,BOT会播放歌曲。但是,当我将其他歌曲添加到队列中时,它们将不会播放。我在print('Terminated')
之后添加了await self.next.wait()
,(next
是asyncio.Event()
),但这从未被打印出来!谁能帮我吗?
class MusicPlayer:
__slots__ = ("bot", "_guild", "_ctxs", "_channel", "_cog", "queue", "next", "np", "volume", "current", "colour", "task")
def __init__(self, ctx, bot):
"""Initializes the MusicPlayer Class."""
self.bot = bot
self._guild = ctx.guild
self._ctxs = ctx
self._channel = ctx.channel
self._cog = ctx.cog
self.queue = asyncio.Queue()
self.next = asyncio.Event()
self.np = None
self.volume = defaultvolume
self.current = None
self.colour = self.bot.defaultcolour
self.task = self.bot.loop.create_task(self.player_loop())
async def player_loop(self):
"""The main player loop."""
await self.bot.wait_until_ready()
while True:
self.next.clear()
try:
async with timeout(300):
self.current = await self.queue.get()
except asyncio.CancelledError:
return
except asyncio.TimeoutError:
guild = self._guild
vc = guild.voice_client
self.destroy(guild)
if not vc: return
await self._ctxs.send(":point_right: **I disconnected myself from the **`{}`** voice channel as I was not playing audio for 5 minutes!**".format(vc.channel.name))
return
except:
self.destroy(self._guild)
await self._ctxs.send(":thumbsdown: **Error: getting next song failed!** Please retry later!")
return
self._ctxs.voice_client.play(self.current, after=lambda: self.bot.loop.call_soon_threadsafe(self.next.set))
self.current.volume = self.volume
thumbnail = self.current.thumbnail if self.current.thumbnail else self.bot.user.avatar_url
self.colour = await self.bot.get_average_colour(thumbnail)
embednps = discord.Embed(colour=self.colour)
embednps.add_field(name="Now Playing", value=f"```{self.current.title}```", inline=False)
embednps.add_field(name="Link", value=f"[URL]({self.current.web_url})", inline=True)
embednps.add_field(name="Duration", value=self.bot.time_from_seconds(self.current.duration), inline=True)
embednps.add_field(name="Channel", value=f"{self.current.uploader}", inline=False)
embednps.set_thumbnail(url=f"{thumbnail}")
embednps.set_footer(text=f"Requested by {self.current.requester}", icon_url=self.current.requester.avatar_url)
self.np = await self._channel.send(embed=embednps)
await self.next.wait()
print("Terminated")
# Cleanup player
self.current.cleanup()
self.current = None
def destroy(self, guild):
"""Disconnect and cleanup the player."""
return self.bot.loop.create_task(self._cog.cleanup(guild))