問題描述
我正在嘗試用 Python 制作一個(gè) Discord 機(jī)器人,它根據(jù)您所在的班級(jí)(在我的學(xué)校)為您提供 Discord 服務(wù)器上的角色.我剛剛開始,但每當(dāng)我嘗試運(yùn)行它時(shí)都會(huì)收到錯(cuò)誤消息(我在 Python 3 Notebook 中的 Google Colab 中運(yùn)行它).這是我的代碼:
I am attempting to make a Discord bot in Python that gives you a role on a Discord server based upon what class you are in (at my school). I have just started, but I am receiving an error whenever I attempt to run it (I run it in Google Colab in a Python 3 Notebook). Here is my code:
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))
當(dāng)我運(yùn)行它時(shí),它會(huì)提示我這個(gè)錯(cuò)誤:
When I run it, it presents me with this error:
---------------------------------------------------------------------------
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
命令放在底部,程序永遠(yuǎn)不會(huì)到達(dá)它,因?yàn)檠h(huán)會(huì)阻止它到達(dá)命令.
If I put the client.run
command at the bottom, the program never reaches it because the loop prevents it from reaching the command.
我錯(cuò)過了什么嗎?我不知道問題出在哪里.非常感謝幫助.
Am I missing something? I do not know where the problem is. Would appreciate help.
推薦答案
基本上,如果你想在 google colab 上運(yùn)行 discord 機(jī)器人,你需要做各種黑客攻擊.這里我使用 discord.ext python 庫(kù),執(zhí)行這個(gè),但是會(huì)阻塞,并阻止你執(zhí)行任何其他單元格.
Basically there are various hacks you need to do if you want to run a discord bot on google colab. Here I use discord.ext python library, executing this, however will be blocking, and prevent you from executing any other cells.
!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"}
這篇關(guān)于運(yùn)行我的 discord.py 機(jī)器人時(shí)在 client.run('token') 收到運(yùn)行時(shí)錯(cuò)誤的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!