Files
HeXi/hexi/core/custom_utils.py
T
sansenhoshi 131b92b319 结构调整
视频解析多图/多媒体结构 消息体适配
2026-09-08 14:25:32 +08:00

61 lines
1.7 KiB
Python

from typing import List
from nonebot import get_plugin_config
from nonebot.adapters import Event
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, MessageEvent
from pydantic import BaseModel
class Config(BaseModel):
# 管理员的QQ号
SUPERUSERS: List[str] = [] # 必填
pc = get_plugin_config(Config)
# 权限等级(与 hoshino 的 priv 保持一致)
USER = 1
GROUP_ADMIN = 2
GROUP_OWNER = 3
SUPERUSER = 999
# 管理操作所需的最低权限等级
MANAGE_PRIV = GROUP_ADMIN
async def check_admin(event: Event) -> bool:
return event.get_user_id() in pc.SUPERUSERS
async def get_user_priv(bot: Bot, event: MessageEvent) -> int:
"""获取用户权限等级:超管=999 群主=3 群管理=2 普通群员=1"""
if str(event.user_id) in pc.SUPERUSERS:
return SUPERUSER
if not isinstance(event, GroupMessageEvent):
return USER
try:
info = await bot.get_group_member_info(
group_id=event.group_id, user_id=event.user_id
)
role = info.get("role")
if role == "owner":
return GROUP_OWNER
if role == "admin":
return GROUP_ADMIN
except Exception:
pass
return USER
async def check_manage(bot: Bot, event: MessageEvent) -> bool:
"""是否为管理及以上权限"""
return await get_user_priv(bot, event) >= MANAGE_PRIV
async def check_bot_admin(bot: Bot, group_id: int) -> bool:
"""bot 是否为该群的群管理/群主"""
try:
info = await bot.get_group_member_info(
group_id=group_id, user_id=bot.self_id
)
return info.get("role") != "member"
except Exception:
return False