【发布时间】:2020-10-29 21:42:30
【问题描述】:
大家好,
我现在正在尝试创建自己的音乐机器人。
但是我有一些问题,希望你能帮助我:)
我的代码是什么样的:
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.
有趣的是,我每次使用机器人时都不会收到此错误:(
第二个问题是每次我想更改歌曲时都需要断开机器人的连接。
如何使用队列之类的东西? - 我不想每次都断开机器人...
谢谢!
【问题讨论】:
标签: python discord discord.py discord.py-rewrite