vc中的discord.py无法播放音乐,报错ClientException:未连接到语音

问题描述 投票:0回答:1

我无法在 vc 中播放任何声音,我的代码昨晚正在运行,现在它只是返回 ClientException:未连接到语音。

discord.errors.ClientException:未连接到语音。

discord.app_commands.errors.CommandInvokeError:命令“播放”引发异常:ClientException:未连接到语音。

如何修复我的代码以再次播放歌曲而不返回相同的错误

@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)
python discord discord.py voice
1个回答
0
投票
voice_channel = discord.utils.get(guild.channels, name='music.')

此线路可能无法正确找到您要连接的语音通道。 最好使用

discord.utils.get
并将类型参数设置为
discord.VoiceChannel
。 喜欢:

voice_channel = discord.utils.get(guild.voice_channels, name='music')

此外,您可能需要在尝试创建新连接之前检查机器人是否已连接到语音通道。

voice_channel = discord.utils.get(guild.voice_channels, name='music')

if not voice_channel:
    # The voice channel was not found
    embed = discord.Embed(title=f"**Playback Error**", description="Voice channel 'music' not found.", color=0xe74c3c)
    await channel.send(embed=embed)
    return

if not 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)
© www.soinside.com 2019 - 2024. All rights reserved.