首页 > 解决方案 > 向 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

标签: pythondiscorddiscord.py

解决方案


首先你需要在你的命令之外创建一个空队列

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 队列


推荐阅读