refactor code

This commit is contained in:
hibiki
2021-07-30 21:21:51 +08:00
parent 2ad891aa1e
commit cc24822dca
165 changed files with 7815 additions and 8174 deletions
+11 -31
View File
@@ -7,9 +7,9 @@ from .data_source import cancel_all_notice, save_data, get_data, set_data_value
from services.log import logger
__plugin_name__ = '查看群最后聊天时间 [Hidden]'
__plugin_name__ = "查看群最后聊天时间 [Hidden]"
__plugin_usage__ = '用法:无'
__plugin_usage__ = "用法:无"
last_chat = on_message(priority=1, block=False, permission=GROUP)
@@ -19,37 +19,17 @@ last_chat = on_message(priority=1, block=False, permission=GROUP)
async def _(bot: Bot, event: GroupMessageEvent, state: T_State):
time_data = await get_data()
set_data_value(event.group_id, time.time())
if event.group_id in time_data['_group']:
time_data['_group'].remove(event.group_id)
set_data_value('_group', time_data['_group'])
if event.group_id in time_data["_group"]:
time_data["_group"].remove(event.group_id)
set_data_value("_group", time_data["_group"])
for key in time_data.keys():
if key not in ['check_time', '_group']:
if key not in time_data['_group']:
if key not in ["check_time", "_group"]:
if key not in time_data["_group"]:
if time.time() - time_data[key] > 60 * 60 * 36:
await cancel_all_notice(key)
time_data["_group"].append(key)
set_data_value('_group', time_data['_group'])
logger.info(f'GROUP {event.group_id} 因群内发言时间大于36小时被取消全部通知')
if time.time() - time_data['check_time'] > 60 * 60 * 1:
set_data_value('check_time', time.time())
set_data_value("_group", time_data["_group"])
logger.info(f"GROUP {event.group_id} 因群内发言时间大于36小时被取消全部通知")
if time.time() - time_data["check_time"] > 60 * 60 * 1:
set_data_value("check_time", time.time())
save_data()
+14 -17
View File
@@ -4,6 +4,7 @@ from models.group_remind import GroupRemind
from datetime import datetime
import time
from services.log import logger
try:
import ujson as json
except ModuleNotFoundError:
@@ -17,34 +18,36 @@ async def init():
global time_data
bot = get_bot()
gl = await bot.get_group_list(self_id=bot.self_id)
gl = [g['group_id'] for g in gl]
data = read_data('group_last_chat_time.json')
gl = [g["group_id"] for g in gl]
data = read_data("group_last_chat_time.json")
for g in gl:
if not data.get(g):
time_data[g] = time.time()
if not time_data.get('check_time'):
time_data['check_time'] = time.time()
if not time_data.get('_group'):
time_data['_group'] = []
if not time_data.get("check_time"):
time_data["check_time"] = time.time()
if not time_data.get("_group"):
time_data["_group"] = []
save_data()
return time_data
def read_data(file_name: str):
try:
with open(DATA_PATH + file_name, 'r', encoding='utf8') as f:
with open(DATA_PATH + file_name, "r", encoding="utf8") as f:
return json.load(f)
except (ValueError, FileNotFoundError):
return {}
def save_data():
with open(DATA_PATH + 'group_last_chat_time.json', 'w') as f:
with open(DATA_PATH + "group_last_chat_time.json", "w") as f:
json.dump(time_data, f, indent=4)
logger.info(f'自动存储 group_last_chat_time.json 时间:{str(datetime.now()).split(".")[0]}')
logger.info(
f'自动存储 group_last_chat_time.json 时间:{str(datetime.now()).split(".")[0]}'
)
command_list = ['zwa', 'hy', 'kxcz', 'blpar', 'epic', 'pa']
command_list = ["zwa", "hy", "kxcz", "blpar", "epic", "pa", "gb"]
# 取消全部通知
@@ -53,7 +56,7 @@ async def cancel_all_notice(group_id):
for command in command_list:
if await GroupRemind.get_status(group_id, command):
await GroupRemind.set_status(group_id, command, False)
logger.info(f'关闭了 {group_id} 群的全部通知')
logger.info(f"关闭了 {group_id} 群的全部通知")
async def get_data():
@@ -66,9 +69,3 @@ async def get_data():
def set_data_value(key, value):
global time_data
time_data[key] = value