title: (Python) Discord bot code returns "RuntimeError: Cannot close a running event loop"
body: <p>I was trying to create code in Spyder to start my discord bot, but I encountered the following error:</p>
  <p>Traceback (most recent call last):</p>
  <p>File &quot;&quot;, line 1, in 
  runfile('C:/Users/Nathan/Desktop/HW/Python stuff/my_discord_bot.py', wdir='C:/Users/Nathan/Desktop/HW/Python stuff')</p>
  <p>File &quot;C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py&quot;, line 827, in runfile
  execfile(filename, namespace)</p>
  <p>File &quot;C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py&quot;, line 110, in execfile
  exec(compile(f.read(), filename, 'exec'), namespace)</p>
  <p>File &quot;C:/Users/Nathan/Desktop/HW/Python stuff/my_discord_bot.py&quot;, line 17, in 
  client.run('my application token')</p>
  <p>File &quot;C:\ProgramData\Anaconda3\lib\site-packages\discord\client.py&quot;, line 705, in run
  _cleanup_loop(loop)</p>
  <p>File &quot;C:\ProgramData\Anaconda3\lib\site-packages\discord\client.py&quot;, line 98, in _cleanup_loop
  loop.close()</p>
  <p>File &quot;C:\ProgramData\Anaconda3\lib\asyncio\selector_events.py&quot;, line 83, in close
  raise RuntimeError(&quot;Cannot close a running event loop&quot;)</p>
  <p>RuntimeError: Cannot close a running event loop</p>
  <p>I've searched online and someone told me to use a package called &quot;nest_asyncio&quot;. I pip installed the package, imported it, and ran nest_asyncio.apply() in the console, but the code still gave the same error after running it again.</p>
  <p>This was my code:</p>
  <pre><code>import discord
  from discord.ext import commands
  
  client = commands.Bot(command_prefix = '.')
  
  @client.event
  async def on_ready():
      print ('Bot is ready.')
      
  client.run('my application token')
  </code></pre>
  <p>By the way, I replaced my actual token with &quot;my application token&quot; because I don't want to share it with anyone.</p>
  <p>Please help. How do I get rid of this error and get it to run like normal?</p>