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
|
|
|
|
2023-02-05 14:33:33 +00:00
|
|
|
from core.builtins import PrivateAssets, Secret
|
2022-06-14 12:41:58 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2023-03-01 13:41:42 +00:00
|
|
|
async def init_async() -> None:
|
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-12 07:07:53 +00:00
|
|
|
gather_list = []
|
|
|
|
Modules = ModulesManager.return_modules_list_as_dict()
|
|
|
|
for x in Modules:
|
2023-03-04 08:51:56 +00:00
|
|
|
if schedules := Modules[x].schedule_list.set:
|
|
|
|
for schedule in schedules:
|
|
|
|
Scheduler.add_job(func=schedule.function, trigger=schedule.trigger, 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']
|
2023-02-05 14:33:33 +00:00
|
|
|
|
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()) != '':
|
2023-03-05 11:18:42 +00:00
|
|
|
await m.sendDirectMessage(m.parent.locale.t('error.loader.load.failed', err_msg=read))
|
2022-07-30 08:21:00 +00:00
|
|
|
else:
|
2023-03-05 11:18:42 +00:00
|
|
|
await m.sendDirectMessage(m.parent.locale.t('error.loader.load.success'))
|
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
|
|
|
|
|
|
|
|
2023-03-01 13:41:42 +00:00
|
|
|
__all__ = ['init_async', 'load_prompt']
|