我有一个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')
编一本字典,把键设为服务器id像歌曲一样有价值。 像这样:
当你添加新歌时, 这样做:等待排队[服务器id].put(播放器)
相关问题 更多 >
编程相关推荐