Discord py - 创建我自己的音乐机器人 - 错误代码
Discord py - creating my own music bot - error code
大家好,
我现在正在尝试创建自己的音乐机器人。
但是我遇到了一些问题,希望你能帮助我:)
我的代码是什么样的:
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.
有趣的是,我每次使用机器人时都不会收到此错误:(
第二个问题是每次我想更改歌曲时都需要断开机器人。
如何使用队列之类的东西? - 我不想每次都断开机器人...
谢谢!
youtube-dl 已经不是东西了。作为替代方案,我建议使用 lavalink with wavelink 库。它有更多的功能。
首先卸载youtube-dl:
pip uninstall youtube-dl
现在我是怎么做到的:
Github
上有一个新的 Youtube-dl
如果您在 Windows 使用此命令安装它:
pip install -U git+https://github.com/l1ving/youtube-dl
大功告成 - 有时我会收到类似
的错误
ERROR: Unable to extract JS player URL
但如果发生这种情况,您只需重新启动机器人,它就会再次运行
大家好,
我现在正在尝试创建自己的音乐机器人。
但是我遇到了一些问题,希望你能帮助我:)
我的代码是什么样的:
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.
有趣的是,我每次使用机器人时都不会收到此错误:(
第二个问题是每次我想更改歌曲时都需要断开机器人。
如何使用队列之类的东西? - 我不想每次都断开机器人...
谢谢!
youtube-dl 已经不是东西了。作为替代方案,我建议使用 lavalink with wavelink 库。它有更多的功能。
首先卸载youtube-dl:
pip uninstall youtube-dl
现在我是怎么做到的:
Github
上有一个新的 Youtube-dl如果您在 Windows 使用此命令安装它:
pip install -U git+https://github.com/l1ving/youtube-dl
大功告成 - 有时我会收到类似
的错误ERROR: Unable to extract JS player URL
但如果发生这种情况,您只需重新启动机器人,它就会再次运行