Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/core/utils/bot.py

61 lines
2.1 KiB
Python
Raw Normal View History

2022-06-12 07:07:53 +00:00
import asyncio
import logging
2021-04-28 12:21:33 +00:00
import os
2021-04-27 15:01:17 +00:00
2021-09-10 18:05:27 +00:00
import ujson as json
2021-02-06 16:30:13 +00:00
2022-06-12 07:07:53 +00:00
from core.elements import PrivateAssets, StartUp, FetchTarget, Schedule
from core.loader import load_modules, ModulesManager
from core.scheduler import Scheduler
2021-04-28 12:21:33 +00:00
2021-08-07 07:56:48 +00:00
2021-10-01 01:18:04 +00:00
def init() -> None:
2022-06-12 07:07:53 +00:00
"""初始化机器人。仅用于bot.py与console.py。"""
2021-10-14 15:18:47 +00:00
load_modules()
2021-08-21 15:58:07 +00:00
version = os.path.abspath(PrivateAssets.path + '/version')
write_version = open(version, 'w')
2021-10-16 05:24:53 +00:00
try:
write_version.write(os.popen('git rev-parse HEAD', 'r').read()[0:6])
2022-04-01 14:55:21 +00:00
except Exception as e:
write_version.write(str(e))
2021-08-21 15:58:07 +00:00
write_version.close()
tag = os.path.abspath(PrivateAssets.path + '/version_tag')
write_tag = open(tag, 'w')
2021-10-16 05:24:53 +00:00
try:
write_tag.write(os.popen('git tag -l', 'r').read().split('\n')[-2])
2022-04-01 14:55:21 +00:00
except Exception as e:
write_tag.write(str(e))
2021-08-21 15:58:07 +00:00
write_tag.close()
2022-06-12 14:30:02 +00:00
async def init_scheduler(ft) -> None:
2022-06-12 07:07:53 +00:00
gather_list = []
Modules = ModulesManager.return_modules_list_as_dict()
for x in Modules:
if isinstance(Modules[x], StartUp):
2022-06-12 14:30:02 +00:00
gather_list.append(asyncio.ensure_future(Modules[x].function(ft)))
2022-06-12 07:07:53 +00:00
elif isinstance(Modules[x], Schedule):
2022-06-12 14:30:02 +00:00
Scheduler.add_job(func=Modules[x].function, trigger=Modules[x].trigger, args=[ft], misfire_grace_time=30, max_instance=1)
2022-06-12 07:07:53 +00:00
await asyncio.gather(*gather_list)
Scheduler.start()
logging.getLogger('apscheduler.executors.default').setLevel(logging.WARNING)
2021-08-19 12:17:48 +00:00
2021-08-21 15:58:07 +00:00
2021-12-31 14:44:34 +00:00
async def load_prompt(bot) -> None:
2021-08-22 14:55:25 +00:00
author_cache = os.path.abspath(PrivateAssets.path + '/cache_restart_author')
2021-10-14 15:18:47 +00:00
loader_cache = os.path.abspath(PrivateAssets.path + '/.cache_loader')
2021-08-19 12:17:48 +00:00
if os.path.exists(author_cache):
open_author_cache = open(author_cache, 'r')
2021-08-21 15:58:07 +00:00
author = json.loads(open_author_cache.read())['ID']
2021-08-19 12:17:48 +00:00
open_loader_cache = open(loader_cache, 'r')
2021-08-21 15:58:07 +00:00
m = await bot.fetch_target(author)
if m:
await m.sendDirectMessage(open_loader_cache.read())
2021-08-21 15:58:07 +00:00
open_loader_cache.close()
open_author_cache.close()
os.remove(author_cache)
2021-11-12 14:25:53 +00:00
os.remove(loader_cache)
2022-06-12 07:07:53 +00:00
__all__ = ['init', 'init_scheduler', 'load_prompt']