PaiGram/plugins/admin/search.py

59 lines
2.3 KiB
Python
Raw Normal View History

2022-12-04 11:56:39 +00:00
import asyncio
import datetime
from telegram import Update
from telegram.ext import CallbackContext
from core.plugin import handler, Plugin, job
from core.services.search.services import SearchServices
2022-12-04 11:56:39 +00:00
from utils.log import logger
__all__ = ("SearchPlugin",)
2022-12-04 11:56:39 +00:00
class SearchPlugin(Plugin):
def __init__(self, search: SearchServices = None):
self.search = search
self.lock = asyncio.Lock()
2022-12-04 11:56:39 +00:00
async def initialize(self):
2022-12-04 11:56:39 +00:00
async def load_data():
logger.info("Search 插件模块正在加载搜索条目")
async with self.lock:
2022-12-04 11:56:39 +00:00
await self.search.load_data()
logger.success("Search 插件加载模块搜索条目成功")
asyncio.create_task(load_data())
@job.run_repeating(interval=datetime.timedelta(hours=1), name="SaveEntryJob")
async def save_entry_job(self, _: CallbackContext):
if self.lock.locked():
2022-12-04 11:56:39 +00:00
logger.warning("条目数据正在保存 跳过本次定时任务")
else:
async with self.lock:
2022-12-04 11:56:39 +00:00
logger.info("条目数据正在自动保存")
await self.search.save_entry()
logger.success("条目数据自动保存成功")
@handler.command("save_entry", block=False, admin=True)
2022-12-04 11:56:39 +00:00
async def save_entry(self, update: Update, _: CallbackContext):
user = update.effective_user
message = update.effective_message
logger.info("用户 %s[%s] 保存条目数据命令请求", user.full_name, user.id)
if self.lock.locked():
2022-12-04 11:56:39 +00:00
await message.reply_text("条目数据正在保存 请稍后重试")
else:
async with self.lock:
2022-12-04 11:56:39 +00:00
reply_text = await message.reply_text("正在保存数据")
await self.search.save_entry()
await reply_text.edit_text("数据保存成功")
@handler.command("remove_all_entry", block=False, admin=True)
2022-12-04 11:56:39 +00:00
async def remove_all_entry(self, update: Update, _: CallbackContext):
user = update.effective_user
message = update.effective_message
logger.info("用户 %s[%s] 删除全部条目数据命令请求", user.full_name, user.id)
reply_text = await message.reply_text("正在删除全部条目数据")
await self.search.remove_all_entry()
await reply_text.edit_text("删除全部条目数据成功")