title: Discord.py bot + Quart: Trying to connect to a voice channel always gives a "task got future attached to a different loop"
body: <p>i've been trying to create a discord bot that can receive commands through a web interface. I'm using discord.py as a Discord API wrapper and Quart as a REST framework, because i need to handle asynchronous tasks and Flask doesn't support them.</p>
  <p>Right now i have two files:</p>
  <p><strong>app.py</strong></p>
  <pre><code>import discord
  
  bot = discord.Client(intents=discord.Intents.all())
  
  ...
  
  async def play_audio(audio_name, voiceChannel):
      vc = await voiceChannel.connect()
      print(&quot;test&quot;)
      vc.play(discord.FFmpegPCMAudio(source=&quot;audio\{}&quot;.format(audio_name), executable=FFMPEG_PATH))
      while vc.is_playing():
          time.sleep(.1)
      await vc.disconnect()
  
  async def get_online_voice_members():
      guild = bot.get_guild(NMC_GUILD_ID)
      online_voice_users = {}
      for voiceChannel in guild.voice_channels:
          for user in voiceChannel.members:
              online_voice_users[user] = voiceChannel
      return online_voice_users
  
  ...
  </code></pre>
  <p><strong>api.py</strong></p>
  <pre><code>import asyncio
  from quart import Quart
  import app as discord
  
  QUART_APP = Quart(__name__)
  
  @QUART_APP.before_serving
  async def before_serving():
      loop = asyncio.get_event_loop()
      await discord.bot.login(&quot;MY BOT TOKEN&quot;)
      loop.create_task(discord.bot.connect())
  
  ...
  
  @QUART_APP.route(&quot;/online_list&quot;, methods=[&quot;GET&quot;])
  async def get_online_members():
      resp = {}
      members = await discord.get_online_voice_members()
      for user in members.keys():
          resp[user.id] = {&quot;nick&quot; : user.nick, &quot;channel&quot; : members[user].id}
      return resp
  
  @QUART_APP.route(&quot;/goodnight&quot;, methods=[&quot;GET&quot;])
  async def send_goodnight():
      members = await discord.get_online_voice_members()
      for user in members.keys():
          if user.id == 12345:
              await discord.play_audio(&quot;goodnight.mp3&quot;, members[user])
              break
      return {&quot;response&quot;:&quot;OK&quot;}
  </code></pre>
  <p>When i make a GET request on the endpoint /online_list everything works fine, but when i make a request on /goodnight, the code successfully runs until reaching the instruction <code>await discord.play_audio(&quot;goodnight.mp3, members[user])</code>, which receives the correct parameters, but it always raises the following exception:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;G:\Gunther\venv\lib\site-packages\quart\app.py&quot;, line 1814, in handle_request
      return await self.full_dispatch_request(request_context)
    File &quot;G:\Gunther\venv\lib\site-packages\quart\app.py&quot;, line 1836, in full_dispatch_request
      result = await self.handle_user_exception(error)
    File &quot;G:\Gunther\venv\lib\site-packages\quart\app.py&quot;, line 1076, in handle_user_exception
      raise error
    File &quot;G:\Gunther\venv\lib\site-packages\quart\app.py&quot;, line 1834, in full_dispatch_request
      result = await self.dispatch_request(request_context)
    File &quot;G:\Gunther\venv\lib\site-packages\quart\app.py&quot;, line 1882, in dispatch_request
      return await handler(**request_.view_args)
    File &quot;G:/Dati HDD F/GitHub Projects/Gunther/api.py&quot;, line 59, in send_buonanotte
      await discord.play_audio(&quot;goodnight.mp3&quot;, members[user])
    File &quot;G:\Gunther\app.py&quot;, line 55, in play_audio
      vc = await voiceChannel.connect()
    File &quot;G:\Gunther\venv\lib\site-packages\discord\abc.py&quot;, line 1122, in connect
      await voice.connect(timeout=timeout, reconnect=reconnect)
    File &quot;G:\Gunther\venv\lib\site-packages\discord\voice_client.py&quot;, line 352, in connect
      self.ws = await self.connect_websocket()
    File &quot;G:\Gunther\venv\lib\site-packages\discord\voice_client.py&quot;, line 323, in connect_websocket
      await ws.poll_event()
    File &quot;G:\Gunther\venv\lib\site-packages\discord\gateway.py&quot;, line 893, in poll_event
      await self.received_message(json.loads(msg.data))
    File &quot;G:\Gunther\venv\lib\site-packages\discord\gateway.py&quot;, line 825, in received_message
      await self.initial_connection(data)
    File &quot;G:\Gunther\venv\lib\site-packages\discord\gateway.py&quot;, line 849, in initial_connection
      recv = await self.loop.sock_recv(state.socket, 70)
    File &quot;C:\Users\Kyles\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py&quot;, line 693, in sock_recv
      return await self._proactor.recv(sock, n)
  RuntimeError: Task &lt;Task pending name='Task-27' coro=&lt;ASGIHTTPConnection.handle_request() running at G:\Gunther\venv\lib\site-packages\quart\asgi.py:70&gt; cb=[_wait.&lt;locals&gt;._on_completion() at C:\Users\Kyles\AppData\Local\Programs\Python\Python38\lib\asyncio\tasks.py:507]&gt; got Future &lt;_OverlappedFuture pending overlapped=&lt;pending, 0x199c31f0ca0&gt;&gt; attached to a different loop
  </code></pre>
  <p>I guess i'm not understanding correctly how the asyncio library works, as it seems to me that no matter what i try, the line <code>vc = await voiceChannel.connect()</code> in app.py always ends up running on a different loop than the main one. Is there something i'm missing?</p>