134 lines
4.9 KiB
Python
134 lines
4.9 KiB
Python
from typing import Dict, Any, List, Optional, Tuple
|
|
from loguru import logger
|
|
|
|
from base.plugin_common.message_plugin_interface import MessagePluginInterface
|
|
from base.plugin_common.plugin_interface import PluginStatus
|
|
from utils.decorator.plugin_decorators import plugin_stats_decorator
|
|
from utils.robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager
|
|
from db.connection import DBConnectionManager
|
|
from db.contacts_db import ContactsDBOperator
|
|
from wechat_ipad import WechatAPIClient
|
|
|
|
|
|
class InactiveRankPlugin(MessagePluginInterface):
|
|
FEATURE_KEY = "INACTIVE_RANK"
|
|
FEATURE_DESCRIPTION = "📉 潜水排行 [潜水排行]"
|
|
|
|
@property
|
|
def name(self) -> str:
|
|
return "潜水排行"
|
|
|
|
@property
|
|
def version(self) -> str:
|
|
return "1.0.0"
|
|
|
|
@property
|
|
def description(self) -> str:
|
|
return "统计群内在指定天数内未活跃的成员排行"
|
|
|
|
@property
|
|
def author(self) -> str:
|
|
return "liu.wei"
|
|
|
|
@property
|
|
def command_prefix(self) -> Optional[str]:
|
|
return ""
|
|
|
|
@property
|
|
def commands(self) -> List[str]:
|
|
return self._commands
|
|
|
|
@property
|
|
def feature_key(self) -> Optional[str]:
|
|
return self.FEATURE_KEY
|
|
|
|
@property
|
|
def feature_description(self) -> Optional[str]:
|
|
return self.FEATURE_DESCRIPTION
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.feature = self.register_feature()
|
|
|
|
def initialize(self, context: Dict[str, Any]) -> bool:
|
|
self.LOG = logger
|
|
self._commands = self._config.get("InactiveRank", {}).get("command", ["潜水排行"])
|
|
self.command_format = self._config.get("InactiveRank", {}).get("command-format", "潜水排行 [天数] [名次]")
|
|
self.enable = self._config.get("InactiveRank", {}).get("enable", True)
|
|
self.default_days = int(self._config.get("InactiveRank", {}).get("default_days", 60))
|
|
self.default_limit = int(self._config.get("InactiveRank", {}).get("default_limit", 10))
|
|
return True
|
|
|
|
def start(self) -> bool:
|
|
self.status = PluginStatus.RUNNING
|
|
return True
|
|
|
|
def stop(self) -> bool:
|
|
self.status = PluginStatus.STOPPED
|
|
return True
|
|
|
|
def can_process(self, message: Dict[str, Any]) -> bool:
|
|
if not self.enable:
|
|
return False
|
|
content = str(message.get("content", "")).strip()
|
|
command = content.split(" ")[0]
|
|
return command in self._commands
|
|
|
|
@plugin_stats_decorator(plugin_name="潜水排行")
|
|
async def process_message(self, message: Dict[str, Any]) -> Tuple[bool, Optional[str]]:
|
|
content = str(message.get("content", "")).strip()
|
|
command = content.split(" ")[0]
|
|
sender = message.get("sender")
|
|
roomid = message.get("roomid", "")
|
|
gbm: GroupBotManager = message.get("gbm")
|
|
bot: WechatAPIClient = message.get("bot")
|
|
|
|
if roomid and gbm.get_group_permission(roomid, self.feature) == PermissionStatus.DISABLED:
|
|
return False, "没有权限"
|
|
|
|
if not roomid:
|
|
await bot.send_text_message((sender), "请在群聊中使用该功能", sender)
|
|
return False, "非群聊"
|
|
|
|
parts = content.split()
|
|
days = self.default_days
|
|
limit = self.default_limit
|
|
if len(parts) >= 2:
|
|
try:
|
|
days = int(parts[1])
|
|
except Exception:
|
|
await bot.send_text_message(roomid, f"❌命令格式错误!\n{self.command_format}", sender)
|
|
return False, "命令格式错误"
|
|
if len(parts) >= 3:
|
|
try:
|
|
limit = int(parts[2])
|
|
except Exception:
|
|
await bot.send_text_message(roomid, f"❌命令格式错误!\n{self.command_format}", sender)
|
|
return False, "命令格式错误"
|
|
|
|
db = ContactsDBOperator(DBConnectionManager.get_instance())
|
|
rows = db.get_inactive_members_rank(roomid, days, limit)
|
|
if not rows:
|
|
await bot.send_text_message(roomid, f"📉 {days}天内暂无潜水成员", sender)
|
|
return True, "暂无数据"
|
|
|
|
lines = [f"📉潜水排行榜 \n(≥{days}天 未发言,前{limit}名)"]
|
|
for i, r in enumerate(rows, 1):
|
|
name = (r.get("nick_name") or r.get("wxid") or "").strip()
|
|
inactivity = int(r.get("inactivity_days", 0))
|
|
last = r.get("latest_active_time")
|
|
never = int(r.get("never_spoken", 0))
|
|
status = "从未发言" if (never == 1 or inactivity >= 999999 or not last) else f"{inactivity}天未发言"
|
|
if i == 1:
|
|
prefix = "🥇 "
|
|
elif i == 2:
|
|
prefix = "🥈 "
|
|
elif i == 3:
|
|
prefix = "🥉 "
|
|
else:
|
|
prefix = f" {i}. "
|
|
lines.append(f"{prefix}{name} -> {status}")
|
|
msg = "\n".join(lines)
|
|
await bot.send_text_message(roomid, msg, sender)
|
|
return True, "已发送"
|