我目前正在研究MusicPlayer,该音乐循环遍历类MusicPlayer()
中的一个函数。问题在于它似乎甚至没有在播放。以下是我的课程,并且create_task位于__init__
内,但它从未运行过。我之前曾跑过循环,但从未上过课,我是否缺少某些东西?
MusicPlayer类别:
class MusicPlayer():
__slots__ = ("client","_guild","_ctxs","_channel","_cog","np","volume","current","colour")
queue = asyncio.Queue()
next = asyncio.Event()
def __init__(self,ctx,client):
self.client = client
self._guild = ctx.guild
self._ctxs = ctx
self._channel = ctx.channel
self._cog = ctx.cog
self.np = None
self.volume = defaultvolume
self.current = None
self.colour = self.client.defaultcolour
self.client.loop.create_task(self.player_loop())
async def player_loop(self):
print('player_loop ran')
await self.client.wait_until_ready()
while True:
self.next.clear()
try:
async with timeout(300):
self.current = await 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.client.loop.call_soon_threadsafe(next.set))
self.current.volume = self.volume
thumbnail = self.current.thumbnail if self.current.thumbnail else self.client.user.avatar_url
self.colour = await self.client.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.client.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 next.wait()
print("Terminated")
# Cleanup player
self.current.cleanup()
self.current = None