【问题标题】:Discord bot "RuntimeError: Cannot close a running event loop" errorDiscord bot“RuntimeError:无法关闭正在运行的事件循环”错误
【发布时间】: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


【解决方案1】:

将最后一行从 client.run('TOKEN') 更改为 client.run(TOKEN)。您使用的是字符串 "TOKEN" 而不是变量 TOKEN

【讨论】:

  • 改了还是报错。
  • 我尝试运行您的代码并得到一个不同的错误 - “NoneType object can't be stripped”,但这是由于您从未运行 load_dotenv() 应该在尝试访问之前运行.env 文件中的值。如果您在文件开头运行它,它可能会起作用。
  • 不幸的是,它不起作用,我仍然得到同样的错误。
  • 这很奇怪 - 它对我来说运行良好。我唯一可以建议的另一件事是确保 .env 文件中的令牌是正确的。
  • 代码在在线 ide 上运行良好,在 jupyter notebook 上我得到“RunTimeError”。我错过了一些东西,我无法弄清楚它是什么。
猜你喜欢
  • 2019-12-29
  • 1970-01-01
  • 2021-01-25
  • 2020-08-02
  • 1970-01-01
  • 2022-10-30
  • 2020-06-23
  • 1970-01-01
  • 2019-10-02
相关资源
最近更新 更多