音乐机器人discord.py的命令

2024-10-04 01:32:32 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图运行leaveplay命令时,我得到了这个错误。 我一直在寻找一些问题很长一段时间,但我没有发现这个错误在任何地方我如何才能修复它

警告!所有代码和错误已于2020年11月17日03.24 CET更新。如果要检查之前写入的内容,请转到编辑

加入和离开代码:

@client.command()
async def join(ctx):
    v_channel = ctx.message.author.voice.channel
    print(v_channel)
    if v_channel:
        await v_channel.connect()

@client.command()
async def leave(ctx):
    player = ctx.message.guild.voice_client
    print(player)
    if player is not None:
        await player.disconnect()

我把print ()放进去看看我身上到底出了什么事。Join在我离开时起作用(实际上它找到通道并进入),它给我变量,但首先它给我一个错误。然后有一个超时错误,它会自动使bot退出调用,但这并不重要

🔊Chiamate Generali 1🔊 #print(v_channel)
Task exception was never retrieved
future: <Task finished name='Task-16' coro=<VoiceClient._create_socket() done, defined at C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\voice_client.py:172> exception=gaierror(11001, 'getaddrinfo failed')>
Traceback (most recent call last):
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\voice_client.py", line 191, in _create_socket
    self.endpoint_ip = socket.gethostbyname(self.endpoint)
socket.gaierror: [Errno 11001] getaddrinfo failed
<discord.voice_client.VoiceClient object at 0x05BE5658> #print(player)
Ignoring exception in command join:
Traceback (most recent call last):
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:/Users/PC GIUSEPPE/PycharmProjects/LMIIBot Development/LMIIBot Development.py", line 41, in join
    await v_channel.connect()
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\abc.py", line 1080, in connect
    await voice.connect(reconnect=reconnect)
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\voice_client.py", line 218, in connect
    await self.start_handshake()
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\voice_client.py", line 154, in start_handshake
    await asyncio.wait_for(self._handshake_complete.wait(), timeout=self.timeout)
  File "C:\Users\PC GIUSEPPE\AppData\Local\Programs\Python\Python38-32\lib\asyncio\tasks.py", line 490, in wait_for
    raise exceptions.TimeoutError()
asyncio.exceptions.TimeoutError

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

Traceback (most recent call last):
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TimeoutError: 

语音状态更新代码:

@client.event
async def on_voice_state_update(member, prev, cur):
    print(member)
    print(prev)
    print(cur)
    if prev.channel is not None and client.user in prev.channel.members and len([m for m in prev.channel.members if not m.bot]) == 0:
        channel = discord.utils.get(client.voice_clients, channel=prev.channel)
        print(channel)
        await channel.disconnect()

我对上面的命令做了同样的事情。以下是错误:

LMII Bot Development#9553 #print(member)
<VoiceState self_mute=False self_deaf=False self_stream=False channel=None> #print(prev)
<VoiceState self_mute=False self_deaf=False self_stream=False channel=<VoiceChannel id=638017907791626250 name='🔊Chiamate Generali 1🔊' position=14 bitrate=128000 user_limit=0 category_id=637626960599842825>> #print(cur)
Task exception was never retrieved
future: <Task finished name='Task-15' coro=<VoiceClient._create_socket() done, defined at C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\voice_client.py:172> exception=gaierror(11001, 'getaddrinfo failed')>
Traceback (most recent call last):
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\voice_client.py", line 191, in _create_socket
    self.endpoint_ip = socket.gethostbyname(self.endpoint)
socket.gaierror: [Errno 11001] getaddrinfo failed
Peppe Grasso#9737 #print(member)
<VoiceState self_mute=False self_deaf=False self_stream=False channel=<VoiceChannel id=638017907791626250 name='🔊Chiamate Generali 1🔊' position=14 bitrate=128000 user_limit=0 category_id=637626960599842825>> #print(prev)
<VoiceState self_mute=False self_deaf=False self_stream=False channel=None> print(cur)
<discord.voice_client.VoiceClient object at 0x05454670> print(channel)

播放代码:

ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }],
}

def endSong(guild, path):
    os.remove(path)

@client.command(pass_context=True)
async def play(ctx, url):
    if not ctx.message.author.voice:
        await ctx.send('You are not connected to a voice channel') #message when you are not connected to any voice channel
        return

    else:
        channel = ctx.message.author.voice.channel
    print(channel)
    voice_client = await channel.connect()
    print(voice_client)
    guild = ctx.message.guild

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        file = ydl.extract_info(url, download=True)
        path = str(file['title']) + "-" + str(file['id'] + ".mp3")

    voice_client.play(discord.FFmpegPCMAudio(path), after=lambda x: endSong(guild, path))
    voice_client.source = discord.PCMVolumeTransformer(voice_client.source, 1)

    await ctx.send(f'**Music: **{url}') #sends info about song playing right now

错误:

ERROR: MYAKzSEKe-g: YouTube said: Unable to extract video data
Ignoring exception in command play:
Traceback (most recent call last):
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\youtube_dl\YoutubeDL.py", line 797, in extract_info
    ie_result = ie.extract(url)
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\youtube_dl\extractor\common.py", line 532, in extract
    ie_result = self._real_extract(url)
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\youtube_dl\extractor\youtube.py", line 1909, in _real_extract
    raise ExtractorError(
youtube_dl.utils.ExtractorError: MYAKzSEKe-g: YouTube said: Unable to extract video data

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:/Users/PC GIUSEPPE/PycharmProjects/LMIIBot Development/LMIIBot Development.py", line 80, in play
    file = ydl.extract_info(url, download=True)
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\youtube_dl\YoutubeDL.py", line 820, in extract_info
    self.report_error(compat_str(e), e.format_traceback())
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\youtube_dl\YoutubeDL.py", line 625, in report_error
    self.trouble(error_message, tb)
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\youtube_dl\YoutubeDL.py", line 595, in trouble
    raise DownloadError(message, exc_info)
youtube_dl.utils.DownloadError: ERROR: MYAKzSEKe-g: YouTube said: Unable to extract video data

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

Traceback (most recent call last):
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\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: DownloadError: ERROR: MYAKzSEKe-g: YouTube said: Unable to extract video data

Tags: inpyselfliblinechannelusersfile
2条回答

请不要允许函数中没有值

async def on_voice_state_update(member, prev, cur):
    if client.user in prev.channel.members and len([m for m in prev.channel.members if not m.bot]) == 0:
        channel = discord.utils.get(client.voice_clients, channel=prev.channel)
        if channel:
            await channel.disconnect()

@client.command()
async def join(ctx):
    v_channel = ctx.message.author.voice.channel
    if v_channel:
        await v_channel.connect()

@client.command()
async def leave(ctx):
    player = ctx.message.guild.voice_client
    if player:
        await player.disconnect()

编辑:只需更新所有依赖项

prev的成员变量channel的类型为None。它告诉你回溯中的错误。不知何故,您需要确保prevchannel被设置为不是None的值,或者至少检查一下

这是一个简单的检查:

@client.event
async def on_voice_state_update(member, prev, cur):
    if prev.channel is not None and client.user in prev.channel.members and len([m for m in prev.channel.members if not m.bot]) == 0:
        channel = discord.utils.get(client.voice_clients, channel=prev.channel)
        await channel.disconnect()

docs描述了为什么channel会是None

变量player的类型为None。它告诉你回溯中的错误。不知何故,您需要确保player不是None的值,或者至少检查一下

@client.command()
async def leave(ctx):
    player = ctx.message.guild.voice_client
    if player is not None:
        await player.disconnect()

docs描述了为什么player会是None

leave()的套接字错误可能也是同样的问题。它失败了,也许是因为没有什么可以留下的

相关问题 更多 >