python - 向 discord.py 音乐机器人添加队列命令
问题描述
我正在寻找一种将以下内容添加到我的音乐机器人的方法:
- 队列功能,允许在播放另一首歌曲时输入歌曲。这将在第一首歌曲结束后通过播放功能
- 能够显示此队列
- 从队列中移除歌曲/更改歌曲位置
我该怎么做呢?下面是我的代码。
from discord.ext import commands
from dotenv import load_dotenv
from keep_alive import keep_alive
from youtube_dl import YoutubeDL
from discord.utils import get
load_dotenv()
TOKEN = os.getenv('TOKEN')
bot = commands.Bot(command_prefix='!', case_insensitive=True)
@bot.event
async def on_ready():
print(f'{bot.user} has successfully connected to Discord!')
@bot.command(aliases=['p'])
async def play(ctx, url: str = None):
YDL_OPTIONS = {'format': 'bestaudio/best', 'noplaylist':'True'}
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
voice = get(bot.voice_clients, guild=ctx.guild)
try:
channel = ctx.message.author.voice.channel
except AttributeError:
await ctx.send('Bruh, join a voice channel')
return None
if not voice:
await channel.connect()
await ctx.send("Music/Audio will begin shortly, unless no URL provided.")
voice = get(bot.voice_clients, guild=ctx.guild)
with YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(url, download=False)
I_URL = info['formats'][0]['url']
source = await discord.FFmpegOpusAudio.from_probe(I_URL, **FFMPEG_OPTIONS)
voice.play(source)
voice.is_playing()
await ctx.send("Playing audio.")
@bot.command()
async def pause(ctx):
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
if voice.is_playing():
voice.pause()
await ctx.send("Audio is paused.")
if voice.is_not_playing():
await ctx.send("Currently no audio is playing.")
@bot.command()
async def resume(ctx):
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
if voice.is_paused():
voice.resume()
await ctx.send("Resumed audio.")
if voice.is_not_paused():
await ctx.send("The audio is not paused.")
@bot.command()
async def stop(ctx):
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
voice.stop()
await ctx.send("Stopped playback.")
@bot.command(aliases=['l'])
async def leave(ctx):
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
await voice.disconnect()
await ctx.send("Left voice channel.")
keep_alive()
bot.run(TOKEN
解决方案
首先你需要在你的命令之外创建一个空队列
queues = {}
以及一个检查队列中是否有一首歌的功能,它将播放该歌曲
#check queue
queues = {}
def check_queue(ctx, id):
if queues[id] !={}:
voice = ctx.guild.voice_client
source = queues[id].pop(0)
voice.play(source, after=lambda x=0: check_queue(ctx, ctx.message.guild.id))
并在播放命令中创建一个函数,如果歌曲已经在播放,则将下一首歌曲添加到队列中,并添加检查队列函数
if voice.is_playing():
guild_id = ctx.message.guild.id
if guild_id in queues:
queues[guild_id].append(source)
else:
queues[guild_id] = [source]
else:
voice.play(source, after=lambda x=0: check_queue(ctx, ctx.message.guild.id))
这种方式是创建一个队列并只播放下一个但仍然无法显示或更改队列 需要帮助 Discord bot 队列
推荐阅读
- git - gitahead - HTTPS Credentials keeps popping up
- node.js - git hook post receive not found env: node: No such file or directory
- javascript - javascript mousedown 事件后不需要的行为
- c++ - 如何在此页面中提交来自 c++
- ubuntu - 如何将 .dash 段合并为一个文件并将其转换为 .mp4?
- css - Nativescript 中的 Font Awesome Pro
- .net - 使用 .NET 驱动程序将新文档添加到 MongDB 上的某个集合时如何获得通知?
- excel - 分组在同类群组中的数据集中唯一出现的计数
- vue.js - Vuejs2 - 如何从服务器更新整个对象而不失去反应性?
- php - 在 VPS 上部署 Laravel 应用程序时出现“内部服务器错误”