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 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-06-29 12:57:46 +00:00
|
|
|
from config import CFG
|
|
|
|
|
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
|
|
|
|
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()
|
2022-06-12 07:07:53 +00:00
|
|
|
gather_list = []
|
2023-04-19 08:38:23 +00:00
|
|
|
Modules = ModulesManager.return_modules_list()
|
2022-06-12 07:07:53 +00:00
|
|
|
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():
|
2023-06-29 12:57:46 +00:00
|
|
|
for x in CFG().cp:
|
|
|
|
if x == 'secret':
|
|
|
|
for y in CFG().cp[x]:
|
2023-07-04 13:30:52 +00:00
|
|
|
if CFG().cp[x][y] is not None:
|
|
|
|
Secret.add(str(CFG().cp[x][y]).upper())
|
2023-04-05 04:33:29 +00:00
|
|
|
|
|
|
|
async def append_ip():
|
|
|
|
try:
|
|
|
|
Logger.info('Fetching IP information...')
|
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-04-05 04:33:29 +00:00
|
|
|
Logger.info('Successfully fetched IP information.')
|
|
|
|
except Exception:
|
|
|
|
Logger.info('Failed to get IP information.')
|
|
|
|
Logger.error(traceback.format_exc())
|
2023-02-05 14:33:33 +00:00
|
|
|
|
2023-04-05 04:33:29 +00:00
|
|
|
asyncio.create_task(append_ip())
|
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']
|