gpt4 book ai didi

python-3.x - discord.py 音乐机器人 : How to Combine a Play and Queue Command

转载 作者:行者123 更新时间:2023-12-03 21:21:27 24 4
gpt4 key购买 nike

您好,我正在尝试创建一个音乐机器人,但我想要它,这样我就可以使用相同的命令播放音乐并将其他音乐添加到队列中。我试过这样做,但我无法让它工作。这是 play 和 queue 是两个单独命令的代码:

@bot.command(pass_context=True)
async def join(ctx):
await bot.join_voice_channel(bot.get_channel('487315853482786820'))


@bot.command(pass_context=True)
async def leave(ctx):
voice_client = bot.voice_client_in(ctx.message.server)
await voice_client.disconnect()


players = {}
queues = {}


def check_queue(id):
if queues[id] != []:
player = queues[id].pop(0)
players[id] = player
player.start()


@bot.command(pass_context=True)
async def play(ctx, url):
server = ctx.message.server
voice_client = bot.voice_client_in(server)
player = await voice_client.create_ytdl_player(url, after=lambda: check_queue(server.id))
players[server.id] = player
player.start()


@bot.command(pass_context=True)
async def queue(ctx, url):
server = ctx.message.server
voice_client = bot.voice_client_in(server)
player = await voice_client.create_ytdl_player(url, after=lambda: check_queue(server.id))

if server.id in queues:
queues[server.id].append(player)
else:
queues[server.id] = [player]
await bot.say('Video queued.')


@bot.command(pass_context=True)
async def pause(ctx):
id = ctx.message.server.id
players[id].pause()


@bot.command(pass_context=True)
async def stop(ctx):
id = ctx.message.server.id
players[id].stop()


@bot.command(pass_context=True)
async def resume(ctx):
id = ctx.message.server.id
players[id].resume()

最佳答案

您需要某种队列系统,其中歌曲请求存储在队列中,并且需要一个循环来检查队列中是否有任何项目,如果有,则抓取队列中的第一个项目。如果队列中没有歌曲,则循环等待直到添加歌曲。您可以使用 asyncio.Queue()asyncio.Event()去做这个。

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')

关于python-3.x - discord.py 音乐机器人 : How to Combine a Play and Queue Command,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53605422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com