我有一个适用于python discord bot的代码,它可以很好地使一台服务器中的音乐排队,但是当多台服务器使用它时,它会等到第一首请求的歌曲完成后再在另一台服务器上播放下一首歌曲。我想获得它,以便它可以在多台服务器上播放多首歌曲。
import asyncio
from discord.ext import commands
client = commands.Bot(command_prefix='!')
songs = asyncio.Queue()
play_next_song = asyncio.Event()
@client.event
async def on_ready():
print('client ready')
async def audio_player_task():
while True:
play_next_song.clear()
current = await songs.get()
current.start()
await play_next_song.wait()
def toggle_next():
client.loop.call_soon_threadsafe(play_next_song.set)
@client.command(pass_context=True)
async def play(ctx, url):
if not client.is_voice_connected(ctx.message.server):
voice = await client.join_voice_channel(ctx.message.author.voice_channel)
else:
voice = client.voice_client_in(ctx.message.server)
player = await voice.create_ytdl_player(url, after=toggle_next)
await songs.put(player)
client.loop.create_task(audio_player_task())
client.run('token')
答案 0 :(得分:1)
制作字典,并将密钥设置为server.id,将值设置为歌曲。 像这样:
client = commands.Bot(command_prefix='!')
songs = asyncio.Queue()
play_next_song = asyncio.Event()
queues = {} #new dictionary
@client.event
async def on_ready():
print('client ready')
async def audio_player_task():
while True:
play_next_song.clear()
current = await queues[id].get()
current.start()
await play_next_song.wait()
def toggle_next():
client.loop.call_soon_threadsafe(play_next_song.set)
@client.command(pass_context=True)
async def play(ctx, url):
ctx.message.server = server
queues[server.id] = songs
...
当您添加新歌曲时, 这样做:await queues [server.id] .put(player)