title: Discord.py Music player loop
body: <p>i'm looking for advice on how to make a player loop process that takes The songs from my queue and automatically plays them. The ones that ive tried making are only possible if the bot is used in 1 server. ive got alot more than 1 server that my bot is in. Im just looking for some advice on where to start </p>
  
  <p>my code right now.</p>
  
  <pre class="lang-py prettyprint-override"><code>import wavelink
  import discord
  import asyncio
  import random
  from discord.ext import commands
  import humanize as h
  
  class MusicQueue(asyncio.Queue):
      def __init__(self, **kwargs):
          super().__init__(**kwargs)
          self._queue = []
          self.index = 0
          self.repeat_start = None
  
      def reset(self):
          while len(self._queue)-1 &gt; self.index:
              self._queue.pop()
          self.repeat_start = None
          #dont reset the index, keep the history
  
      def hard_reset(self):
          self._queue.clear()
          self.index = 0
          self.repeat_start = None
  
      def shuffle(self):
          if self.repeat_start is not None:
              n = self.repeat_start
          else:
              n = self.index
          shuffle = self._queue[n:]
          random.shuffle(shuffle)
          old = self._queue[:n]
          self._queue = old + shuffle
  
      def repeat(self):
          if self.repeat_start is not None:
              self.repeat_start = None
          else:
              self.repeat_start = self.index
  
      def _get(self):
          if self.repeat_start is not None:
              if len(self._queue) == 1:
                  # it doesnt seem to like it when only one item is in the queue, so dont increase the index
                  return self._queue[0]
              diff = self.index - self.repeat_start
              self.index += 1
              if len(self._queue) &lt;= self.index:
                  self.index = self.repeat_start
              return self._queue[diff]
  
          else:
              r = self._queue[self.index]
              self.index += 1
              return r
  
      def putleft(self, item):
          self._queue.insert(self.index+1, item)
  
      def empty(self):
          if self.repeat_start is not None:
              if len(self._queue) &lt;= self.index:
                  self.index = self.repeat_start
          return len(self._queue) &lt;= self.index
  
      @property
      def q(self):
          return self._queue[self.index:]
  
      @property
      def history(self):
          return self._queue[:self.index]
  
  class music(commands.Cog):
  
      def __init__(self, client):
          self.client = client
          self.queues = {}
  
          if not hasattr(client, 'wavelink'):
              self.client.wavelink : wavelink.Client = wavelink.Client(self.client)
  
          self.client.loop.create_task(self.start_nodes())
  
  
      async def start_nodes(self):
          await self.client.wait_until_ready()
  
          await self.client.wavelink.initiate_node(host='0.0.0.0',
                                                   port=2333,
                                                   rest_uri='http://0.0.0.0:2333',
                                                   password='youshallnotpass',
                                                   identifier='DRIZZI',
                                                   region='us_central')
  
      def getQueue(self,ctx):
  
          if not self.queues[ctx.guild.id]:
              self.queues[ctx.guild.id] = MusicQueue()
  
          queue = self.queues[ctx.guild.id]
  
          return queue
  
  
      @commands.command(name='connect')
      async def connect_(self, ctx, *, channel: discord.VoiceChannel=None):
          if not channel:
              try:
                  channel = ctx.author.voice.channel
              except AttributeError:
                  raise discord.DiscordException('No channel to join. Please either specify a valid channel or join one.')
  
          player = self.client.wavelink.get_player(ctx.guild.id)
          await ctx.send(f'Connecting to **`{channel.name}`**')
          await player.connect(channel.id)
  
      @commands.command()
      async def play(self, ctx, *, query: str):
  
          def convert_from_ms( milliseconds ):
              seconds, milliseconds = divmod(milliseconds,1000)
              minutes, seconds = divmod(seconds, 60)
              hours, minutes = divmod(minutes, 60)
              seconds = seconds
              return hours, minutes, seconds
  
          queue : MusicQueue = self.getQueue(ctx)
  
          tracks = await self.client.wavelink.get_tracks(f'ytsearch:{query}')
          song : wavelink.Track = tracks[0]
  
          if song.duration &gt; 600000:
              return await ctx.send("Song may not be more than 10 minutes")
  
  
          if not tracks:
              return await ctx.send('Could not find any songs with that query.')
  
          player = self.client.wavelink.get_player(ctx.guild.id)
          if not player.is_connected:
              await ctx.invoke(self.connect_)
  
          emb = discord.Embed(title = f'Added {str(song)} to the queue')
          emb.set_thumbnail(url=song.thumb)
          time=convert_from_ms(song.duration)
          emb.add_field(name='Duration', value=f'{time[1]}:{time[2]}')
          emb.add_field(name='Uploaded by', value=song.author)
  
          await ctx.send(embed=emb) 
          await queue.put((song,player))
  
  
  
  def setup(client):
      client.add_cog(music(client))```
  </code></pre>