python-3.x - Discord 音乐机器人 - 队列 (discord.py)
问题描述
我是一名初学者程序员(或者在我看来是这样),我需要帮助在音乐机器人中实现队列。
目前,只有当其中有一首歌曲时,队列才能正常工作。如果有更多歌曲,则“递归”开始(在 = await serverQueue (voice, message) from def play 和 await play (queue.pop (0), voice, message) from queue)之后,所有歌曲都跳过。
import discord
import nacl
import ffmpeg
from discord import FFmpegPCMAudio
from discord.utils import get
from youtube_dl import YoutubeDL
client = discord.Client()
async def join(message):
##Connect to channel
connection = message.author.guild.voice_client
idUserChannel = message.author.voice.channel.id
idBotChannel = 0
if connection:
idBotChannel = client.voice_clients[0].channel.id
if (connection) and (idBotChannel != idUserChannel) :
await message.channel.send('**Moving to** ' + str(message.author.voice.channel))
await connection.move_to(message.author.voice.channel)
elif (idBotChannel != idUserChannel):
await message.channel.send('**Connect to** ' + str(message.author.voice.channel))
await message.author.voice.channel.connect()
async def play(video_link, voice, message):
##Playing songs
ydl_opts = {'format': 'bestaudio', 'noplaylist':'True'}
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
with YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(video_link, download = False)
print(info.get('title'))
URL = info['formats'][0]['url']
print(URL)
voice.play(FFmpegPCMAudio(URL, **FFMPEG_OPTIONS), after = await serverQueue(voice, message))
voice.is_playing()
await message.channel.send('**Now playing** - ' + info.get('title'))
async def skip(voice, message):
##Skip
voice.stop()
await serverQueue(voice, message)
await message.channel.send('**Successfully skiped** - ' + info.get('title'))
##Queue
queue = []
async def serverQueue(voice, message):
if queue != [] and not voice.is_playing():
await play(queue.pop(0), voice, message)
print('queue - ' + str(queue))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('&' + 'join'):
await join(message)
##Connect and play
if message.content.startswith('&' + 'play'):
await join(message)
voice = get(client.voice_clients, guild = message.channel.guild)
msg = message.content[1:].split()
video_link = msg[1]
if not voice.is_playing():
await play(video_link, voice, message)
else:
await message.channel.send('Added to queue successfully')
queue.append(video_link)
##Skip
if message.content.startswith('&' + 'skip'):
voice = get(client.voice_clients, guild = message.channel.guild)
await skip(voice, message)
我试图以不同的方式解决这个问题,例如,引入了第二个变量,但这并没有成功。如果有任何帮助,我将不胜感激。
解决方案
async def play(video_link, voice, message):
##Воспроизведение песенок
ydl_opts = {'format': 'bestaudio', 'noplaylist':'True'}
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
with YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(video_link, download = False)
print(info.get('title'))
URL = info['formats'][0]['url']
voice.play(FFmpegPCMAudio(URL, **FFMPEG_OPTIONS), after = lambda e: myAfter(voice, message))
voice.is_playing()
await message.channel.send('**Сейчас играет** - ' + info.get('title'))
async def myAfter(voice, message):
coro = await musicQueue(voice, message)
asyncio.run_coroutine_threadsafe(coro).result()
推荐阅读
- c# - Xamarin webview WebPage 无法下载文件 mSecurityInputMethodService 为空
- javascript - glsify - 错误'您可能需要适当的加载程序来处理此文件类型'?
- javascript - 在 JavaScript 中从中缀转换为前缀表示法
- spring - @EventListener(ApplicationReadyEvent.class) 只启动一种方法?
- kotlin - SonarQube 不显示输出
- python - 用户可以更新或删除他人创建的数据
- vector - 如何安全地从向量中删除项目?
- node.js - 如果我们使用集群,connect-mongo 将无法正常工作
- java - 如何在Java 8中同时按自然顺序按desc中的值和键对Map进行排序
- python - 用每列的特定值替换 NaN 值