我一直在尝试排队,但没有成功。我可以播放youtube视频的音频,但是无法排队,发现的信息已经过时,需要帮助。 这是我的代码
import discord
from discord.ext import commands
from urllib import parse, request
import re
import asyncio
import youtube_dl
from discord.utils import get
from discord import FFmpegPCMAudio
from youtube_dl import YoutubeDL
bot = commands.Bot(command_prefix= '>', description = 'this is a helper bot')
#Ping
@bot.command()
async def ping(ctx):
await ctx.send('pong')
#Entrar
@bot.command(pass_context=True)
async def join(ctx):
if ctx.message.author.voice:
channel = ctx.message.author.voice.channel
await channel.connect()
#Salir
@bot.command(pass_context=True)
async def leave(ctx):
if ctx.message.author.voice:
server = ctx.message.guild.voice_client
await server.disconnect()
#Reproducir
@bot.command(brief="Plays a single video, from a youtube URL")
async def play(ctx, *, search):
query_string = parse.urlencode({'search_query': search})
html_content = request.urlopen('http://www.youtube.com/results?' + query_string)
search_results = re.findall( r"watch\?v=(\S{11})", html_content.read().decode())
#print(search_results)
await ctx.send('https://www.youtube.com/watch?v=' + search_results[0])
link = ('https://www.youtube.com/watch?v=' + search_results[0])
#print(link)
YDL_OPTIONS = {'format': 'bestaudio', 'noplaylist':'True'}
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
voice = get(bot.voice_clients, guild=ctx.guild)
if not voice.is_playing():
with YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(link, download=False)
await ctx.send(info.get('title'))
URL = info['formats'][0]['url']
voice.play(FFmpegPCMAudio(URL, **FFMPEG_OPTIONS))
voice.is_playing()
else:
await ctx.send("Already playing song")
return
#Pausa
@bot.command()
async def pausa(ctx):
if ctx.message.author.voice:
server = ctx.message.guild.voice_client
await server.pause()
#Resumir
@bot.command()
async def res(ctx):
if ctx.message.author.voice:
server = ctx.message.guild.voice_client
await server.resume()
#events
@bot.event
async def on_ready():
print('my bot is ready')
bot.run('Token')
该机器人无需下载音频就可以播放youtube音频,或者这是我一直在尝试的操作。