首页 > 解决方案 > 如何修复 Discord bot 的音乐播放错误?

问题描述

我正在为我自己的服务器编写一个机器人。我决定使用一个简单的 .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()

标签: pythondiscorddiscord.py

解决方案


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

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

我强烈建议不要降级该版本,因为您将与其他未修复的问题作斗争,因为旧版本已过时且未维护。

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

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


推荐阅读