如何使voice.play在我的机器人的cog文件中工作?discord.py重写

如何使voice.play在我的机器人的cog文件中工作?discord.py重写,discord.py,discord.py-rewrite,Discord.py,Discord.py Rewrite,我正在尝试创建一个播放视频的机器人 我已经为我的机器人(用于语音频道)实现了join和leave命令,但我似乎无法让它播放视频。我已经试着把它放在我的主bot文件中,它工作得非常好,但是我想把它变成一个cog,这样它在help命令下就是它自己的类别了,我想不出来 它下载了这首歌,一切看起来都很好,但它只是没有播放视频,并向我显示了一个错误 这是我的机器人的代码: @bot.command(pass_context=True, aliases=["p"]) async def play(s

我正在尝试创建一个播放视频的机器人

我已经为我的机器人(用于语音频道)实现了
join
leave
命令,但我似乎无法让它播放视频。我已经试着把它放在我的主bot文件中,它工作得非常好,但是我想把它变成一个cog,这样它在help命令下就是它自己的类别了,我想不出来

它下载了这首歌,一切看起来都很好,但它只是没有播放视频,并向我显示了一个错误

这是我的机器人的代码:

@bot.command(pass_context=True, aliases=["p"])
    async def play(self,ctx, url: str):
        song_there = os.path.isfile("song.mp3")
        try:
            if song_there:
                os.remove("song.mp3")
                print("Removed old song file")
        except PermissionError:
            print("Trying to delete song file, but its being played.")
            await ctx.send("ERROR: Music playing.")
            return
        await ctx.send("Getting everything ready...")

        voice = get(bot.voice_clients, guild=ctx.guild)

        ydl_opts = {
            'format': 'bestaudio/best',
            'default_search': 'auto',
            'postprocessors': [{
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'mp3',
                'preferredquality': '192',
            }],
        }

        with youtube_dl.YoutubeDL(ydl_opts) as ydl:
            print("Downloading audio now\n")
            ydl.download([url])

        for file in os.listdir("./"):
            if file.endswith(".mp3"):
                name = file
                print(f"Renamed File: {file}\n")
                os.rename(file, "song.mp3")

        voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e:print(f'{name} has finished playing'))
        voice.source = discord.PCMVolumeTransformer(voice.source)
        voice.source.valume = 0.07

        nname = name.rsplit("-", 2)
        await ctx.send(f"Playing: {nname}")
        print("Playing\n")
这就是它给我的错误:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'play'

我有一个类似的问题,似乎声音在齿轮中不可用,因为它没有传入,所以需要声明。尝试将
voice=ctx.voice\u client
添加到
voice.play(…)
上面,我遇到了一个类似的问题,似乎voice在cog中不可用,因为它没有传入,所以需要声明。尝试在
voice.play(…)
上方添加
voice=ctx.voice\u客户端