zhenxun_bot/plugins/last_chat/data_source.py

72 lines
1.8 KiB
Python
Raw Normal View History

2021-05-20 19:23:32 +08:00
from configs.path_config import DATA_PATH
2021-06-30 19:50:55 +08:00
from utils.utils import get_bot
2021-05-20 19:23:32 +08:00
from models.group_remind import GroupRemind
from datetime import datetime
import time
from services.log import logger
2021-07-30 21:21:51 +08:00
2021-05-20 19:23:32 +08:00
try:
import ujson as json
except ModuleNotFoundError:
import json
time_data = {}
async def init():
global time_data
bot = get_bot()
gl = await bot.get_group_list(self_id=bot.self_id)
2021-07-30 21:21:51 +08:00
gl = [g["group_id"] for g in gl]
data = read_data("group_last_chat_time.json")
2021-05-20 19:23:32 +08:00
for g in gl:
if not data.get(g):
time_data[g] = time.time()
2021-07-30 21:21:51 +08:00
if not time_data.get("check_time"):
time_data["check_time"] = time.time()
if not time_data.get("_group"):
time_data["_group"] = []
2021-05-20 19:23:32 +08:00
save_data()
return time_data
def read_data(file_name: str):
try:
2021-07-30 21:21:51 +08:00
with open(DATA_PATH + file_name, "r", encoding="utf8") as f:
2021-05-20 19:23:32 +08:00
return json.load(f)
except (ValueError, FileNotFoundError):
return {}
def save_data():
2021-07-30 21:21:51 +08:00
with open(DATA_PATH + "group_last_chat_time.json", "w") as f:
2021-05-20 19:23:32 +08:00
json.dump(time_data, f, indent=4)
2021-07-30 21:21:51 +08:00
logger.info(
f'自动存储 group_last_chat_time.json 时间:{str(datetime.now()).split(".")[0]}'
)
2021-05-20 19:23:32 +08:00
2021-07-30 21:21:51 +08:00
command_list = ["zwa", "hy", "kxcz", "blpar", "epic", "pa", "gb"]
2021-05-20 19:23:32 +08:00
# 取消全部通知
async def cancel_all_notice(group_id):
group_id = int(group_id)
for command in command_list:
if await GroupRemind.get_status(group_id, command):
await GroupRemind.set_status(group_id, command, False)
2021-07-30 21:21:51 +08:00
logger.info(f"关闭了 {group_id} 群的全部通知")
2021-05-20 19:23:32 +08:00
async def get_data():
global time_data
if not time_data:
time_data = await init()
return time_data
def set_data_value(key, value):
global time_data
time_data[key] = value