python让程序无法关闭,如何修复运行时错误:无法关闭正在运行的事件循环-Python Discord Bot...

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 "", line 1, in

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

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.

解决方案

(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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值