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以便它将一个接一个地播放。我曾尝试使用Queue,但总是失败!
答案 0 :(得分:0)
哇,有11个月了,没有人回答,我想这真的很简单,您需要为URL和歌曲名称设置两个异步队列,您可以看看我是如何安排我的机器人来做到这一点的,它工作得很好:
@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