cannot play music in vc discord.py, error ClientException: Not connected to voice

76 Views Asked by At

I cannot play any sound in vc. my code was working last night,However now its just returning ClientException: not connected to voice.

discord.errors.ClientException: Not connected to voice.

discord.app_commands.errors.CommandInvokeError: Command 'play' raised an exception: ClientException: Not connected to voice.

How do i fix my code to play the songs again without returning the same error

@bot.tree.command(name="play", description="plays a song provided")
async def self(interaction: discord.Interaction, song: str):
    guild = interaction.guild
    voice_channel = discord.utils.get(guild.channels, name='music.')
    channel = interaction.channel
    if not interaction.guild.voice_client:
        # Connect to a voice channel
        player = await voice_channel.connect()
    else:
        # Use the existing voice client
        player = discord.utils.get(bot.voice_clients, guild=guild)

    
    # try:
    if not player.is_playing():
        try:
            embed = discord.Embed(title=f"**Searching**", description=f"Searching for: {song}", color=0x13fc03)
            await interaction.response.send_message(embed=embed)
            with youtube_dl.YoutubeDL({'format': 'bestaudio', 'max_results': 1, 'noplaylist': True}) as ydl:
                info = ydl.extract_info(f"ytsearch1:{song}", download=False)
                url = info['entries'][0]['url']


        except:
            embed = discord.Embed(title=f"**Playback Error**", description=f"Song not found: {song}", color=0xe74c3c)
            await channel.send(embed=embed)
            return
        print(url)
            
        FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
        source = FFmpegPCMAudio(url, **FFMPEG_OPTIONS)
        print(player)
        player.play(source)
        print(source)
        embed = discord.Embed(title=f"**Playing**", description=f"Playing: {song}", color=0x13fc03)
        await channel.send(embed=embed)
        
        while player.is_playing():
            await asyncio.sleep(1)
        while que != []:
            next_song = que.pop(0)
            nque.pop(0)
            FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
            source = FFmpegPCMAudio(next_song, **FFMPEG_OPTIONS)
            player.play(source)
            while player.is_playing():
                await asyncio.sleep(1)
            
    else:
        embed = discord.Embed(title=f"**Playback Error**", description=f"Already playing.", color=0xe74c3c)
        await interaction.followup.send(embed=embed)
0

There are 0 best solutions below