【问题标题】:Adding a queue command to discord.py music bot向 discord.py 音乐机器人添加队列命令
【发布时间】:2021-12-19 03:24:11
【问题描述】:

我正在寻找一种将以下内容添加到我的音乐机器人的方法:

  • 队列功能,允许在播放另一首歌曲时输入歌曲。这将在第一首歌曲结束后通过播放功能
  • 能够显示此队列
  • 从队列中移除歌曲/更改歌曲位置

我该怎么做呢?下面是我的代码。

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

【问题讨论】:

  • 欢迎来到 SO!该站点通常用于您可能遇到或需要解决的特定代码错误或错误,因此如果此问题已关闭,请不要担心。要添加队列,您可能想要存储歌曲列表,并遍历它们(for loop 或 iter.next())。队列对于永久存储来说并不是很重要,但是如果你想保证它的安全,请查看 json 模块,它与 open() 函数结合使用时,可以让你将字典写入硬盘上的文件驱动器,用于永久存储。
  • 大声笑,你的机器人会因为使用 YTDL 而被删除。万事如意
  • 该机器人正在我和我的朋友的一台个人服务器中使用。
  • queue 必须是 global 列表 - 这样所有函数都可以访问队列

标签: python discord discord.py


【解决方案1】:

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

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

这种方式是创建队列并仅播放下一个但仍然无法显示或更改队列 Need help Discord bot queue

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-24
    • 2021-02-07
    • 2021-02-21
    • 2019-05-05
    • 2021-05-05
    • 2021-11-11
    • 1970-01-01
    相关资源
    最近更新 更多