【问题标题】:How do I implement a queue system into my Discord Music Bot?如何在我的 Discord Music Bot 中实现队列系统?
【发布时间】:2021-11-07 00:18:15
【问题描述】:
@client.command()
async def play(ctx, *, url = None):
  if ctx.author.voice is None:
      msg = await ctx.send("You are not in a voice channel.")
      await msg.delete(delay = 3)
  voice_channel = ctx.author.voice.channel
  if ctx.voice_client is None:
      await voice_channel.connect()
  else:
      await ctx.voice_client.move_to(voice_channel)

  search_keyword = url
  if not ("youtube.com/watch?" in search_keyword):
    search_keyword = search_keyword.replace(" ", "+")
    html = urllib.request.urlopen(f"https://www.youtube.com/results?search_query={search_keyword}")
    video_ids = re.findall(r"watch\?v=(\S{11})", html.read().decode())
    url = str(f"https://www.youtube.com/watch?v={video_ids[0]}")
  
  ctx.voice_client.stop()
  FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
  YDL_OPTIONS = {'format': 'bestaudio', 'noplaylist':'True'}
  vc = ctx.voice_client

  with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
    info = ydl.extract_info(url, download=False)
    title = info.get('title', None)
    length = info['duration']
    url2 = info["formats"][0]["url"]
    source = await discord. FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
    vc.play(source)
  
  embed = discord.Embed(title = "Currently Playing", colour = discord.Colour.blue())
  embed.add_field(name = "Song", value = title, inline = False)
  embed.add_field(name = "Length", value = str(datetime.timedelta(seconds = length)), inline = False)
  embed.add_field(name = "Link", value = url, inline = False)
  msg = await ctx.send(embed=embed)
  await msg.add_reaction("\u23F8")
  await msg.add_reaction("\u25B6")
  await msg.add_reaction("\u23F9")
  while True:
    try:
      reaction, user = await client.wait_for("reaction_add", check=lambda reaction, user: user.id == ctx.author.id and reaction.message.id == msg.id and reaction.emoji in ["\u23F8", "\u25B6", "\u23F9"], timeout = length)
    
    except asyncio.TimeoutError:
      return await msg.clear_reactions()
        
    async def process():
      if reaction.emoji == "\u23F8":
        await msg.remove_reaction(reaction.emoji, ctx.author)
        ctx.voice_client.pause()
      elif reaction.emoji == "\u25B6":
        await msg.remove_reaction(reaction.emoji, ctx.author)
        ctx.voice_client.resume()
      elif reaction.emoji == "\u23F9":
        await msg.remove_reaction(reaction.emoji, ctx.author)
        ctx.voice_client.stop()

    asyncio.create_task(process())

这是我的播放命令的代码。现在,如果在播放歌曲时调用该命令,则该歌曲停止并开始新请求的歌曲,这是因为下载歌曲之前的ctx.voice_client.stop()。但是,我正在尝试实现一个队列系统,如果在播放另一首歌曲时调用该命令,它将被添加到队列中,并且队列中的歌曲将在当前歌曲结束后立即播放。目前我如何做到这一点的想法是用 if 语句替换 ctx.voice_client.stop() 来检查机器人的状态,如果它当前正在播放,或者将它附加到包含当前队列的全局列表变量中或播放歌曲。但是我对如何让它一个接一个地播放队列并实现一个跳过命令一无所知。另一个复杂的部分是将跳过命令实现为类似于我如何实现暂停和播放命令的反应。我现在不知道我的想法是否可行,因此欢迎任何意见。

【问题讨论】:

    标签: python 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:
        vc.play(source, after=lambda x=0: check_queue(ctx, ctx.message.guild.id))
    

    如果您需要更多帮助,可以在这里查看我的代码Need help Discord bot queue

    【讨论】:

      猜你喜欢
      • 2018-03-12
      • 2020-05-13
      • 2021-02-03
      • 1970-01-01
      • 2021-06-26
      • 2014-08-19
      • 1970-01-01
      • 2019-02-27
      • 2021-04-21
      相关资源
      最近更新 更多