运行discord.py机器人时在client.run('token')接收到运行时错误

时间:2020-01-23 23:16:22

标签: python google-colaboratory discord.py

我正在尝试用Python创建一个Discord机器人,该机器人根据您所在的班级(在我的学校中)在Discord服务器上扮演一个角色。我才刚开始,但是每当尝试运行它时都会收到错误消息(我在Python 3 Notebook中的Google Colab中运行它)。这是我的代码:

from datetime import date
import time
import discord

client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

client.run('my token (not shown for obvious reasons)')

starttime=time.time()
while True:
  currentTime = time.strftime("%H:%M")
  print("new minute")
  if 0 <= date(int(time.strftime("%Y")), int(time.strftime("%m")), int(time.strftime("%d"))).weekday() <= 4:
    if currentTime == "13:41":
      print("First hour has started!")
    elif currentTime == "13:45":
      print("First hour has started! (hs)")
    elif currentTime == "14:30":
      print("First hour has ended at high school.")
  time.sleep(60.0 - ((time.time() - starttime) % 60.0))

当我运行它时,它向我显示此错误:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-5-d40f2b4200ae> in <module>()
      9     print('We have logged in as {0.user}'.format(client))
     10 
---> 11 client.run('my token')
     12 
     13 starttime=time.time()

2 frames
/usr/local/lib/python3.6/dist-packages/discord/client.py in run(self, *args, **kwargs)
    570 
    571         try:
--> 572             loop.add_signal_handler(signal.SIGINT, lambda: loop.stop())
    573             loop.add_signal_handler(signal.SIGTERM, lambda: loop.stop())
    574         except NotImplementedError:

/usr/lib/python3.6/asyncio/unix_events.py in add_signal_handler(self, sig, callback, *args)
     92                             "with add_signal_handler()")
     93         self._check_signal(sig)
---> 94         self._check_closed()
     95         try:
     96             # set_wakeup_fd() raises ValueError if this is not the

/usr/lib/python3.6/asyncio/base_events.py in _check_closed(self)
    375     def _check_closed(self):
    376         if self._closed:
--> 377             raise RuntimeError('Event loop is closed')
    378 
    379     def _asyncgen_finalizer_hook(self, agen):

RuntimeError: Event loop is closed

如果我将client.run命令放在底部,程序将永远无法到达它,因为循环会阻止它到达命令。

我错过了什么吗?我不知道问题出在哪里。希望能有所帮助。

1 个答案:

答案 0 :(得分:0)

Jupyter笔记本是Google Colab所基于和使用的笔记本,它们具有自己的事件循环。
Client.run使用当前事件循环(如果未指定),并在完成运行后将其关闭。
在关闭事件循环之前,您应该已经能够运行一次,并且连续尝试再次运行它会告诉您很多信息。如果要在同一笔记本中多次运行事件循环,则需要使用Client.start并自己处理事件循环。