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/console.py

71 lines
2.5 KiB
Python
Raw Normal View History

2021-08-24 10:22:36 +00:00
import os
2021-07-30 19:32:58 +00:00
from config import Config
if not Config('db_path'):
raise AttributeError('Wait! You need to fill a valid database address into the config.cfg "db_path"\n'
'Example: \ndb_path = sqlite:///database/save.db\n'
2021-07-30 19:57:40 +00:00
'(Also you can fill in the above example directly,'
2021-07-30 19:32:58 +00:00
' bot will automatically create a SQLite database in the "./database/save.db")')
2021-02-19 08:56:57 +00:00
import asyncio
2021-04-10 13:12:32 +00:00
import traceback
import aioconsole
2021-02-19 08:56:57 +00:00
2022-01-08 15:14:44 +00:00
from datetime import datetime
2022-01-20 13:44:54 +00:00
from bot import init_bot
2022-01-17 13:28:49 +00:00
from core.elements import Schedule, StartUp, MsgInfo, Session, PrivateAssets, EnableDirtyWordCheck, Url
from core.console.template import Template as MessageSession, FetchTarget
2021-07-30 18:06:04 +00:00
from core.parser.message import parser
from core.scheduler import Scheduler
2021-10-14 15:18:47 +00:00
from core.loader import ModulesManager
2021-10-14 15:29:59 +00:00
from core.utils import init
2022-01-20 13:31:50 +00:00
from core.logger import Logger
2021-08-24 10:22:36 +00:00
EnableDirtyWordCheck.status = True
2021-08-24 10:22:36 +00:00
PrivateAssets.set(os.path.abspath(os.path.dirname(__file__) + '/assets'))
2021-10-14 15:18:47 +00:00
init()
2021-02-19 12:20:00 +00:00
async def console_scheduler():
gather_list = []
2021-10-14 15:18:47 +00:00
Modules = ModulesManager.return_modules_list_as_dict()
for x in Modules:
2021-10-14 14:49:12 +00:00
if isinstance(Modules[x], StartUp):
2022-01-18 04:06:09 +00:00
gather_list.append(asyncio.ensure_future(
Modules[x].function(FetchTarget)))
2021-08-30 18:53:39 +00:00
if isinstance(Modules[x], Schedule):
2022-01-18 04:06:09 +00:00
Scheduler.add_job(
2022-05-08 15:13:29 +00:00
func=Modules[x].function, trigger=Modules[x].trigger, args=[FetchTarget], misfire_grace_time=30)
await asyncio.gather(*gather_list)
Scheduler.start()
async def console_command():
try:
m = await aioconsole.ainput('> ')
2022-01-08 15:14:44 +00:00
time = datetime.now()
2021-08-07 07:56:48 +00:00
await parser(MessageSession(target=MsgInfo(targetId='TEST|0',
senderId='TEST|0',
senderName='',
targetFrom='TEST|Console',
senderFrom='TEST|Console'),
2021-08-07 07:56:48 +00:00
session=Session(message=m, target='TEST|0', sender='TEST|0')))
print('----Process end----')
2022-01-08 15:14:44 +00:00
usage_time = datetime.now() - time
print('Usage time:', usage_time)
await console_command()
except KeyboardInterrupt:
print('Exited.')
2021-10-14 15:18:47 +00:00
exit()
except Exception:
2022-01-20 13:31:50 +00:00
Logger.error(traceback.format_exc())
2021-08-23 12:44:31 +00:00
2021-08-07 12:55:07 +00:00
init_bot()
loop = asyncio.get_event_loop()
loop.create_task(console_scheduler())
loop.create_task(console_command())
loop.run_forever()