【发布时间】:2021-11-17 02:23:07
【问题描述】:
每当我运行下面的代码时,我都会收到此错误“RuntimeError:无法关闭正在运行的事件循环”,尝试了许多解决方案,但都没有奏效。
我正在使用 jupyter 笔记本,
print(discord.__version__) --> 1.7.3
提前致谢。
import nest_asyncio
nest_asyncio.apply()
import discord
import os
from dotenv import load_dotenv
# https://stackoverflow.com/questions/63530888/how-would-i-go-about-creating-an-env-file-for-my-discord-bot-token
client = discord.Client()
@client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == message.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
TOKEN = os.getenv("token")
client.run('TOKEN')
代码输出:
任务异常在未来永远不会被检索到: exception=LoginFailure('不正确的令牌已通过。')> Traceback (最近一次通话最后):文件 “C:\Users\khalid\anaconda3\lib\site-packages\discord\http.py”,行 300,在 static_login data = await self.request(Route('GET', '/users/@me')) 文件“C:\Users\khalid\anaconda3\lib\site-packages\discord\http.py”,行 254, 应要求 raise HTTPException(r, data) discord.errors.HTTPException: 401 Unauthorized (error code: 0): 401: Unauthorized
上述异常是以下异常的直接原因:
Traceback(最近一次调用最后一次):文件 “C:\Users\khalid\anaconda3\lib\asyncio\tasks.py”,第 280 行,在 __step 结果 = coro.send(None) 文件“C:\Users\khalid\anaconda3\lib\site-packages\discord\client.py”,行 702,在亚军 等待 self.start(*args, **kwargs) 文件“C:\Users\khalid\anaconda3\lib\site-packages\discord\client.py”,行 665,开始 等待 self.login(*args, bot=bot) 文件“C:\Users\khalid\anaconda3\lib\site-packages\discord\client.py”,行 511,在登录 等待 self.http.static_login(token.strip(), bot=bot) 文件“C:\Users\khalid\anaconda3\lib\site-packages\discord\http.py”,行 304,在静态登录 raise LoginFailure('Improper token has been passed.') from exc discord.errors.LoginFailure: Improper token has been pass.
RuntimeError Traceback (most recent call last)
<ipython-input-10-ccfa19a2810e> in <module>
26 TOKEN = os.getenv("token")
27
---> 28 client.run('TOKEN')
29
30
~\anaconda3\lib\site-packages\discord\client.py in run(self, *args, **kwargs)
717 future.remove_done_callback(stop_loop_on_completion)
718 log.info('Cleaning up tasks.')
--> 719 _cleanup_loop(loop)
720
721 if not future.cancelled():
~\anaconda3\lib\site-packages\discord\client.py in _cleanup_loop(loop)
93 finally:
94 log.info('Closing the event loop.')
---> 95 loop.close()
96
97 class _ClientEventTask(asyncio.Task):
~\anaconda3\lib\asyncio\selector_events.py in close(self)
87 def close(self):
88 if self.is_running():
---> 89 raise RuntimeError("Cannot close a running event loop")
90 if self.is_closed():
91 return
RuntimeError: Cannot close a running event loop
【问题讨论】:
-
请发布整个回溯。
标签: python discord discord.py