'Play all songs in a queue
This is the code that I have:
@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)
I can create a separate queue for each server and add songs to it by the command:
-aq song_name
example queue:
Your current queue is {0: 'abcdefu', 1: 'stereo hearts', 2: 'shivers'}
I can play the songs in the queue with the command:
-pq 0 or -pq 1 or -pq 2
But the problem is that the bot only plays the one song and stops after it finishes, and I want the bot to play the next song after the current song finishes and keep going until the last song in the queue is played.
Please help me out with this....
Thanks In Advance!!!
Solution 1:[1]
Firstly, your dictionary ({0: 'abcdefu', 1: 'stereo hearts', 2: 'shivers'}) really can just be a list since the keys are basically just the indices.
Secondly, I don't have any experience with audio in discord.py but it seems like your pq function doesn't actually go to the next song. It calls the transformer function once and thats it. It seems that really all you have to do is just loop through the queue and play each song. Here is some psuedocode that could be helpful:
@commands.command()
async def play_queue(self,ctx,number=0):
for num in range(number,len(queue)):
song = queue[num]
# play the song
Defaulting number=0 would allow for the the entire queue to play if no number was specified.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Roopesh-J |
