title: How to Fix Runtime Error: Cannot close a running event loop - Python Discord Bot
body: <p>I am trying to create a Discord bot with Python, however whenever I run the sample code here:</p>
  
  <pre><code>import discord
  
  client = discord.Client()
  
  @client.event
  async def on_message(message):
      # we do not want the bot to reply to itself
      if message.author == client.user:
          return
  
      if message.content.startswith('!hello'):
          msg = 'Hello {0.author.mention}'.format(message)
          await client.send_message(message.channel, msg)
  
  @client.event
  async def on_ready():
      print('Logged in as')
      print(client.user.name)
      print(client.user.id)
      print('------')
  
  client.run('tokenhere')
  </code></pre>
  
  <p>It returns the error:</p>
  
  <pre><code>Traceback (most recent call last):
  
    File "&lt;ipython-input-6-ea5a13e5703d&gt;", line 1, in &lt;module&gt;
      runfile('C:/Users/User/Pictures/rito_bot.py', wdir='C:/Users/User/Pictures')
  
    File "C:\Users\User\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 703, in runfile
      execfile(filename, namespace)
  
    File "C:\Users\User\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
      exec(compile(f.read(), filename, 'exec'), namespace)
  
    File "C:/Users/User/Pictures/rito_bot.py", line 22, in &lt;module&gt;
      client.run('token')
  
    File "C:\Users\User\Anaconda3\lib\site-packages\discord\client.py", line 595, in run
      _cleanup_loop(loop)
  
    File "C:\Users\User\Anaconda3\lib\site-packages\discord\client.py", line 97, in _cleanup_loop
      loop.close()
  
    File "C:\Users\User\Anaconda3\lib\asyncio\selector_events.py", line 94, in close
      raise RuntimeError("Cannot close a running event loop")
  
  RuntimeError: Cannot close a running event loop 
  </code></pre>
  
  <p>Every other line seems to run just fine, but without the last line it doesn't connect to the server, which makes it useless. </p>
  
  <p>Note: I have seen the question <a href="https://stackoverflow.com/questions/51862318/python-asyncio-runtimeerror-cannot-close-a-running-event-loop">here</a>, however OP's solution doesn't seem applicable to my situation.</p>