在尝试使用YouTube\u DL流式传输YouTube链接时,出现错误“'NoneType'对象没有属性'play'”

2024-10-03 02:37:14 发布

您现在位置:Python中文网/ 问答频道 /正文

代码:

    async def memesoundeffect(self, ctx):
        if await checkroles(ctx, ctx.author):
            async with ctx.typing():
                player = await YTDLSource.from_url("https://www.youtube.com/watch?v=J85jV37CsYE&list=PLWL3FzHaRRMkQqUhks8Y9l35rqY_kKCto&index=1", loop=self.bot.loop, stream=True)
                ctx.voice_client.play(player, after=lambda e: print("Player error: %s" % e) if e else None)
                print()
            await ctx.send("Now playing: {}".format(player.title))
        else:
            await ctx.channel.send("You do not have the required permissions for this command. Requirements: Admin+")
            print()

当我运行它时,这是我得到的错误:

Traceback (most recent call last):
  File "/Users/anna54office/PycharmProjects/DiscordtestBot/venv/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "/Users/anna54office/PycharmProjects/DiscordtestBot/venv/lib/python3.9/site-packages/discord/ext/commands/core.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/Users/anna54office/PycharmProjects/DiscordtestBot/venv/lib/python3.9/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'play'

我怎样才能解决这个问题


Tags: venvlibsiteawaitusersextcommandsfile
1条回答
网友
1楼 · 发布于 2024-10-03 02:37:14

这样做不是更容易吗

@client.command()
async def play(ctx, url : str):
    song_there = os.path.isfile('song.mp3')
 
    try:
        if song_there:
            os.remove('song.mp3')
            print('[log] Old file delete')
    except PermissionError:
        print('[log] error no delete file')
 
    await ctx.send('...')
 
    voice = get(client.voice_clients, guild = ctx.guild)
 
    ydl_opts = {
        'format' : 'bestaudio/best',
        'postprocessors' : [{
            'key' : 'FFmpegExtractAudio',
            'preferredcodec' : 'mp3',
            'preferredquality' : '192'
        }],
    }
 
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        print('[log] Download music...')
        ydl.download([url])
 
    for file in os.listdir('./'):
        if file.endswith('.mp3'):
            name = file
            print(f'[log] Rename File: {file}')
            os.rename(file, 'song.mp3')
 
    voice.play(discord.FFmpegPCMAudio('song.mp3'), after = lambda e: print(f'[log] {name}, music stopped'))
    voice.source = discord.PCMVolumeTransformer(voice.source)
    voice.source.volume = 0.07
 
    song_name = name.rsplit('-', 2)
    await ctx.send(f'Song Playes: {song_name[0]}')

相关问题 更多 >