如何修复Discord机器人的音乐播放错误



我正在为自己的服务器编写一个机器人程序。我决定用一个简单的.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СтепанAppDataLocalProgramsPythonPython38-32libsite-packagesdiscordextcommandscore.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СтепанAppDataLocalProgramsPythonPython38-32libsite-packagesdiscordextcommandsbot.py", line 892, in invoke
await ctx.command.invoke(ctx)
File "C:UsersСтепанAppDataLocalProgramsPythonPython38-32libsite-packagesdiscordextcommandscore.py", line 797, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:UsersСтепанAppDataLocalProgramsPythonPython38-32libsite-packagesdiscordextcommandscore.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()

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

你能告诉我你可以使用什么吗,好吧,或者如何降级不和谐.py版本?

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

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

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

最新更新