Привет, я не знаю, почему этот код не работает:
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
ctx.bot.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))
@bot.command(aliases=['yt', 'youtube'])
async def play(ctx, url):
channel = ctx.author.voice.channel
if url is None:
await ctx.send("Music: Please specify a Youtube URL. Syntax (!play {URL})")
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.")
return
await channel.connect()
else:
if not ctx.author.voice:
await ctx.send("Music: Please join a Voice Channel or use join command.")
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=ctx.bot.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))
return
voice_channel = ctx.guild.voice_client
voice_channel.play(player, after=lambda: self.bot.loop.call_soon_threadsafe(MusicPlayer.next.set))
await ctx.send('Music: Now playing {}'.format(player.title))
Кто может объяснить, почему это не работает? Консоль сообщает мне эту ошибку:
self.after(error)
TypeError: <lambda () takes 0 positional arguments but 1 was given
-1
CiemnyTM
17 Июл 2020 в 20:44
1 ответ
Лучший ответ
Когда у вас есть lambda: ...
, это функция, которая не принимает аргументов, ошибка, которую вы получаете, подразумевает, что все, что когда-либо вызывает ее, передает ей аргумент, иногда обратные вызовы передают объект события, который вы хотите игнорировать, поэтому обычно решение сделать что-то вроде:
lambda x=None: ...
Таким образом он может принимать 1 или 0 аргументов, и вам не нужно использовать x
.
0
Tadhg McDonald-Jensen
17 Июл 2020 в 18:32
Похожие вопросы
Новые вопросы
python-3.x
НЕ ИСПОЛЬЗУЙТЕ, ЕСЛИ ВАШ ВОПРОС ТОЛЬКО ДЛЯ PYTHON 3. Всегда используйте вместе со стандартным тегом [python].