mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-02 10:10:01 +08:00
webui主页和插件管理api更新
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
from enum import Enum
|
||||
from typing import NamedTuple
|
||||
|
||||
|
||||
class SearchType(Enum):
|
||||
|
||||
"""
|
||||
查询类型
|
||||
"""
|
||||
|
||||
DAY = "day_statistics"
|
||||
"""天"""
|
||||
WEEK = "week_statistics"
|
||||
"""周"""
|
||||
MONTH = "month_statistics"
|
||||
"""月"""
|
||||
TOTAL = "total_statistics"
|
||||
"""总数"""
|
||||
|
||||
|
||||
class ParseData(NamedTuple):
|
||||
|
||||
global_search: bool
|
||||
"""是否全局搜索"""
|
||||
search_type: SearchType
|
||||
"""搜索类型"""
|
||||
@@ -1,31 +0,0 @@
|
||||
|
||||
|
||||
from tortoise import fields
|
||||
|
||||
from services.db_context import Model
|
||||
|
||||
|
||||
class Statistics(Model):
|
||||
|
||||
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
||||
"""自增id"""
|
||||
user_id = fields.CharField(255)
|
||||
"""用户id"""
|
||||
group_id = fields.CharField(255, null=True)
|
||||
"""群聊id"""
|
||||
plugin_name = fields.CharField(255)
|
||||
"""插件名称"""
|
||||
create_time = fields.DatetimeField(auto_now=True)
|
||||
"""添加日期"""
|
||||
|
||||
class Meta:
|
||||
table = "statistics"
|
||||
table_description = "用户权限数据库"
|
||||
|
||||
@classmethod
|
||||
async def _run_script(cls):
|
||||
return [
|
||||
"ALTER TABLE statistics RENAME COLUMN user_qq TO user_id;", # 将user_qq改为user_id
|
||||
"ALTER TABLE statistics ALTER COLUMN user_id TYPE character varying(255);",
|
||||
"ALTER TABLE statistics ALTER COLUMN group_id TYPE character varying(255);",
|
||||
]
|
||||
@@ -1,13 +1,13 @@
|
||||
import asyncio
|
||||
import os
|
||||
from typing import Tuple
|
||||
|
||||
from nonebot import on_command
|
||||
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, Message, MessageEvent
|
||||
from nonebot.params import Command, CommandArg
|
||||
from nonebot.params import CommandArg
|
||||
|
||||
from configs.path_config import DATA_PATH, IMAGE_PATH
|
||||
from models.group_info import GroupInfo
|
||||
from utils.depends import OneCommand
|
||||
from utils.image_utils import BuildMat
|
||||
from utils.manager import plugins2settings_manager
|
||||
from utils.message_builder import image
|
||||
@@ -91,7 +91,7 @@ statistics_user_file = DATA_PATH / "statistics" / "_prefix_user_count.json"
|
||||
|
||||
|
||||
@statistics.handle()
|
||||
async def _(bot: Bot, event: MessageEvent, cmd: Tuple[str, ...] = Command(), arg: Message = CommandArg()):
|
||||
async def _(bot: Bot, event: MessageEvent, cmd: str = OneCommand(), arg: Message = CommandArg()):
|
||||
msg = arg.extract_plain_text().strip()
|
||||
if cmd[0][:2] == "全局":
|
||||
if str(event.user_id) in bot.config.superusers:
|
||||
|
||||
@@ -6,11 +6,10 @@ from nonebot.message import run_postprocessor
|
||||
from nonebot.typing import Optional, T_State
|
||||
|
||||
from configs.path_config import DATA_PATH
|
||||
from models.statistics import Statistics
|
||||
from utils.manager import plugins2settings_manager
|
||||
from utils.utils import scheduler
|
||||
|
||||
from ._model import Statistics
|
||||
|
||||
try:
|
||||
import ujson as json
|
||||
except ModuleNotFoundError:
|
||||
@@ -21,69 +20,69 @@ __zx_plugin_name__ = "功能调用统计 [Hidden]"
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = "HibiKier"
|
||||
|
||||
statistics_group_file = DATA_PATH / "statistics" / "_prefix_count.json"
|
||||
statistics_user_file = DATA_PATH / "statistics" / "_prefix_user_count.json"
|
||||
# statistics_group_file = DATA_PATH / "statistics" / "_prefix_count.json"
|
||||
# statistics_user_file = DATA_PATH / "statistics" / "_prefix_user_count.json"
|
||||
|
||||
try:
|
||||
with open(statistics_group_file, "r", encoding="utf8") as f:
|
||||
_prefix_count_dict = json.load(f)
|
||||
except (FileNotFoundError, ValueError):
|
||||
_prefix_count_dict = {
|
||||
"total_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"day_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"week_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"month_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"start_time": str(datetime.now().date()),
|
||||
"day_index": 0,
|
||||
}
|
||||
# try:
|
||||
# with open(statistics_group_file, "r", encoding="utf8") as f:
|
||||
# _prefix_count_dict = json.load(f)
|
||||
# except (FileNotFoundError, ValueError):
|
||||
# _prefix_count_dict = {
|
||||
# "total_statistics": {
|
||||
# "total": {},
|
||||
# },
|
||||
# "day_statistics": {
|
||||
# "total": {},
|
||||
# },
|
||||
# "week_statistics": {
|
||||
# "total": {},
|
||||
# },
|
||||
# "month_statistics": {
|
||||
# "total": {},
|
||||
# },
|
||||
# "start_time": str(datetime.now().date()),
|
||||
# "day_index": 0,
|
||||
# }
|
||||
|
||||
try:
|
||||
with open(statistics_user_file, "r", encoding="utf8") as f:
|
||||
_prefix_user_count_dict = json.load(f)
|
||||
except (FileNotFoundError, ValueError):
|
||||
_prefix_user_count_dict = {
|
||||
"total_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"day_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"week_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"month_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"start_time": str(datetime.now().date()),
|
||||
"day_index": 0,
|
||||
}
|
||||
# try:
|
||||
# with open(statistics_user_file, "r", encoding="utf8") as f:
|
||||
# _prefix_user_count_dict = json.load(f)
|
||||
# except (FileNotFoundError, ValueError):
|
||||
# _prefix_user_count_dict = {
|
||||
# "total_statistics": {
|
||||
# "total": {},
|
||||
# },
|
||||
# "day_statistics": {
|
||||
# "total": {},
|
||||
# },
|
||||
# "week_statistics": {
|
||||
# "total": {},
|
||||
# },
|
||||
# "month_statistics": {
|
||||
# "total": {},
|
||||
# },
|
||||
# "start_time": str(datetime.now().date()),
|
||||
# "day_index": 0,
|
||||
# }
|
||||
|
||||
|
||||
# 以前版本转换
|
||||
if _prefix_count_dict.get("day_index") is None:
|
||||
tmp = _prefix_count_dict.copy()
|
||||
_prefix_count_dict = {
|
||||
"total_statistics": tmp["total_statistics"],
|
||||
"day_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"week_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"month_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"start_time": tmp["start_time"],
|
||||
"day_index": 0,
|
||||
}
|
||||
# # 以前版本转换
|
||||
# if _prefix_count_dict.get("day_index") is None:
|
||||
# tmp = _prefix_count_dict.copy()
|
||||
# _prefix_count_dict = {
|
||||
# "total_statistics": tmp["total_statistics"],
|
||||
# "day_statistics": {
|
||||
# "total": {},
|
||||
# },
|
||||
# "week_statistics": {
|
||||
# "total": {},
|
||||
# },
|
||||
# "month_statistics": {
|
||||
# "total": {},
|
||||
# },
|
||||
# "start_time": tmp["start_time"],
|
||||
# "day_index": 0,
|
||||
# }
|
||||
|
||||
|
||||
# 添加命令次数
|
||||
@@ -95,7 +94,7 @@ async def _(
|
||||
event: MessageEvent,
|
||||
state: T_State,
|
||||
):
|
||||
global _prefix_count_dict
|
||||
# global _prefix_count_dict
|
||||
if (
|
||||
matcher.type == "message"
|
||||
and matcher.priority not in [1, 999]
|
||||
@@ -107,112 +106,112 @@ async def _(
|
||||
plugin_name=matcher.plugin_name,
|
||||
create_time=datetime.now(),
|
||||
)
|
||||
module = matcher.plugin_name
|
||||
day_index = _prefix_count_dict["day_index"]
|
||||
try:
|
||||
group_id = str(event.group_id)
|
||||
except AttributeError:
|
||||
group_id = "total"
|
||||
user_id = str(event.user_id)
|
||||
plugin_name = plugins2settings_manager.get_plugin_data(module)
|
||||
if plugin_name and plugin_name.cmd:
|
||||
plugin_name = plugin_name.cmd[0]
|
||||
check_exists_key(group_id, user_id, plugin_name)
|
||||
for data in [_prefix_count_dict, _prefix_user_count_dict]:
|
||||
data["total_statistics"]["total"][plugin_name] += 1
|
||||
data["day_statistics"]["total"][plugin_name] += 1
|
||||
data["week_statistics"]["total"][plugin_name] += 1
|
||||
data["month_statistics"]["total"][plugin_name] += 1
|
||||
# print(_prefix_count_dict)
|
||||
if group_id != "total":
|
||||
for data in [_prefix_count_dict, _prefix_user_count_dict]:
|
||||
if data == _prefix_count_dict:
|
||||
key = group_id
|
||||
else:
|
||||
key = user_id
|
||||
data["total_statistics"][key][plugin_name] += 1
|
||||
data["day_statistics"][key][plugin_name] += 1
|
||||
data["week_statistics"][key][str(day_index % 7)][plugin_name] += 1
|
||||
data["month_statistics"][key][str(day_index % 30)][plugin_name] += 1
|
||||
with open(statistics_group_file, "w", encoding="utf8") as f:
|
||||
json.dump(_prefix_count_dict, f, indent=4, ensure_ascii=False)
|
||||
with open(statistics_user_file, "w", encoding="utf8") as f:
|
||||
json.dump(_prefix_user_count_dict, f, ensure_ascii=False, indent=4)
|
||||
# module = matcher.plugin_name
|
||||
# day_index = _prefix_count_dict["day_index"]
|
||||
# try:
|
||||
# group_id = str(event.group_id)
|
||||
# except AttributeError:
|
||||
# group_id = "total"
|
||||
# user_id = str(event.user_id)
|
||||
# plugin_name = plugins2settings_manager.get_plugin_data(module)
|
||||
# if plugin_name and plugin_name.cmd:
|
||||
# plugin_name = plugin_name.cmd[0]
|
||||
# check_exists_key(group_id, user_id, plugin_name)
|
||||
# for data in [_prefix_count_dict, _prefix_user_count_dict]:
|
||||
# data["total_statistics"]["total"][plugin_name] += 1
|
||||
# data["day_statistics"]["total"][plugin_name] += 1
|
||||
# data["week_statistics"]["total"][plugin_name] += 1
|
||||
# data["month_statistics"]["total"][plugin_name] += 1
|
||||
# # print(_prefix_count_dict)
|
||||
# if group_id != "total":
|
||||
# for data in [_prefix_count_dict, _prefix_user_count_dict]:
|
||||
# if data == _prefix_count_dict:
|
||||
# key = group_id
|
||||
# else:
|
||||
# key = user_id
|
||||
# data["total_statistics"][key][plugin_name] += 1
|
||||
# data["day_statistics"][key][plugin_name] += 1
|
||||
# data["week_statistics"][key][str(day_index % 7)][plugin_name] += 1
|
||||
# data["month_statistics"][key][str(day_index % 30)][plugin_name] += 1
|
||||
# with open(statistics_group_file, "w", encoding="utf8") as f:
|
||||
# json.dump(_prefix_count_dict, f, indent=4, ensure_ascii=False)
|
||||
# with open(statistics_user_file, "w", encoding="utf8") as f:
|
||||
# json.dump(_prefix_user_count_dict, f, ensure_ascii=False, indent=4)
|
||||
|
||||
|
||||
def check_exists_key(group_id: str, user_id: str, plugin_name: str):
|
||||
global _prefix_count_dict, _prefix_user_count_dict
|
||||
for data in [_prefix_count_dict, _prefix_user_count_dict]:
|
||||
if data == _prefix_count_dict:
|
||||
key = group_id
|
||||
else:
|
||||
key = user_id
|
||||
if not data["total_statistics"]["total"].get(plugin_name):
|
||||
data["total_statistics"]["total"][plugin_name] = 0
|
||||
if not data["day_statistics"]["total"].get(plugin_name):
|
||||
data["day_statistics"]["total"][plugin_name] = 0
|
||||
if not data["week_statistics"]["total"].get(plugin_name):
|
||||
data["week_statistics"]["total"][plugin_name] = 0
|
||||
if not data["month_statistics"]["total"].get(plugin_name):
|
||||
data["month_statistics"]["total"][plugin_name] = 0
|
||||
# def check_exists_key(group_id: str, user_id: str, plugin_name: str):
|
||||
# global _prefix_count_dict, _prefix_user_count_dict
|
||||
# for data in [_prefix_count_dict, _prefix_user_count_dict]:
|
||||
# if data == _prefix_count_dict:
|
||||
# key = group_id
|
||||
# else:
|
||||
# key = user_id
|
||||
# if not data["total_statistics"]["total"].get(plugin_name):
|
||||
# data["total_statistics"]["total"][plugin_name] = 0
|
||||
# if not data["day_statistics"]["total"].get(plugin_name):
|
||||
# data["day_statistics"]["total"][plugin_name] = 0
|
||||
# if not data["week_statistics"]["total"].get(plugin_name):
|
||||
# data["week_statistics"]["total"][plugin_name] = 0
|
||||
# if not data["month_statistics"]["total"].get(plugin_name):
|
||||
# data["month_statistics"]["total"][plugin_name] = 0
|
||||
|
||||
if not data["total_statistics"].get(key):
|
||||
data["total_statistics"][key] = {}
|
||||
if not data["total_statistics"][key].get(plugin_name):
|
||||
data["total_statistics"][key][plugin_name] = 0
|
||||
if not data["day_statistics"].get(key):
|
||||
data["day_statistics"][key] = {}
|
||||
if not data["day_statistics"][key].get(plugin_name):
|
||||
data["day_statistics"][key][plugin_name] = 0
|
||||
# if not data["total_statistics"].get(key):
|
||||
# data["total_statistics"][key] = {}
|
||||
# if not data["total_statistics"][key].get(plugin_name):
|
||||
# data["total_statistics"][key][plugin_name] = 0
|
||||
# if not data["day_statistics"].get(key):
|
||||
# data["day_statistics"][key] = {}
|
||||
# if not data["day_statistics"][key].get(plugin_name):
|
||||
# data["day_statistics"][key][plugin_name] = 0
|
||||
|
||||
if key != "total":
|
||||
if not data["week_statistics"].get(key):
|
||||
data["week_statistics"][key] = {}
|
||||
if data["week_statistics"][key].get("0") is None:
|
||||
for i in range(7):
|
||||
data["week_statistics"][key][str(i)] = {}
|
||||
if data["week_statistics"][key]["0"].get(plugin_name) is None:
|
||||
for i in range(7):
|
||||
data["week_statistics"][key][str(i)][plugin_name] = 0
|
||||
# if key != "total":
|
||||
# if not data["week_statistics"].get(key):
|
||||
# data["week_statistics"][key] = {}
|
||||
# if data["week_statistics"][key].get("0") is None:
|
||||
# for i in range(7):
|
||||
# data["week_statistics"][key][str(i)] = {}
|
||||
# if data["week_statistics"][key]["0"].get(plugin_name) is None:
|
||||
# for i in range(7):
|
||||
# data["week_statistics"][key][str(i)][plugin_name] = 0
|
||||
|
||||
if not data["month_statistics"].get(key):
|
||||
data["month_statistics"][key] = {}
|
||||
if data["month_statistics"][key].get("0") is None:
|
||||
for i in range(30):
|
||||
data["month_statistics"][key][str(i)] = {}
|
||||
if data["month_statistics"][key]["0"].get(plugin_name) is None:
|
||||
for i in range(30):
|
||||
data["month_statistics"][key][str(i)][plugin_name] = 0
|
||||
# if not data["month_statistics"].get(key):
|
||||
# data["month_statistics"][key] = {}
|
||||
# if data["month_statistics"][key].get("0") is None:
|
||||
# for i in range(30):
|
||||
# data["month_statistics"][key][str(i)] = {}
|
||||
# if data["month_statistics"][key]["0"].get(plugin_name) is None:
|
||||
# for i in range(30):
|
||||
# data["month_statistics"][key][str(i)][plugin_name] = 0
|
||||
|
||||
|
||||
# 天
|
||||
@scheduler.scheduled_job(
|
||||
"cron",
|
||||
hour=0,
|
||||
minute=1,
|
||||
)
|
||||
async def _():
|
||||
for data in [_prefix_count_dict, _prefix_user_count_dict]:
|
||||
data["day_index"] += 1
|
||||
for x in data["day_statistics"].keys():
|
||||
for key in data["day_statistics"][x].keys():
|
||||
try:
|
||||
data["day_statistics"][x][key] = 0
|
||||
except KeyError:
|
||||
pass
|
||||
for type_ in ["week_statistics", "month_statistics"]:
|
||||
index = str(
|
||||
data["day_index"] % 7
|
||||
if type_ == "week_statistics"
|
||||
else data["day_index"] % 30
|
||||
)
|
||||
for x in data[type_].keys():
|
||||
try:
|
||||
for key in data[type_][x][index].keys():
|
||||
data[type_][x][index][key] = 0
|
||||
except KeyError:
|
||||
pass
|
||||
with open(statistics_group_file, "w", encoding="utf8") as f:
|
||||
json.dump(_prefix_count_dict, f, indent=4, ensure_ascii=False)
|
||||
with open(statistics_user_file, "w", encoding="utf8") as f:
|
||||
json.dump(_prefix_user_count_dict, f, indent=4, ensure_ascii=False)
|
||||
# @scheduler.scheduled_job(
|
||||
# "cron",
|
||||
# hour=0,
|
||||
# minute=1,
|
||||
# )
|
||||
# async def _():
|
||||
# for data in [_prefix_count_dict, _prefix_user_count_dict]:
|
||||
# data["day_index"] += 1
|
||||
# for x in data["day_statistics"].keys():
|
||||
# for key in data["day_statistics"][x].keys():
|
||||
# try:
|
||||
# data["day_statistics"][x][key] = 0
|
||||
# except KeyError:
|
||||
# pass
|
||||
# for type_ in ["week_statistics", "month_statistics"]:
|
||||
# index = str(
|
||||
# data["day_index"] % 7
|
||||
# if type_ == "week_statistics"
|
||||
# else data["day_index"] % 30
|
||||
# )
|
||||
# for x in data[type_].keys():
|
||||
# try:
|
||||
# for key in data[type_][x][index].keys():
|
||||
# data[type_][x][index][key] = 0
|
||||
# except KeyError:
|
||||
# pass
|
||||
# with open(statistics_group_file, "w", encoding="utf8") as f:
|
||||
# json.dump(_prefix_count_dict, f, indent=4, ensure_ascii=False)
|
||||
# with open(statistics_user_file, "w", encoding="utf8") as f:
|
||||
# json.dump(_prefix_user_count_dict, f, indent=4, ensure_ascii=False)
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
from typing import List
|
||||
from nonebot.adapters.onebot.v11 import MessageEvent
|
||||
from ._config import SearchType
|
||||
|
||||
|
||||
def parse_data(cmd: str, event: MessageEvent, superusers: List[str]):
|
||||
search_type = SearchType.TOTAL
|
||||
if cmd[:2] == "全局":
|
||||
if str(event.user_id) in superusers:
|
||||
if cmd[2] == '日':
|
||||
search_type = SearchType.DAY
|
||||
elif cmd[2] == '周':
|
||||
_type = SearchType.WEEK
|
||||
elif cmd[2] == '月':
|
||||
_type = SearchType.MONTH
|
||||
|
||||
Reference in New Issue
Block a user