✨ 新增插件商店api (#1659)

* ✨ 新增插件商店api

* chore(version): Update version to v0.2.2-7e15f20

---------

Co-authored-by: HibiKier <HibiKier@users.noreply.github.com>
This commit is contained in:
HibiKier
2024-09-29 20:47:58 +08:00
committed by GitHub
co-authored by HibiKier
parent e8425f371c
commit d68a4099ba
20 changed files with 419 additions and 248 deletions
@@ -0,0 +1,22 @@
from nonebot import require
from fastapi import APIRouter
from ....base_model import Result
from .data_source import BotManage
from ....utils import authentication
require("plugin_store")
router = APIRouter(prefix="/dashboard")
@router.get(
"/get_bot_list",
dependencies=[authentication()],
deprecated="获取bot列表", # type: ignore
)
async def _() -> Result:
try:
return Result.ok(await BotManage.get_bot_list(), "拿到信息啦!")
except Exception as e:
return Result.fail(f"发生了一点错误捏 {type(e)}: {e}")
@@ -0,0 +1,81 @@
import time
from datetime import datetime, timedelta
import nonebot
from nonebot.adapters import Bot
from nonebot.drivers import Driver
from zhenxun.models.statistics import Statistics
from zhenxun.utils.platform import PlatformUtils
from zhenxun.models.chat_history import ChatHistory
from .model import BotInfo
from ..main.data_source import bot_live
driver: Driver = nonebot.get_driver()
CONNECT_TIME = 0
@driver.on_startup
async def _():
global CONNECT_TIME
CONNECT_TIME = int(time.time())
class BotManage:
@classmethod
async def __build_bot_info(cls, bot: Bot) -> BotInfo:
"""构建Bot信息
参数:
bot: Bot
返回:
BotInfo: Bot信息
"""
now = datetime.now()
platform = PlatformUtils.get_platform(bot) or ""
if platform == "qq":
login_info = await bot.get_login_info()
nickname = login_info["nickname"]
ava_url = PlatformUtils.get_user_avatar_url(bot.self_id, "qq") or ""
else:
nickname = bot.self_id
ava_url = ""
bot_info = BotInfo(
self_id=bot.self_id, nickname=nickname, ava_url=ava_url, platform=platform
)
group_list, _ = await PlatformUtils.get_group_list(bot)
group_list = [g for g in group_list if g.channel_id is None]
friend_list = await PlatformUtils.get_friend_list(bot)
bot_info.group_count = len(group_list)
bot_info.friend_count = len(friend_list)
bot_info.day_call = await Statistics.filter(
create_time__gte=now - timedelta(hours=now.hour, minutes=now.minute)
).count()
bot_info.received_messages = await ChatHistory.filter(
bot_id=bot_info.self_id,
create_time__gte=now - timedelta(hours=now.hour, minutes=now.minute),
).count()
bot_info.connect_time = bot_live.get(bot.self_id) or 0
if bot_info.connect_time:
connect_date = datetime.fromtimestamp(CONNECT_TIME)
connect_date_str = connect_date.strftime("%Y-%m-%d %H:%M:%S")
bot_info.connect_date = datetime.strptime(
connect_date_str, "%Y-%m-%d %H:%M:%S"
)
return bot_info
@classmethod
async def get_bot_list(cls) -> list[BotInfo]:
"""获取bot列表
返回:
list[BotInfo]: Bot列表
"""
bot_list: list[BotInfo] = []
for _, bot in nonebot.get_bots().items():
bot_list.append(await cls.__build_bot_info(bot))
return bot_list
@@ -0,0 +1,26 @@
from datetime import datetime
from pydantic import BaseModel
class BotInfo(BaseModel):
self_id: str
"""SELF ID"""
nickname: str
"""昵称"""
ava_url: str
"""头像url"""
platform: str
"""平台"""
friend_count: int = 0
"""好友数量"""
group_count: int = 0
"""群聊数量"""
received_messages: int = 0
"""今日消息接收"""
day_call: int = 0
"""今日调用插件次数"""
connect_time: int = 0
"""连接时间"""
connect_date: datetime | None = None
"""连接日期"""