最近我一直在努力学习 Python,我一直在查看一些机器人和自我机器人的源代码,试图了解发生了什么。我现在正在看的自我机器人(当然是为了教育目的),没有我在任何地方都看到过的 @client.event 装饰器。以下是两个相关文件的片段。
main.py:
6.23 s ± 320 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
client.py:
# Importing
import asyncio
from collections import namedtuple
import signal
from replit import clear
import client
import config
import updater
# Create clients
entries = []
clear()
for login in config.clients:
entry = namedtuple('entry', 'client, token')
newClient = client.myClient(login.catchingChannels, login.prefix, login.autoDuration)
entries.append(
entry(client = newClient, token = login.token))
loop = asyncio.get_event_loop()
signals = (signal.SIGHUP, signal.SIGTERM, signal.SIGINT)
# Shutdown
async def shutdown(signal, runningBots):
[runningBot.cancel() for runningBot in runningBots]
await asyncio.gather(*runningBots, return_exceptions=True)
loop.stop()
async def wrapped_connect(entry):
try:
await entry.client.start(entry.token, bot = False)
print('Logged in as {}'.format(entry))
finally:
await entry.client.close()
try:
runningBots = []
for entry in entries:
runningBots.append(loop.create_task(wrapped_connect(entry)))
for s in signals:
loop.add_signal_handler(s, lambda s = s: asyncio.create_task(shutdown(s, runningBots)))
loop.run_forever()
finally:
client.utils.cprint("Exiting...", "yellow")
client.utils.shutdown_event()
exit()
我删除了一些不相关的代码,但还有更多的事情发生。我认为所有东西都在一个类中并且有两个文件的原因是它登录了多个帐户。
那么对于我的问题,为什么 on_message 函数不使用 @client.event 装饰器,没有它如何工作?