discord bot next song python

Solutions on MaxInterview for discord bot next song python by the best coders in the world

showing results for - "discord bot next song python"
Gaia
16 Mar 2020
1#this may not be the most optimal way to do it.
2
3#initializiation. this where I placed my variables.
4def __init__(self, client):
5    self.client = client
6    self.queue = []
7    self.position= 0
8#have a playback function.
9async def start_playback(self,ctx):
10    while (len(self.queue)) > self.position:#will continue looping until the queue is exhausted.
11      await asyncio.sleep(5)#lessens the lag imo
12      if not ctx.voice_client.is_playing():
13        self.position+=1
14        FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_delay_max 5', 'options': '-vn'}
15        source = self.queue[self.position]
16        source1= await discord.FFmpegOpusAudio.from_probe(source,**FFMPEG_OPTIONS)
17        ctx.voice_client.play(source1)
18    if (len(self.queue)) < self.position:#once queue is empty it clears the list and resets it
19      self.queue.clear()
20      self.position=0
21      
22# the play command 
23@commands.command()
24async def play(self,ctx, *,query: t.Optional[str]):
25    voice_channel= ctx.author.voice.channel
26    embed = discord.Embed()
27    if ctx.voice_client is None:
28      await voice_channel.connect()
29    #FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_delay_max 5', 'options': '-vn'}
30    YDL_OPTIONS = {'format':"bestaudio"}
31    IsString=False
32    query = query.strip("<>")
33    if not re.match(URL_REGEX, query):
34      query1 = f" {query}"
35      query = f"ytsearch:{query}"
36      IsString=True
37      await ctx.send(f"Searching for{query1}....")
38
39    with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
40      if IsString==True:
41        info = ydl.extract_info(query, download=False, ie_key='YoutubeSearch')
42        info1 = info['entries'][0]['webpage_url']
43        info2= ydl.extract_info(info1, download=False)
44        embed.description = f"Found \"[{info2.get('title', None)}]({info1})\" "
45        await ctx.send(embed=embed)
46        url2 = info2['formats'][0]['url']
47      else:
48        info = ydl.extract_info(query, download=False)
49        url2= info['formats'][0]['url']
50        embed.description = f"Found \"[{info.get('title', None)}]({query})\" "
51        await ctx.send(embed=embed)
52   	source= url2
53    await self.add_song(ctx, source)
54    if not ctx.voice_client.is_playing(): #if nothing is playing it will start the playback function
55      self.position-=1
56      await self.start_playback(ctx)
57
58