-1

这是我拥有的代码:

@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

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

这个你能帮我吗....

提前致谢!!!

4

2 回答 2

1

首先,您的字典 ( {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如果未指定编号,则默认设置将允许整个队列播放。

于 2021-11-23T18:41:58.483 回答
0

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

我将作为字典的队列传递给转换器函数,以及一个默认为 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())

谢谢!。

于 2021-12-03T06:39:41.057 回答