How to Fix Runtime Error: Cannot close a running event loop - Python Discord Bot
How to Fix Runtime Error: Cannot close a running event loop - Python Discord Bot
我正在尝试使用 Python 创建一个 Discord 机器人,但是每当我 运行 此处的示例代码时:
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')
它returns错误:
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
其他每一行似乎 运行 都很好,但如果没有最后一行,它就不会连接到服务器,这使它毫无用处。
注意:我看到了问题,但是OP的解决方案似乎并不适用于我的情况。
(此处为 Spyder 维护者)要在我们的控制台中 运行 异步代码,您首先需要安装 nest_asyncio 包,然后在 运行 任何代码之前调用它,如所述在其自述文件中。
我在尝试 运行 Jupyter Notebook
上的 Discord 示例时遇到了同样的问题。转向普通 python 脚本为我解决了这个问题。
我正在尝试使用 Python 创建一个 Discord 机器人,但是每当我 运行 此处的示例代码时:
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')
它returns错误:
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
其他每一行似乎 运行 都很好,但如果没有最后一行,它就不会连接到服务器,这使它毫无用处。
注意:我看到了问题
(此处为 Spyder 维护者)要在我们的控制台中 运行 异步代码,您首先需要安装 nest_asyncio 包,然后在 运行 任何代码之前调用它,如所述在其自述文件中。
我在尝试 运行 Jupyter Notebook
上的 Discord 示例时遇到了同样的问题。转向普通 python 脚本为我解决了这个问题。