Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Discord.py类型错误:<;lambda>;()接受0个位置参数,但给出了1个_Python 3.x_Discord.py - Fatal编程技术网

Python 3.x Discord.py类型错误:<;lambda>;()接受0个位置参数,但给出了1个

Python 3.x Discord.py类型错误:<;lambda>;()接受0个位置参数,但给出了1个,python-3.x,discord.py,Python 3.x,Discord.py,您好,我不知道为什么此代码不起作用: class MusicPlayer(): __slots__ = ("client", "_guild", "_ctxs", "_channel", "_cog", "np", "volume", "current", "colour", "task")

您好,我不知道为什么此代码不起作用:

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
self.after(错误)

TypeError:当您有
lambda:…
这是一个不带参数的函数时,您得到的错误意味着调用它的对象会传递一个参数,有时回调传递一个您希望忽略的事件对象,因此通常解决方案是执行以下操作:

lambda x=None: ...

这样,它可以接受1个或0个参数,并且您不需要使用
x

您可以提供有关错误在代码中发生的位置的其他信息吗?我很难将错误与代码进行匹配。我想它在这里的某个地方:voice\u channel.play(player,after=lambda:self.bot.loop.call\u soon\u threadsafe(MusicPlayer.next.set))第一首音乐播放但队列不起作用-在第一首音乐结束后,其显示类型错误不确定这是否起作用,但我查看您的代码的唯一解决方案是将
voice\u channel.play(player,after=lambda:self.bot.loop.call\u soon\u threadsafe(MusicPlayer.next.set))
更改为
voice\u channel.play(player,after=lambda:self.bot.loop.call_soon_threadsafe(next.set))
正如上面在代码中使用的那样,显然成功了。同样的,控制台说:Traceback(最近一次调用):File“/usr/local/lib/python3.6/site packages/discord/player.py”,第611行,在self.after(error)TypeError:()接受0个位置参数,但1被赋予Nokay,感谢这帮助我删除了该错误,我改为voice_channel.play(player,after=lambda x=None:ctx.bot.loop.call_soon_threadsafe(MusicPlayer.next.set))但是现在我的下一首歌不起作用了,不要说任何错误好的,但是idk如何帮助,你是否已经通过调试,比如添加打印语句,并确保它们在你期望的时候运行?