Discord.ext.com.mands.errors.CommandInvokeError:命令引发异常:Clien



我讨厌在这件事上听起来很愚蠢(我可能对一些人这样做(我真的在尽我最大的努力。我的嗓音一直有问题。我有一个具有音乐机器人功能的机器人,当我尝试运行命令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..PycharmProjectskyoko testvenvlibsite-packagesdiscordextcommandscore.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:Users..PycharmProjectskyoko testmain.py", line 850, in play
voice.play(discord.FFmpegPCMAudio("song.mp3"))
File "C:Users..PycharmProjectskyoko testvenvlibsite-packagesdiscordvoice_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..PycharmProjectskyoko testvenvlibsite-packagesdiscordextcommandsbot.py", line 940, in invoke
await ctx.command.invoke(ctx)
File "C:Users..PycharmProjectskyoko testvenvlibsite-packagesdiscordextcommandscore.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:Users..PycharmProjectskyoko testvenvlibsite-packagesdiscordextcommandscore.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ClientException: Not connected to voice.

这就是错误日志

这行代码可能是问题所在:

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

尝试获取语音客户端:

voice = ctx.message.guild.voice_client

我不完全确定这是否是问题,但希望能奏效。哦,还要确保你在运行命令时处于语音通道中。

最新更新