【发布时间】:2021-10-27 00:57:40
【问题描述】:
我正在创建一个音乐机器人,并且正在创建队列。 我已经创建了检查队列中是否有东西的循环,如果有,请重现它,但是当机器人开始播放第二首歌曲时,我收到以下错误。
File "C:\Users\hmd4i\Desktop\BlueZoom\src\music.py", line 33, in siguiente
self.vc.play(discord.FFmpegPCMAudio.from_probe(url2, **FFMPEG_OPTIONS), after=lambda e: self.siguiente())
AttributeError: 'str' object has no attribute 'play'.
我的代码如下:
from discord.ext import commands
import youtube_dl
import ffmpeg
import ffmpy
import re
from urllib import parse, request
queue = []
class music(commands.Cog):
def __init__(self,client):
self.client = client
self.queue = queue
self.is_playing = False
self.vc = ""'''
async def siguiente(self):
if len(self.queue) > 0:
self.is_playing = True
self.vc = ""
for url in queue:
#vc = ctx.voice_client
FFMPEG_OPTIONS = {"before_options": "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5", "options":"-vn"}
YDL_OPTIONS = {"format":"bestaudio"}
#ctx.voice_client.stop()
with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(url, download=False)
url2 = info["formats"][0]["url"]
queue.pop(0)
#vc.play(await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS), after=lambda e: self.siguiente())
self.vc.play(discord.FFmpegPCMAudio.from_probe(url2, **FFMPEG_OPTIONS), after=lambda e: self.siguiente())
else:
self.is_playing = False
@commands.command(name="play", help="Plays a selected song from youtube")
async def p(self,ctx, *, search):
if ctx.author.voice is None:
await ctx.send("No estás en el canal qlo")
voice_channel = ctx.author.voice.channel
if ctx.voice_client is None:
await voice_channel.connect()
else:
await ctx.voice_client.move_to(voice_channel)
if len(self.queue) == 0:
vc = ctx.voice_client
FFMPEG_OPTIONS = {"before_options": "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5", "options":"-vn"}
YDL_OPTIONS = {"format":"bestaudio"}
ctx.voice_client.stop()
with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
query_string = parse.urlencode({'search_query': search})
html_content = request.urlopen('https://www.youtube.com/results?' + query_string)
search_results = re.findall(r"watch\?v=(\S{11})", html_content.read().decode())
url = (search_results[0])
info = ydl.extract_info(url, download=False)
url2 = info["formats"][0]["url"]
queue.append(url)
vc.play(await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS))
#source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
#vc.play(source)
member = ctx.author
await ctx.send(f'{member.mention} puso el temazo https://www.youtube.com/watch?v=' + search_results[0])
elif len(self.queue) > 0 and ctx.voice_client.is_playing:
query_string = parse.urlencode({'search_query': search})
html_content = request.urlopen('https://www.youtube.com/results?' + query_string)
search_results = re.findall(r"watch\?v=(\S{11})", html_content.read().decode())
url = (search_results[0])
queue.append(url)
await ctx.send(f"Se ha añadido https://www.youtube.com/watch?v={url}, a la cola ")
await self.siguiente()
@commands.command()
async def cola(self, ctx):
await ctx.send(queue)
@commands.command()
async def limpiar_cola(self, ctx):
global queue
queue.clear()
user = ctx.message.author.mention
await ctx.send(f"La cola ha sido limpiada por {user}")
def setup(client):
client.add_cog(music(client))'''
【问题讨论】:
标签: python visual-studio-code discord.py bots