Discord py-创建自己的音乐bot-错误代码

时间:2020-10-28 15:27:33

标签: python discord discord.py discord.py-rewrite

嗨,

我现在正在尝试创建自己的音乐机器人。

但是我有一些问题,希望您能帮助我:)

我的代码是什么样的

from discord import Game, Intents
from discord.ext import commands
from os import environ, listdir

from pafy import new
from discord import FFmpegPCMAudio

import asyncio
import urllib.parse, urllib.request, re

intents = Intents.all()
client = commands.Bot(command_prefix='?', intents=intents)

@client.event
async def on_ready():
    print(f'Bot is ready to go!')

@client.command(pass_context=True)
async def play(ctx, url):
    ffmpeg_opts = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}

    if not ctx.message.author.voice:
        await ctx.send("**Please connect to a voice channel!**")
        return
    
    channel = ctx.author.voice.channel
    voice = await channel.connect()
        
    await ctx.send(f':pushpin: Music bot connected to {channel}\n:mag_right: **Searching for** ``' + url + "``")
    video = new(url)
    audio = video.getbestaudio().url
    voice.play(FFmpegPCMAudio(audio, **ffmpeg_opts))
    voice.is_playing()

@client.command()
async def join(ctx):

    if not ctx.message.author.voice:
        await ctx.send("You are not connected to a voice channel!")
        return
    else:
        channel = ctx.message.author.voice.channel
        await ctx.send(f'Connected to ``{channel}``')

    await channel.connect()

@client.command()
async def leave(ctx):
    voice_client = ctx.message.guild.voice_client
    user = ctx.message.author.mention
    await voice_client.disconnect()
    await ctx.send(f'Disconnected from {user}')

client.run(TOKEN)

所以现在我遇到了两个主要问题:

  • 不再支持Youtube-dl,所以我切换到了pafy

但我仍然收到此错误

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: OSError: ERROR: Unable to extract JS player URL; please report this issue on https://yt-dl.org/bug . Make sure you are using the latest version; see  https://yt-dl.org/update  on how to update. Be sure to call youtube-dl with the --verbose flag and include its complete output.

有趣的是,我每次使用机器人时都不会收到此错误:(

第二个问题是,每当我要更改歌曲时,我都需要断开bot的连接。

如何使用类似队列的内容? -我不想每次都断开机器人的连接...

谢谢!

2 个答案:

答案 0 :(得分:0)

youtube-dl不再是一回事。作为替代,我建议将lavalinkwavelink库一起使用。它具有更多功能。

答案 1 :(得分:0)

首先卸载youtube-dl:

pip uninstall youtube-dl

现在我是怎么做到的:

Github上有一个新的Youtube-dl

如果您在Windows上,请使用以下命令进行安装:

pip install -U git+https://github.com/l1ving/youtube-dl

您已经完成了-有时我会收到类似的错误

错误:无法提取JS播放器URL

但是,如果发生这种情况,您只需要重新启动bot,它就可以再次工作