1

I tried a few different codes but they all seem to be giving me the same error.

import discord

client = discord.Client()
client.run("TheBotTokenzzzInQuotes")

And it's giving me this error

File "<ipython-input-1-1f31c2ad1160>", line 1, in <module>
    runfile('C:/Users/Lenovo/Desktop/bot.py', wdir='C:/Users/Lenovo/Desktop')

      File "C:\Users\Lenovo\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
        execfile(filename, namespace)

      File "C:\Users\Lenovo\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
        exec(compile(f.read(), filename, 'exec'), namespace)

      File "C:/Users/Lenovo/Desktop/bot.py", line 4, in <module>
        client.run("token")

      File "C:\Users\Lenovo\Anaconda3\lib\site-packages\discord\client.py", line 637, in run
        _cleanup_loop(loop)

      File "C:\Users\Lenovo\Anaconda3\lib\site-packages\discord\client.py", line 97, in _cleanup_loop
        loop.close()

      File "C:\Users\Lenovo\Anaconda3\lib\asyncio\selector_events.py", line 83, in close
        raise RuntimeError("Cannot close a running event loop")

    RuntimeError: Cannot close a running event loop
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • Which version of Python are you running? – Kawaaii Apr 19 '20 at 19:49
  • Our IPython consoles already run an asyncio event loop, so I think you need to install and run [`nest-asyncio`](https://pypi.org/project/nest-asyncio/) before running your code. – Carlos Cordoba Apr 19 '20 at 20:50
  • Im using python 3.7.4 running it on spyder, anaconda. I installed and ran it but the code still isnt working – Anna Kallivayalil Apr 20 '20 at 03:59
  • 1
    I have the same problem directly after importing and applying `nest_asyncio`. Before that I got a different runtime error, that one about the loop already running. I found an answer that said Jupyter Notebook runs it's own event loop, and 2 cannot run together until `nest_asyncio` is installed. So I installed it, and now sit with a different but somehow clearer error. I still have no clue, so I am pinning a bounty on this question. – ProfK Jul 19 '20 at 14:28

1 Answers1

4

I have my own discord bot that I run, and the client variable for mine doesn't look anything like that if I'm being honest.

If you are not using "Discord.py Rewrite", that could be a possible reason for a failure.

Another possible reason(although very unlikely) is that you do not have aiohttp installed(which is the dependency asyncio needs for Discord.py Rewrite), you can install it by using the following command:

pip install aiohttp

If you would like an example bot to look at, I have listed one below that is directly from the developer of discord.py Rewrite(the version that is currently supported for python).

https://github.com/Rapptz/discord.py/blob/master/examples/basic_bot.py

If you were wondering what the beginning of my code looked like, I have it below this:

import discord
from discord.ext import commands
from discord.utils import get

client = commands.Bot(command_prefix = '>')