Python Discord Queue for youtube dl


class BOT(object):
def __init__(self, client):
self.client = client
player = None
vc = None
async def play(self, url):
if self.player is None:
channel = self.client.get_channel("id")
self.vc = await self.client.join_voice_channel(channel)
self.player = await self.vc.create_ytdl_player(url, after=lambda: play_next(client))
self.player.start()
def play_next(self):
asyncio.run_coroutine_threadsafe(play(self.client, nexturl), client.loop)

client = discord.Client(commands_prefix="!")

def run_bot():
client.run("token")

@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
bot = BOT(client)
@client.event
async def on_message(message):
await bot.play("https://www.youtube.com/watch?v=bpOSxM0rNPM")

run_bot()

所以我有这段代码,但是我不知道如何从这里继续制作它,以便我可以对下一个 url 进行排队,以便它一个接一个地播放。我尝试使用队列,但它总是失败!

哇,11 个月没有人回答,我想这真的很简单,你需要有两个 url 和歌曲名称的 asyncio 队列,你可以看看我是如何安排我的机器人来做的,它工作得很好:

@bot.command(pass_context=True)
async def basta(ctx):
global bot_voice_status
await bot.say(":pause_button: OK, i'm leaving the voice channel, ciao")
player.stop()
await vc.disconnect()
bot_voice_status = 0
@bot.command(pass_context=True)
async def skip(ctx):
global player
if songs_url_queue.qsize != 0:
player.stop()
@bot.command(pass_context=True)
async def song(ctx, *, content:str):
server = discord.Server(id="") #put server id
song_name = ""
if str(content).find("https://www.youtube.com") == -1:
song_id= await Get_Song_Id(str(content))
url = "https://www.youtube.com/watch?v=" + song_id
if url == None:
await bot.say("Song not found")
return
else:
url = str(content)
author = ctx.message.author
if(author != "St3veB0T"):
if songs_url_queue.qsize() == 0 and not bot.is_voice_connected(server):
await songs_url_queue.put(url)
song_name = await Get_Song_Name(str(content))
mess = "<:youtube:568148804084170753> **Searching** :mag_right: `"+ str(content) + "`n:musical_note: **Now playing** `" + song_name + "` :notes: Use !basta for stopping and !skip for next song"  
await bot.say(mess)
else:
await songs_url_queue.put(url)
song_name = await Get_Song_Name(str(content))
await songs_names_queue.put(song_name)
mess = "<:youtube:568148804084170753> **Searching** :mag_right: `"+ str(content) + "`n:musical_note: **Added** `" + song_name + "` to play next :notes: Use !basta for stopping and !skip for next song"  
await bot.say(mess)
voice_channel = author.voice_channel
global vc
global bot_voice_status
bot_voice_status = 1
if songs_url_queue.qsize() == 1:
try:
vc = await bot.join_voice_channel(voice_channel)
except: 
bot.say("I'm already in a voice channel")
return
global player
options = "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 2"
while songs_url_queue.qsize() != 0:
if songs_names_queue.qsize() >= 1:
await bot.say(":musical_note: **Now playing** `" + str(await songs_names_queue.get()) + "` :notes: Use !basta for stopping and !skip for next song")
try:
player = await vc.create_ytdl_player(str(await songs_url_queue.get()), before_options=options)
except:
await vc.disconnect()
bot_voice_status = 0
return
player.start()
while not player.is_done():
await asyncio.sleep(1)
player.stop()
await vc.disconnect()
bot_voice_status = 0

在脚本的第一部分,添加以下行:

songs_url_queue = asyncio.Queue()
songs_names_queue = asyncio.Queue()

而且我想你不需要全局变量bot_voice_status,如果你不想对其他函数进行某种检查,看看机器人是否正在播放歌曲。 顺便说一句,如果你想要它,只需在机器人不播放时将其设置为 0,在播放时将其设置为 1。

bot_voice_status = 0

最新更新