不和谐。ext.命令。错误。CommandInvokeError:命令提出了一个例外:ClientException:未连接到语音。 错误的当连接到语音

0

的问题

我讨厌愚蠢的声音在这个(我可能做到一些)我真的想我的最好的。 我一直有的问题,d。py的声音。 我有一个机器人与音乐机器人的功能性,和当我试运行命令 k!play (song name) 它未说它没有连接到语音,当它连接上的客户的侧面,并且代码侧。 我不明白这是怎么回事。 这里的代码整命令,以及整个错误的日志在于:

@client.command(aliases=['p'])
async def play(ctx, *, query: t.Optional[str]):
    song_there = os.path.isfile("song.mp3")
    try:
        if song_there:
            os.remove("song.mp3")

    except PermissionError:
        return


    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    if voice is None:
        voiceChannel = ctx.message.author.voice.channel
        await voiceChannel.connect()
        print("Connected to voice")
        voice = discord.utils.get(client.voice_clients, guild=ctx.guild)

    spotify_regex1 = r"https://open.spotify.com/track/(......................)"
    spotify_regex2 = r"https://open.spotify.com/track/(.......................)(si=)(................)"

    match = re.match(spotify_regex1, query)
    is_match1 = bool(match)
    match = re.match(spotify_regex2, query)
    is_match2 = bool(match)

    if is_match1 or is_match2 is True:
        print("Trying With Spotify")
        os.system(f"spotdl '{query}'")
        await ctx.send(f"`Now Playing:`  {query}")


    ydl_opts = {
        'format': 'bestaudio',
        'restrictfilenames': False,
        'noplaylist': True,
        'nocheckcertificate': True,
        'no_warnings': True,
        'default_search': 'auto',
        'source_address': '0.0.0.0',
    }

    try:
        with youtube_dl.YoutubeDL(ydl_opts) as ydl:
            ydl.download([query])
            query_id = os.popen(f'youtube-dl --get-id "ytsearch:{query}"').read()
            title = os.popen(f'youtube-dl --get-title "ytsearch:{query_id}"').read()
            thumbnail_embed = os.popen(f'youtube-dl --get-thumbnail "ytsearch:{query_id}"').read()
            duration_embed = os.popen(f'youtube-dl --get-duration "ytsearch:{query_id}"').read()
            embed = discord.Embed(title="Now Playing", color=0xa00000)
            embed.set_thumbnail(url=thumbnail_embed)
            embed.add_field(name=title, value=f"`0:00 / {duration_embed}`", inline=True)
            embed.set_footer(text=f"Requested by {ctx.message.author}")
            await ctx.send(embed=embed)

    except:
        return

    for file in os.listdir("./"):
        if file.endswith(".mp3"):
            os.rename(file, "song.mp3")
            print("Changed mp3 to mp3")

    for file in os.listdir("./"):
        if file.endswith(".m4a"):
            os.rename(file, "song.mp3")
            print("Changed m4a to mp3")

    for file in os.listdir("./"):
        if file.endswith(".webm"):
            os.rename(file, "song.mp3")
            print("Changed webm to mp3")

    if song_there is False:
        time.sleep(5)
        await ctx.send("Song not found!")

    print("Playing File (or trying to)")

    voice.play(discord.FFmpegPCMAudio("song.mp3"))
    voice.source = discord.PCMVolumeTransformer(voice.source)
    voice.source.volume = 0.80

Connected to voice
[download] Downloading playlist: fuwa fuwa time
[youtube:search] query "fuwa fuwa time": Downloading page 1
[youtube:search] playlist fuwa fuwa time: Downloading 1 videos
[download] Downloading video 1 of 1
[youtube] jL8p9vteR5g: Downloading webpage
[youtube] Downloading just video jL8p9vteR5g because of --no-playlist
[youtube] jL8p9vteR5g: Downloading player f1ca6900
[download] Destination: K-ON! Fuwa Fuwa Time Yui and Mio Duet HD-jL8p9vteR5g.m4a
[download] 100% of 3.71MiB in 01:05                 
[ffmpeg] Correcting container in "K-ON! Fuwa Fuwa Time Yui and Mio Duet HD-jL8p9vteR5g.m4a"
[download] Finished downloading playlist: fuwa fuwa time
Changed m4a to mp3
Playing File (or trying to)
Ignoring exception in command play:
Traceback (most recent call last):
  File "C:\Users\..\PycharmProjects\kyoko test\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users..\PycharmProjects\kyoko test\main.py", line 850, in play
    voice.play(discord.FFmpegPCMAudio("song.mp3"))
  File "C:\Users\..\PycharmProjects\kyoko test\venv\lib\site-packages\discord\voice_client.py", line 555, in play
    raise ClientException('Not connected to voice.')
discord.errors.ClientException: Not connected to voice.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\..\PycharmProjects\kyoko test\venv\lib\site-packages\discord\ext\commands\bot.py", line 940, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\..\PycharmProjects\kyoko test\venv\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\..\PycharmProjects\kyoko test\venv\lib\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: ClientException: Not connected to voice.

比那是错误记录

discord discord.py python
2021-11-23 14:15:32
1

最好的答案

0

这一行代码可能是问题:

voice = discord.utils.get(client.voice_clients,guild=ctx.guild)

试试这个用于获取的声音客户:

voice = ctx.message.guild.voice_client

我不完全确信如果这个问题,但希望工作。 哦,还要确保你是在一个声音频道在运行命令。

2021-11-23 20:52:38

你能告诉我的哪一部分代码应该替换? 这个功能的定义是多次在不同的参数,你还在改变它被称为 voice_channel 这是令人困惑。 @Roopesh-J
Jacob Shankman

啊对不起对不断变化的变量名称,还是让我的编辑。 那么首先要确保你想要跑的 play 命令而你也在语音通道。 不要替代码但只是添加行我建议你的下线我提及。 它基本上只是一个不同的方式得到声音的客户,所以我想可能这样会的工作。
Roopesh-J

我这样做,它仍然给了我错误的,是的,我在一个vc,我测试过它,如果我不这给我一个"用户不在vc"的错误。
Jacob Shankman

该死的,那是很奇怪的。 我不知道还有什么可能是这一问题。 我建议可能建立另一个命令只是连接的机器人声音的频道,因为你可以只是视觉上的检查,如果该人是在道或没有。
Roopesh-J

这是码我用一个老机器人: voiceChannel = discord.utils.get(ctx.guild.voice_channels, name='General'). await voiceChannel.connect(). 我只有用它在我的通道所以我只是直线上升的硬编码的名称。 也许它会帮助虽然。
Roopesh-J

我用精确的代码,但是我改变它的任何vc只是,我会试试看到,如果工程。 (更新:它仍然,! 我不明白怎么回事因为我从来没有得到这一错误。)
Jacob Shankman

Roopesh-J

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................