我正在创建一个音乐机器人,并且正在创建队列。 我已经创建了循环来检查队列中是否有某些内容,如果有,则重现它,但是当机器人播放第二首歌曲时,我收到以下错误。
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))'''
你好,这是我的队列命令希望它可以帮助你
async def check_queue(self, ctx):
if len(self.song_queue[ctx.guild.id]) > 0:
ctx.voice_client.stop()
await self.play_song(ctx, self.song_queue[ctx.guild.id][0])
self.song_queue[ctx.guild.id].pop(0)
@commands.command()
@commands.cooldown(1, 5, commands.BucketType.user)
async def queue(self, ctx):
if await get_mute(ctx.author) != 0:
await ctx.send("You are blacklisted from the bot")
else: # display the current guilds queue
commandd = "queue"
print(f"{ctx.author.name}, {ctx.author.id} used command "+commandd+" used at ")
print(x)
print(" ")
if len(self.song_queue[ctx.guild.id]) == 0:
return await ctx.send("There are currently no songs in the queue.")
embed = discord.Embed(title="Song Queue", description="", colour=discord.Color.green().dark_gold())
i = 1
for url in self.song_queue[ctx.guild.id]:
embed.description += f"{i}) {url}\n"
i += 1
embed.set_footer(text="Thanks for using me!")
await ctx.send(embed=embed)
只要删除黑名单成员就可以了