Setup modules load and unload system
This commit is contained in:
parent
2a963df62d
commit
d74f29fefb
10 changed files with 169 additions and 40 deletions
|
@ -5,7 +5,7 @@ from os.path import isdir, isfile
|
||||||
from threading import RLock
|
from threading import RLock
|
||||||
|
|
||||||
from aiogram import Bot, Dispatcher, types
|
from aiogram import Bot, Dispatcher, types
|
||||||
from aiogram.types import reply_keyboard, ContentType
|
from aiogram.types import reply_keyboard
|
||||||
from aiogram.utils.callback_data import CallbackData
|
from aiogram.utils.callback_data import CallbackData
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
@ -64,34 +64,11 @@ def check_id(user: types.User):
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
|
||||||
from TelegramEDT.basic import start, help_cmd
|
from TelegramEDT.modules import load_module, load_cmd, unload_cmd
|
||||||
dp.register_message_handler(start, commands="start")
|
dp.register_message_handler(load_cmd, commands="load")
|
||||||
dp.register_message_handler(help_cmd, commands="help")
|
dp.register_message_handler(unload_cmd, commands="unload")
|
||||||
|
|
||||||
from TelegramEDT.edt import edt_cmd, edt_query, inline_edt, edt_await, edt_geturl
|
logger.info("Start loading modules")
|
||||||
dp.register_message_handler(edt_cmd, lambda msg: msg.text.lower() == "edt")
|
for m in ["basic", "edt", "kfet", "tomuss", "notif", "await_cmd", "tools"]:
|
||||||
dp.register_inline_handler(inline_edt)
|
load_module(m)
|
||||||
dp.register_callback_query_handler(edt_query, posts_cb.filter(action=["day", "next", "week", "next week"]))
|
logger.info("Modules loading finish")
|
||||||
dp.register_message_handler(edt_await, lambda msg: msg.text.lower() == "setedt")
|
|
||||||
dp.register_message_handler(edt_geturl, commands="getedt")
|
|
||||||
|
|
||||||
from TelegramEDT.kfet import kfet, kfet_set
|
|
||||||
dp.register_message_handler(kfet, lambda msg: msg.text.lower() == "kfet")
|
|
||||||
dp.register_message_handler(kfet_set, lambda msg: msg.text.lower() == "setkfet")
|
|
||||||
|
|
||||||
from TelegramEDT.tomuss import settomuss
|
|
||||||
dp.register_message_handler(settomuss, lambda msg: msg.text.lower() == "settomuss")
|
|
||||||
|
|
||||||
from TelegramEDT.notif import notif, notif_cmd, notif_query
|
|
||||||
dp.register_message_handler(notif_cmd, lambda msg: msg.text.lower() == "notif")
|
|
||||||
dp.register_callback_query_handler(notif_query, posts_cb.filter(action=["toggle", "time", "cooldown"]))
|
|
||||||
|
|
||||||
from TelegramEDT.await_cmd import await_cmd, have_await_cmd
|
|
||||||
dp.register_message_handler(await_cmd, lambda msg: have_await_cmd(msg), content_types=[ContentType.TEXT, ContentType.PHOTO])
|
|
||||||
|
|
||||||
from TelegramEDT.tools import get_id, get_logs, get_db, eval_cmd, errors
|
|
||||||
dp.register_message_handler(get_id, commands="getid")
|
|
||||||
dp.register_message_handler(get_logs, commands="getlogs")
|
|
||||||
dp.register_message_handler(get_db, commands="getdb")
|
|
||||||
dp.register_message_handler(eval_cmd, commands="eval")
|
|
||||||
dp.register_errors_handler(errors)
|
|
||||||
|
|
|
@ -3,13 +3,13 @@ import re
|
||||||
import requests
|
import requests
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from aiogram import types
|
from aiogram import types
|
||||||
from aiogram.types import ParseMode
|
from aiogram.types import ParseMode, ContentType
|
||||||
from feedparser import parse
|
from feedparser import parse
|
||||||
from ics.parse import ParseError
|
from ics.parse import ParseError
|
||||||
from pyzbar.pyzbar import decode
|
from pyzbar.pyzbar import decode
|
||||||
from requests.exceptions import ConnectionError, InvalidSchema, MissingSchema
|
from requests.exceptions import ConnectionError, InvalidSchema, MissingSchema
|
||||||
|
|
||||||
from TelegramEDT import API_TOKEN, bot, dbL, key, logger, session, check_id
|
from TelegramEDT import API_TOKEN, bot, dbL, dp, key, logger, session, check_id
|
||||||
from TelegramEDT.EDTcalendar import Calendar
|
from TelegramEDT.EDTcalendar import Calendar
|
||||||
from TelegramEDT.base import User
|
from TelegramEDT.base import User
|
||||||
from TelegramEDT.lang import lang
|
from TelegramEDT.lang import lang
|
||||||
|
@ -91,3 +91,14 @@ async def await_cmd(message: types.message):
|
||||||
|
|
||||||
if msg:
|
if msg:
|
||||||
await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key)
|
await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key)
|
||||||
|
|
||||||
|
|
||||||
|
def load():
|
||||||
|
logger.info("Load await_cmd module")
|
||||||
|
dp.register_message_handler(await_cmd, lambda msg: have_await_cmd(msg),content_types=[ContentType.TEXT,
|
||||||
|
ContentType.PHOTO])
|
||||||
|
|
||||||
|
|
||||||
|
def unload():
|
||||||
|
logger.info("Unload await_cmd module")
|
||||||
|
dp.message_handlers.unregister(await_cmd)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from aiogram import types
|
from aiogram import types
|
||||||
from aiogram.types import ParseMode
|
from aiogram.types import ParseMode
|
||||||
|
|
||||||
from TelegramEDT import dbL, key, logger, session, check_id
|
from TelegramEDT import dbL, dp, key, logger, session, check_id
|
||||||
from TelegramEDT.base import User
|
from TelegramEDT.base import User
|
||||||
from TelegramEDT.lang import lang
|
from TelegramEDT.lang import lang
|
||||||
|
|
||||||
|
@ -22,3 +22,15 @@ async def help_cmd(message: types.Message):
|
||||||
with dbL:
|
with dbL:
|
||||||
user = session.query(User).filter_by(id=message.from_user.id).first()
|
user = session.query(User).filter_by(id=message.from_user.id).first()
|
||||||
await message.reply(lang(user, "help"), parse_mode=ParseMode.MARKDOWN, reply_markup=key)
|
await message.reply(lang(user, "help"), parse_mode=ParseMode.MARKDOWN, reply_markup=key)
|
||||||
|
|
||||||
|
|
||||||
|
def load():
|
||||||
|
logger.info("Load basic module")
|
||||||
|
dp.register_message_handler(start, commands="start")
|
||||||
|
dp.register_message_handler(help_cmd, commands="help")
|
||||||
|
|
||||||
|
|
||||||
|
def unload():
|
||||||
|
logger.info("Unload basic module")
|
||||||
|
dp.message_handlers.unregister(start)
|
||||||
|
dp.message_handlers.unregister(help_cmd)
|
||||||
|
|
|
@ -4,7 +4,7 @@ from aiogram import types
|
||||||
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, ParseMode, InputTextMessageContent, \
|
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, ParseMode, InputTextMessageContent, \
|
||||||
InlineQueryResultArticle, InlineQuery
|
InlineQueryResultArticle, InlineQuery
|
||||||
|
|
||||||
from TelegramEDT import dbL, key, logger, posts_cb, session, TIMES, bot, check_id
|
from TelegramEDT import dbL, dp, key, logger, posts_cb, session, TIMES, bot, check_id
|
||||||
from TelegramEDT.base import User
|
from TelegramEDT.base import User
|
||||||
from TelegramEDT.lang import lang
|
from TelegramEDT.lang import lang
|
||||||
|
|
||||||
|
@ -77,3 +77,21 @@ async def edt_geturl(message: types.Message):
|
||||||
await message.reply(user.resources, reply_markup=key)
|
await message.reply(user.resources, reply_markup=key)
|
||||||
else:
|
else:
|
||||||
await message.reply(lang(user, "getedt_err"), reply_markup=key)
|
await message.reply(lang(user, "getedt_err"), reply_markup=key)
|
||||||
|
|
||||||
|
|
||||||
|
def load():
|
||||||
|
logger.info("Load edt module")
|
||||||
|
dp.register_message_handler(edt_cmd, lambda msg: msg.text.lower() == "edt")
|
||||||
|
dp.register_inline_handler(inline_edt)
|
||||||
|
dp.register_callback_query_handler(edt_query, posts_cb.filter(action=["day", "next", "week", "next week"]))
|
||||||
|
dp.register_message_handler(edt_await, lambda msg: msg.text.lower() == "setedt")
|
||||||
|
dp.register_message_handler(edt_geturl, commands="getedt")
|
||||||
|
|
||||||
|
|
||||||
|
def unload():
|
||||||
|
logger.info("Unload edt module")
|
||||||
|
dp.message_handlers.unregister(edt_cmd)
|
||||||
|
dp.inline_query_handlers.unregister(inline_edt)
|
||||||
|
dp.callback_query_handlers.unregister(edt_query)
|
||||||
|
dp.message_handlers.unregister(edt_await)
|
||||||
|
dp.message_handlers.unregister(edt_geturl)
|
||||||
|
|
|
@ -5,7 +5,7 @@ from aiogram import types
|
||||||
from aiogram.types import ParseMode
|
from aiogram.types import ParseMode
|
||||||
from aiogram.utils import markdown
|
from aiogram.utils import markdown
|
||||||
|
|
||||||
from TelegramEDT import dbL, key, logger, session, check_id
|
from TelegramEDT import dbL, dp, key, logger, session, check_id
|
||||||
from TelegramEDT.base import User, KFET_URL
|
from TelegramEDT.base import User, KFET_URL
|
||||||
from TelegramEDT.lang import lang
|
from TelegramEDT.lang import lang
|
||||||
|
|
||||||
|
@ -45,3 +45,15 @@ async def kfet_set(message: types.Message):
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key)
|
await message.reply(msg, parse_mode=ParseMode.MARKDOWN, reply_markup=key)
|
||||||
|
|
||||||
|
|
||||||
|
def load():
|
||||||
|
logger.info("Load kfet module")
|
||||||
|
dp.register_message_handler(kfet, lambda msg: msg.text.lower() == "kfet")
|
||||||
|
dp.register_message_handler(kfet_set, lambda msg: msg.text.lower() == "setkfet")
|
||||||
|
|
||||||
|
|
||||||
|
def unload():
|
||||||
|
logger.info("Unload kfet module")
|
||||||
|
dp.message_handlers.unregister(kfet)
|
||||||
|
dp.message_handlers.unregister(kfet_set)
|
||||||
|
|
59
TelegramEDT/modules.py
Normal file
59
TelegramEDT/modules.py
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
from importlib import import_module
|
||||||
|
|
||||||
|
from aiogram.types import Message
|
||||||
|
|
||||||
|
from TelegramEDT import ADMIN_ID, logger
|
||||||
|
|
||||||
|
|
||||||
|
def load_module(module: str) -> bool:
|
||||||
|
try:
|
||||||
|
module = import_module(f"TelegramEDT.{module}")
|
||||||
|
except ModuleNotFoundError:
|
||||||
|
logger.error(f"Fail to load module {module}, module not found !")
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
module.load()
|
||||||
|
except AttributeError:
|
||||||
|
logger.error(f"Fail to load module {module}, no load function !")
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def unload_module(module: str) -> bool:
|
||||||
|
try:
|
||||||
|
module = import_module(f"TelegramEDT.{module}")
|
||||||
|
except ModuleNotFoundError:
|
||||||
|
logger.error(f"Fail to unload module {module}, module not found !")
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
module.unload()
|
||||||
|
except AttributeError:
|
||||||
|
logger.error(f"Fail to unload module {module}, no unload function !")
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
async def load_cmd(message: Message):
|
||||||
|
logger.info(f"{message.from_user.username} do load command")
|
||||||
|
if message.from_user.id == ADMIN_ID:
|
||||||
|
module = message.text[6:]
|
||||||
|
if load_module(module):
|
||||||
|
msg = f"Module {module} loaded !"
|
||||||
|
else:
|
||||||
|
msg = f"Fail to load module {module} !"
|
||||||
|
|
||||||
|
await message.reply(msg)
|
||||||
|
|
||||||
|
|
||||||
|
async def unload_cmd(message: Message):
|
||||||
|
logger.info(f"{message.from_user.username} do unload command")
|
||||||
|
if message.from_user.id == ADMIN_ID:
|
||||||
|
module = message.text[8:]
|
||||||
|
if unload_module(module):
|
||||||
|
msg = f"Module {module} unloaded !"
|
||||||
|
else:
|
||||||
|
msg = f"Fail to unload module {module} !"
|
||||||
|
|
||||||
|
await message.reply(msg)
|
|
@ -4,7 +4,7 @@ from aiogram import types
|
||||||
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, ParseMode
|
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, ParseMode
|
||||||
from aiogram.utils import markdown
|
from aiogram.utils import markdown
|
||||||
|
|
||||||
from TelegramEDT import bot, dbL, logger, posts_cb, session, check_id
|
from TelegramEDT import bot, dbL, dp, logger, posts_cb, session, check_id
|
||||||
from TelegramEDT.base import User
|
from TelegramEDT.base import User
|
||||||
from TelegramEDT.lang import lang
|
from TelegramEDT.lang import lang
|
||||||
|
|
||||||
|
@ -81,3 +81,15 @@ async def notif_query(query: types.CallbackQuery, callback_data: dict):
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
await query.message.reply(msg, parse_mode=ParseMode.MARKDOWN)
|
await query.message.reply(msg, parse_mode=ParseMode.MARKDOWN)
|
||||||
|
|
||||||
|
|
||||||
|
def load():
|
||||||
|
logger.info("Load notif module")
|
||||||
|
dp.register_message_handler(notif_cmd, lambda msg: msg.text.lower() == "notif")
|
||||||
|
dp.register_callback_query_handler(notif_query, posts_cb.filter(action=["toggle", "time", "cooldown"]))
|
||||||
|
|
||||||
|
|
||||||
|
def unload():
|
||||||
|
logger.info("Unload notif module")
|
||||||
|
dp.message_handlers.unregister(notif_cmd)
|
||||||
|
dp.callback_query_handlers.unregister(notif_query)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from aiogram import types
|
from aiogram import types
|
||||||
from aiogram.types import ParseMode
|
from aiogram.types import ParseMode
|
||||||
|
|
||||||
from TelegramEDT import dbL, key, logger, session, check_id
|
from TelegramEDT import dbL, dp, key, logger, session, check_id
|
||||||
from TelegramEDT.base import User
|
from TelegramEDT.base import User
|
||||||
from TelegramEDT.lang import lang
|
from TelegramEDT.lang import lang
|
||||||
|
|
||||||
|
@ -16,3 +16,13 @@ async def settomuss(message: types.Message):
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
await message.reply(lang(user, "settomuss_wait"), parse_mode=ParseMode.MARKDOWN, reply_markup=key)
|
await message.reply(lang(user, "settomuss_wait"), parse_mode=ParseMode.MARKDOWN, reply_markup=key)
|
||||||
|
|
||||||
|
|
||||||
|
def load():
|
||||||
|
logger.info("Load tomuss module")
|
||||||
|
dp.register_message_handler(settomuss, lambda msg: msg.text.lower() == "settomuss")
|
||||||
|
|
||||||
|
|
||||||
|
def unload():
|
||||||
|
logger.info("Unload tomuss module")
|
||||||
|
dp.message_handlers.unregister(settomuss)
|
||||||
|
|
|
@ -3,7 +3,7 @@ from aiogram.types import ParseMode
|
||||||
from aiogram.utils import markdown
|
from aiogram.utils import markdown
|
||||||
from aiogram.utils.exceptions import MessageIsTooLong
|
from aiogram.utils.exceptions import MessageIsTooLong
|
||||||
|
|
||||||
from TelegramEDT import ADMIN_ID, bot, dbL, key, log_date, logger, session, check_id
|
from TelegramEDT import ADMIN_ID, bot, dbL, dp, key, log_date, logger, session, check_id
|
||||||
from TelegramEDT.base import User
|
from TelegramEDT.base import User
|
||||||
|
|
||||||
|
|
||||||
|
@ -79,3 +79,21 @@ async def errors(*args, **partial_data):
|
||||||
sep="\n"
|
sep="\n"
|
||||||
)
|
)
|
||||||
await bot.send_message(ADMIN_ID, msg, parse_mode=ParseMode.MARKDOWN)
|
await bot.send_message(ADMIN_ID, msg, parse_mode=ParseMode.MARKDOWN)
|
||||||
|
|
||||||
|
|
||||||
|
def load():
|
||||||
|
logger.info("Load tools module")
|
||||||
|
dp.register_message_handler(get_id, commands="getid")
|
||||||
|
dp.register_message_handler(get_logs, commands="getlogs")
|
||||||
|
dp.register_message_handler(get_db, commands="getdb")
|
||||||
|
dp.register_message_handler(eval_cmd, commands="eval")
|
||||||
|
dp.register_errors_handler(errors)
|
||||||
|
|
||||||
|
|
||||||
|
def unload():
|
||||||
|
logger.info("Unload tools module")
|
||||||
|
dp.message_handlers.unregister(get_id)
|
||||||
|
dp.message_handlers.unregister(get_logs)
|
||||||
|
dp.message_handlers.unregister(get_db)
|
||||||
|
dp.message_handlers.unregister(eval_cmd)
|
||||||
|
dp.errors_handlers.unregister(errors)
|
||||||
|
|
2
main.py
2
main.py
|
@ -2,7 +2,7 @@ import asyncio
|
||||||
|
|
||||||
from aiogram.utils import executor
|
from aiogram.utils import executor
|
||||||
|
|
||||||
from TelegramEDT import notif, dp
|
from TelegramEDT.notif import notif, dp
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
loop.create_task(notif())
|
loop.create_task(notif())
|
||||||
|
|
Reference in a new issue