Discord 音乐机器人 - 队列 (discord.py)
Posted
技术标签:
【中文标题】Discord 音乐机器人 - 队列 (discord.py)【英文标题】:Discord Music Bot - Queue (discord.py) 【发布时间】:2021-05-24 14:12:58 【问题描述】:我是一名初级程序员(至少在我看来是这样),我需要帮助在音乐机器人中实现队列。
目前,只有当其中有一首歌曲时,队列才能正常工作。如果有更多歌曲,则“递归”开始(在 = 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)
我试图以不同的方式解决这个问题,例如,引入第二个变量,但这并没有成功。如果有任何帮助,我将不胜感激。
【问题讨论】:
我注意到程序缺少此代码after = lambda c: await serverQueue(voice, message)
,但这并不能解决问题,显示错误SyntaxError: 'await' outside async function
。
根据您的机器人判断,您应该尝试使用discord.ext.commands
。它将真正帮助您管理机器人命令。
你能告诉我在哪里可以读到这个吗?
discordpy.readthedocs.io/en/latest/ext/commands/commands.html
【参考方案1】:
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()
【讨论】:
以上是关于Discord 音乐机器人 - 队列 (discord.py)的主要内容,如果未能解决你的问题,请参考以下文章
不断收到 DiscordAPIError:在 Discord.js 中的音乐机器人上使用 reconlx 的分页执行队列命令时出现未知交互
我的 Heroku Discord 机器人离线 - discord.js