Python 运行我的discord.py bot时,在client.run(';令牌';)处收到运行时错误

Python 运行我的discord.py bot时,在client.run(';令牌';)处收到运行时错误,python,google-colaboratory,discord.py,Python,Google Colaboratory,Discord.py,我正在尝试用Python制作一个Discord机器人,它根据您所在的班级(在我的学校)在Discord服务器上为您提供一个角色。我刚刚开始,但每当我尝试运行它时,我都会收到一个错误(我在Python 3笔记本的Google Colab中运行它)。这是我的密码: from datetime import date import time import discord client = discord.Client() @client.event async def on_ready():

我正在尝试用Python制作一个Discord机器人,它根据您所在的班级(在我的学校)在Discord服务器上为您提供一个角色。我刚刚开始,但每当我尝试运行它时,我都会收到一个错误(我在Python 3笔记本的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))

from datetime导入日期
导入时间
进口不和
client=discord.client()
@客户端事件
_ready()上的异步定义:
打印('我们已以{0.user}的身份登录。格式(客户端))
client.run('我的令牌(由于明显的原因未显示)')
starttime=time.time()
尽管如此:
currentTime=time.strftime(“%H:%M”)
打印(“新分钟”)
if 0 572 loop.add_signal_处理程序(signal.SIGINT,lambda:loop.stop())
573 loop.add_signal_处理程序(signal.SIGTERM,lambda:loop.stop())
574除未实施错误外:
/添加信号处理程序中的usr/lib/python3.6/asyncio/unix\u events.py(self、sig、callback、*args)
92“带添加信号处理程序()”)
93自检信号(sig)
--->94自我检查关闭()
95尝试:
96#set#u wakeup_fd()如果不是正确的设置,则会引发ValueError
/usr/lib/python3.6/asyncio/base\u events.py in\u check\u closed(self)
375 def检查关闭(自):
376如果自我关闭:
-->377 raise RUNTIMERROR('事件循环已关闭')
378
379 def_asyncgen_finalizer_hook(self,agen):
RuntimeError:事件循环已关闭
如果我将
client.run
命令放在底部,程序将永远无法到达它,因为循环阻止它到达命令

我错过什么了吗?我不知道问题出在哪里。非常感谢您的帮助。

笔记本电脑是Google Colab的基础和使用工具,它们有自己的事件循环。
如果未指定任何事件循环,则使用当前事件循环,并在完成运行后将其关闭。
在事件循环关闭之前,您应该能够运行它一次,连续尝试再次运行它会告诉您同样的情况。如果您想在同一个笔记本中多次运行事件循环,您需要使用并自己处理事件循环。

笔记本是Google Colab的基础和使用工具,它们有自己的事件循环。
如果未指定任何事件循环,则使用当前事件循环,并在完成运行后将其关闭。

在事件循环关闭之前,您应该能够运行它一次,连续尝试再次运行它会告诉您同样的情况。如果您想在同一个笔记本中多次运行事件循环,您将需要使用它并自己处理它。

如果您想在google colab上运行discord bot,基本上需要进行各种黑客操作。在这里,我使用discord.ext python库,但是执行它将被阻塞,并阻止您执行任何其他单元格

!pip install discord.py
import nest_asyncio
nest_asyncio.apply()
 
import asyncio
await = lambda x: asyncio.get_event_loop().run_until_complete(x)
async def init(what, token):
    await what(token)
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="")
async def start():
    await bot.wait_until_ready()
    print("ready")
    await bot.get_channel(channelID).send("hi")

bot.loop.create_task(start())

TOKEN = ""   #@param {type: "string"}


如果你想在google colab上运行discord机器人,基本上你需要做各种各样的破解。在这里,我使用discord.ext python库,但是执行它将被阻塞,并阻止您执行任何其他单元格

!pip install discord.py
import nest_asyncio
nest_asyncio.apply()
 
import asyncio
await = lambda x: asyncio.get_event_loop().run_until_complete(x)
async def init(what, token):
    await what(token)
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="")
async def start():
    await bot.wait_until_ready()
    print("ready")
    await bot.get_channel(channelID).send("hi")

bot.loop.create_task(start())

TOKEN = ""   #@param {type: "string"}


当我启动bot时,使用client run会返回此消息
/usr/local/lib/python3.6/dist packages/ipykernel_launcher.py:11:RuntimeWarning:coroutine“client.start”从未被等待过#这是由InteractiveShellApp.init_path()添加回来的
,bot不会响应discord命令,但循环开始进行。如文档所述,
Client.start
是一个协同程序。您需要使用事件循环来运行它。当我启动bot
/usr/local/lib/python3.6/dist packages/ipykernel\u launcher.py:11:RuntimeWarning:coroutine“client.start”从未等待过,这是由InteractiveShellApp.init\u path()添加回来的,bot不会对discord命令做出响应,但是循环开始了。如文档所述,
Client.start
是一个协同程序。您需要使用事件循环来运行它。