53448

How to Fix Runtime Error: Cannot close a running event loop - Python Discord Bot

<h3>Question</h3>

I am trying to create a Discord bot with Python, however whenever I run the sample code here:

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')

It returns the error:

Traceback (most recent call last): File "<ipython-input-6-ea5a13e5703d>", line 1, in <module> 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 <module> 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

Every other line seems to run just fine, but without the last line it doesn't connect to the server, which makes it useless.

Note: I have seen the question here, however OP's solution doesn't seem applicable to my situation.


<h3>Answer1:</h3>

(Spyder maintainer here) To run async code in our consoles you first need to install the nest_asyncio package and then call it before running any of your code as described on its Readme.


<h3>Answer2:</h3>

I encountered the same problem trying to run Discord examples on Jupyter Notebook. Moving to plain python script solved it for me.

来源:https://stackoverflow.com/questions/57639751/how-to-fix-runtime-error-cannot-close-a-running-event-loop-python-discord-bot

Recommend

  • jQuery blur keeps firing when two inputs are involved
  • How to get confusion matrix when using model.fit_generator
  • importing text files with variables into python
  • Update Stripe Credit Card with Coffeescript
  • Get a list of who has what access to git repositories
  • Decompress string in java from compressed string in C#
  • Auto-incrementation with HSQLDB (2.2.8) + DDLUtils
  • How to map a native query to POJO class using jpa and hibernate
  • Ways of filling 10 places with number from [1..10] such that digit at ith place has value atmost 1 m
  • Slice assignment to tensorflow variable
  • Year over Year Stats from a Crossfilter Dataset
  • Create an average of multiple excel chart without the data source
  • Neo4j…how to get a visual representation of my data?
  • how to run ejabberd with Erlang on Heroku?
  • How to use Streams api peek() function and make it work?
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • Google App Engine Datastore: Dealing with eventual consistency
  • Debug `Unexpected end of JSON input Error` on content script
  • Bad automatic Triangulation with Mayavi for coloring a surface known only by its corner
  • ssh remote server login script
  • How to call different template for different category archive page in woocommerce
  • Using Service Component Runtime
  • How to use FirstOrDefault inside Include
  • Firebase: How to read from external DB?
  • PHP Permalinks.. how to change?
  • Running R's aov() mixed effects model from Python using rpy2
  • How to handle a codeigniter PDF generator
  • ReferenceError: TextEncoder is not defined