Search code examples
pythondiscord.pyyoutube-dl

Discord.py voice bot youtube_dl invalidate error


Creating a python discord voice bot using youtube_dl using this code for the play command:

@bot.command(pass_context=True, brief="This will play a song 'play [url]'", aliases=['pl'])
async def play(ctx, url:str):
    server = ctx.message.server
    voice_client = bot.voice_client_in(server)
    player = await voice_client.create_ytdl_player(url, after=lambda: check_queue(server.id))
    players[server.id] = player
    player.start()

After running the command in the discord the song will play, but after some time if the link is a youtube video the song will stop and I get this error:

[tls @ 000001b884817cc0] Error in the pull function.
[matroska,webm @ 000001b88417a180] Read error
[tls @ 000001b884817cc0] The specified session has been invalidated for some reason.
    Last message repeated 1 times

I've done some research and I think it's because the youtube link expires. Thanks for any help!


Solution

  • You can add '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5' as part of before_options.

    @bot.command(pass_context=True, brief="This will play a song 'play [url]'", aliases=['pl'])
    async def play(ctx, url:str):
        server = ctx.message.server
        voice_client = bot.voice_client_in(server)
        player = await voice_client.create_ytdl_player(url, after=lambda: check_queue(server.id), before_options='-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5')
        players[server.id] = player
        player.start()