错误输出
AttributeError: 'member_descriptor' object has no attribute 'put'
我目前有一个MusicPlayer类供我的机器人以我想要的方式播放音乐。我相信我的命令导致了这个问题,但是我不确定。我从未听说过member_descriptor,并且在查找时似乎对这个错误的了解不多。我只想将歌曲添加到队列中并按此顺序播放,如果队列中没有歌曲,则应该播放。
下面是我的班级和指挥部。任何帮助都非常感谢!
已更新
MusicPlayer类
class MusicPlayer():
__slots__ = ("client", "_guild", "_ctxs", "_channel", "_cog", "np", "volume", "current", "colour", "task")
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.task = self.client.loop.create_task(self.player_loop())
async def player_loop(self):
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
async def add_song(self, player):
return await self.queue.put(player)
def destroy(self, guild):
return self.client.loop.create_task(self._cog.cleanup(guild))
播放命令
@commands.command(aliases=['yt', 'youtube'])
async def play(self, ctx, *, url=None):
await ctx.message.delete()
channel = ctx.message.author.voice.channel
if url is None:
await ctx.send("Music: Please specify a Youtube URL. Syntax (!play {URL})", delete_after=7)
return
if ctx.guild.voice_client is None:
if not ctx.author.voice:
await ctx.send("Music: Please join a Voice Channel or use join command.", delete_after=7)
return
await channel.connect()
else:
if not ctx.author.voice:
await ctx.send("Music: Please join a Voice Channel or use join command.", delete_after=7)
return
if ctx.guild.voice_client.channel != ctx.message.author.voice.channel:
await ctx.guild.voice_client.move_to(channel)
async with ctx.typing():
player = await YTDLSource.from_url(url, loop=self.client.loop, stream=True)
if ctx.guild.voice_client.is_playing():
await MusicPlayer.add_song(MusicPlayer, player)
await ctx.send('Music: {} has now been added to the Queue'.format(player.title), delete_after=7)
return
voice_channel = ctx.guild.voice_client
voice_channel.play(player, after=lambda: self.client.loop.call_soon_threadsafe(MusicPlayer.next.set))
await ctx.send('Music: Now playing {}'.format(player.title), delete_after=7)
答案 0 :(得分:1)
类MusicPlayer
具有由queue
创建的描述符__slots__
。在执行MusicPlayer.queue
时,您将直接访问该描述符,而不是使用music_player.queue
来访问实例的属性。
有时,您需要从类中创建一个MusicPlayer
对象,并使用它代替类本身。