2022-06-12 07:07:53 +00:00
|
|
|
import asyncio
|
|
|
|
import logging
|
2021-04-28 12:21:33 +00:00
|
|
|
import os
|
2022-06-14 12:41:58 +00:00
|
|
|
import traceback
|
2022-07-31 08:27:58 +00:00
|
|
|
from configparser import ConfigParser
|
|
|
|
from os.path import abspath
|
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-14 12:41:58 +00:00
|
|
|
from core.elements import PrivateAssets, StartUp, Schedule, Secret
|
|
|
|
from core.exceptions import ConfigFileNotFound
|
2022-07-31 08:27:58 +00:00
|
|
|
from core.loader import load_modules, ModulesManager
|
2022-06-14 12:41:58 +00:00
|
|
|
from core.logger import Logger
|
2022-07-31 08:27:58 +00:00
|
|
|
from core.scheduler import Scheduler
|
2022-06-14 12:41:58 +00:00
|
|
|
from core.utils.http import get_url
|
2023-01-31 11:03:14 +00:00
|
|
|
from core.utils.ip import IP
|
2022-06-14 12:41:58 +00:00
|
|
|
|
2023-01-31 14:42:02 +00:00
|
|
|
bot_version = 'v4.0.10'
|
2022-06-13 12:19:26 +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:
|
2022-06-13 12:19:26 +00:00
|
|
|
write_version.write(bot_version)
|
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:
|
2022-06-13 12:19:26 +00:00
|
|
|
write_tag.write(bot_version)
|
2021-08-21 15:58:07 +00:00
|
|
|
write_tag.close()
|
|
|
|
|
|
|
|
|
2022-06-14 12:41:58 +00:00
|
|
|
async def init_async(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-07-31 08:33:20 +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)
|
2022-06-14 12:41:58 +00:00
|
|
|
await load_secret()
|
|
|
|
|
|
|
|
|
|
|
|
async def load_secret():
|
|
|
|
config_filename = 'config.cfg'
|
|
|
|
config_path = abspath('./config/' + config_filename)
|
|
|
|
cp = ConfigParser()
|
|
|
|
cp.read(config_path)
|
|
|
|
section = cp.sections()
|
|
|
|
if len(section) == 0:
|
|
|
|
raise ConfigFileNotFound(config_path) from None
|
|
|
|
section = section[0]
|
|
|
|
options = cp.options(section)
|
|
|
|
for option in options:
|
|
|
|
value = cp.get(section, option)
|
|
|
|
if value.upper() not in ['', 'TRUE', 'FALSE']:
|
|
|
|
Secret.add(value.upper())
|
2022-06-23 13:51:53 +00:00
|
|
|
try:
|
|
|
|
async def append_ip():
|
2023-01-31 11:03:14 +00:00
|
|
|
ip = await get_url('https://api.ip.sb/geoip', timeout=10, fmt='json')
|
2022-06-23 13:51:53 +00:00
|
|
|
if ip:
|
2023-01-31 11:03:14 +00:00
|
|
|
Secret.add(ip['ip'])
|
|
|
|
IP.country = ip['country']
|
|
|
|
IP.address = ip['ip']
|
2022-06-23 13:51:53 +00:00
|
|
|
Logger.info('Fetching IP information...')
|
|
|
|
await asyncio.create_task(append_ip())
|
|
|
|
Logger.info('Successfully fetched IP information.')
|
2022-06-22 14:22:18 +00:00
|
|
|
except Exception:
|
2022-06-23 13:51:53 +00:00
|
|
|
Logger.info('Failed to get IP information.')
|
|
|
|
Logger.error(traceback.format_exc())
|
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:
|
2022-07-30 08:21:00 +00:00
|
|
|
if (read := open_loader_cache.read()) != '':
|
|
|
|
await m.sendDirectMessage('加载模块中发生了以下错误,对应模块未加载:\n' + read)
|
|
|
|
else:
|
|
|
|
await m.sendDirectMessage('所有模块已正常加载。')
|
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
|
|
|
|
|
|
|
|
2022-06-14 12:41:58 +00:00
|
|
|
__all__ = ['init', 'init_async', 'load_prompt']
|