音乐bot discord.py的命令

时间:2020-11-08 04:18:26

标签: python discord.py

当我尝试运行leaveplay命令时,出现此错误。 很长时间以来,我一直在寻找一些问题,但是在任何地方都找不到该错误该如何解决?

警告!所有代码和错误已在CET的17/11/2020 03.24更新。如果您想在写之前检查写的内容

加入和离开代码:

@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起作用(实际上它找到通道并进入),它给了我变量,但首先它给了我一个错误。然后出现一个超时错误,该错误会自动使漫游器退出呼叫,但这并不重要。

?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: 

代码on_voice_state_update:

@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

2 个答案:

答案 0 :(得分:0)

请不要在该函数中不允许输入任何值

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()

答案 1 :(得分:0)

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

grid_forget的成员变量import tkinter as tk class App(tk.Tk): def __init__(self): super().__init__() self.title("Testing") self.minsize(800, 600) self.swap_btn = tk.Button(self, text='swap canvas', command=self.swap_canvas) self.swap_btn.pack() self.canvas1 = tk.Canvas(self, bg="black") self.canvas2 = tk.Canvas(self, bg="blue") self.canvasses = [self.canvas2, self.canvas1] self.current_canvas = self.canvas1 self.current_canvas.pack(expand=True, fill=tk.BOTH) self.draw_some_stuff() def swap_canvas(self): self.current_canvas.pack_forget() self.current_canvas = self.canvasses.pop(0) self.canvasses.append(self.current_canvas) self.current_canvas.pack(expand=True, fill=tk.BOTH) def draw_some_stuff(self): self.canvas1.create_oval(200, 200, 300, 300, fill='blue') self.canvas2.create_rectangle(200, 200, 300, 300, fill='black') self.canvas1.create_oval(400, 200, 500, 300, fill='blue') self.canvas2.create_rectangle(400, 200, 500, 300, fill='black') window = App() #Declaring the window window.mainloop() 的类型为channel。它告诉您回溯中的错误。以某种方式,您需要确保prev中的None设置为非channel的值,或者至少要进行检查。

这是一个简单的检查:

prev

docs描述了为什么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()

变量channel的类型为None。它告诉您回溯中的错误。某种程度上,您需要确保player的值不是None,或者至少要进行检查。

player

docs描述了为什么None将是@client.command() async def leave(ctx): player = ctx.message.guild.voice_client if player is not None: await player.disconnect()

player的套接字错误可能是相同的问题。它失败了,也许是因为没有东西要留下了。