如何修复Discord bot的音乐播放错误?

时间:2020-03-13 11:03:30

标签: python discord discord.py

我正在为自己的服务器编写一个机器人。我决定通过一个简单的.play url命令来实现通过引用播放音乐的命令。我使用discord.py库,并使用音乐youtube_dl。我找到了一种方法,但是主库已经得到了进一步改进,该方法根本不起作用,因此会发生错误。剩下的错误很少,已经确定了两个错误。底线是discord.py似乎不想使用youtube_dl,或者我不了解错误的本质,或者我只是在做错什么。我什至不得不从discord.py导入VoiceClient函数。

错误:

Ignoring exception in command play:
Traceback (most recent call last):
  File "C:\Users\Степан\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:/Users/Степан/Desktop/Clown/main.py", line 57, in play
    player = await voice_client.create_ytdl_player(url)
AttributeError: 'VoiceClient' object has no attribute 'create_ytdl_player'

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

Traceback (most recent call last):
  File "C:\Users\Степан\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Степан\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Степан\AppData\Local\Programs\Python\Python38-32\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: AttributeError: 'VoiceClient' object has no attribute 'create_ytdl_player'

代码:

import discord
import config
import random
import variables
import youtube_dl
from discord import utils
from discord.ext import commands
from discord.voice_client import VoiceClient

client = commands.Bot(command_prefix='.')
players = {}


@client.command(pass_context=True)
async def play(ctx, url):
    channel = ctx.author.voice.channel
    await channel.connect()
    server = ctx.message.guild 
    voice_client = discord.utils.find(lambda c: c.guild.id == server.id, client.voice_clients)
    player = await voice_client.create_ytdl_player(url) # < Ошибка возникает тут
    players[server.id] = player
    player.start()

1 个答案:

答案 0 :(得分:2)

create_ytdl_player已在discord.py v1.0.0中删除。它不再维护,您不应该使用它。

您能告诉我然后可以使用什么,或者如何使用 降级discord.py版本?

我会强烈建议您不要降级该版本,因为您会遇到其他问题,这些问题由于较旧的版本已过时且未维护而无法解决。

许多主要的不和谐机器人都在使用Lavalink:https://github.com/Frederikam/Lavalink

Python也有许多不同的客户端库。做您想解决的研究。在新的StackOverflow问题中发布来自该问题的任何新问题。他们还有一个Discord服务器,值得一试。请参阅我链接的GitHub上的自述文件。