【问题标题】:Play all songs in a queue播放队列中的所有歌曲
【发布时间】:2022-01-01 09:25:34
【问题描述】:

这是我的代码:

@commands.command(pass_context=True, aliases= ["aq"])
async def add_queue(self, ctx, *, url):
  a = ctx.message.guild.id
  b = servers[a]
  global queue
  try: 
    b[len(b)] = url 
    user = ctx.message.author.mention
    await ctx.send(f'``{url}`` was added to the queue by {user}!')
  except:
    await ctx.send(f"Couldnt add {url} to the queue!")

@commands.command(pass_context=True, aliases= ["qp"], case_insensitive=True)
async def pq(self,ctx, number):
  a = ctx.message.guild.id
  b = servers[a]
  if int(number) in b:
    source = b[int(number)]
    self.cur_song_id = int(number)
    await ctx.send(f"**Now Playing:** {source}")
    await self.transformer(ctx, source)
    
async def transformer(self,ctx, url):
  player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True)
  if not ctx.message.author.voice:
    await ctx.send("You are not connected to a voice channel!")
    return
  elif ctx.voice_client and ctx.voice_client.is_connected():
    print('Already connected to voice')
    pass
  else:
    channel = ctx.message.author.voice.channel
    await ctx.send(f'Connected to ``{channel}``')
    await channel.connect()
  ctx.voice_client.play(player)

我可以为每个服务器创建一个单独的队列,并通过命令向其中添加歌曲:

-aq song_name

示例队列:

Your current queue is {0: 'abcdefu', 1: 'stereo hearts', 2: 'shivers'}

我可以使用命令播放队列中的歌曲:

-pq 0 or -pq 1 or -pq 2

但问题是机器人只播放一首歌曲并在完成后停止,我希望机器人在当前歌曲完成后播放下一首歌曲并继续播放直到播放队列中的最后一首歌曲。

请帮我解决这个问题......

提前致谢!!!

【问题讨论】:

  • 您的代码标签是否带有...单个空格?
  • 不,当我把它粘贴到这里时,格式搞砸了

标签: python asynchronous async-await discord.py ytdl


【解决方案1】:

所以,为了解决我的问题,我实现了这段代码并且它可以工作。

我将作为字典的队列传递给转换器函数,以及一个默认为 0 的数字(以便队列从头开始播放)。

并且在播放函数中使用after参数,我不断调用该函数并不断迭代数字,只要它小于队列的长度。

它会自动播放队列中的歌曲。

我知道此代码有效,但如果可以进行任何改进,我愿意接受建议。

async def transformer(self,ctx, number, que):
  player = await YTDLSource.from_url(que[number], loop=self.bot.loop,stream=True)
  await ctx.send(f"**Now Playing:** {que[number]}")
  ctx.voice_client.play(player, after=lambda e: asyncio.run_coroutine_threadsafe(self.transformer(ctx,number+1 , que),self.bot.loop) if number < len(que) else ctx.voice_client.pause())

谢谢!

【讨论】:

    【解决方案2】:

    首先,您的字典 ({0: 'abcdefu', 1: 'stereo hearts', 2: 'shivers'}) 实际上可以只是一个列表,因为键基本上只是索引。

    其次,我对discord.py 中的音​​频没有任何经验,但您的pq 功能似乎并没有真正转到下一首歌曲。它调用transformer 函数一次,仅此而已。看起来你真正需要做的只是遍历队列并播放每首歌曲。以下是一些可能有用的伪代码:

    @commands.command()
    async def play_queue(self,ctx,number=0):
      for num in range(number,len(queue)):
        song = queue[num]
        # play the song
    

    如果没有指定号码,默认 number=0 将允许整个队列播放。

    【讨论】:

    • 嗨,首先,很抱歉这么晚才回复,笔记本电脑坏了。无论如何,我以前尝试过这样做。问题是循环不会等待第一首歌曲完成,它只是在第一首歌曲仍在播放时增加迭代器,这会给出一个错误,提示音频已在播放。
    • 嗯,我明白了。 play 函数有一个参数叫做after(discordpy.readthedocs.io/en/latest/…)。它旨在用于错误处理,但它似乎通常用于重复一首歌曲或播放另一首歌曲。另请查看此post。它显示了一种非常简单的方法来使用适合您情况的after 参数
    • 是的,那天晚上我真的想通了!感谢您努力提供帮助!真的很感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-05
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    相关资源
    最近更新 更多