群活跃度记录表: 群最后有人使用本插件的时间
| 17 | |
| 18 | |
| 19 | class RoverGroupActivity(BaseBotIDModel, table=True): |
| 20 | """群活跃度记录表: 群最后有人使用本插件的时间""" |
| 21 | |
| 22 | __tablename__ = "RoverGroupActivity" |
| 23 | __table_args__: Dict[str, Any] = {"extend_existing": True} |
| 24 | |
| 25 | group_id: str = Field(default="", title="群组ID") |
| 26 | bot_self_id: str = Field(default="", title="机器人自身ID") |
| 27 | last_active_time: Optional[int] = Field(default=None, title="最后活跃时间") |
| 28 | |
| 29 | @classmethod |
| 30 | async def update_group_activity( |
| 31 | cls: Type[T_RoverGroupActivity], |
| 32 | group_id: str, |
| 33 | bot_id: str, |
| 34 | bot_self_id: str, |
| 35 | ) -> bool: |
| 36 | try: |
| 37 | return await cls._do_update_group_activity(group_id, bot_id, bot_self_id) |
| 38 | except Exception as e: |
| 39 | if "malformed" in str(e) or "corrupt" in str(e): |
| 40 | logger.warning(f"[库洛签到·群活跃度] 数据库损坏,跳过更新: {e}") |
| 41 | return False |
| 42 | raise |
| 43 | |
| 44 | @classmethod |
| 45 | @with_lock |
| 46 | @with_session |
| 47 | async def _do_update_group_activity( |
| 48 | cls: Type[T_RoverGroupActivity], |
| 49 | session: AsyncSession, |
| 50 | group_id: str, |
| 51 | bot_id: str, |
| 52 | bot_self_id: str, |
| 53 | ) -> bool: |
| 54 | import time |
| 55 | |
| 56 | current_time = int(time.time()) |
| 57 | sql = select(cls).where( |
| 58 | and_( |
| 59 | cls.group_id == group_id, |
| 60 | cls.bot_id == bot_id, |
| 61 | cls.bot_self_id == bot_self_id, |
| 62 | ) |
| 63 | ) |
| 64 | result = await session.execute(sql) |
| 65 | existing = result.scalars().first() |
| 66 | if existing: |
| 67 | existing.last_active_time = current_time |
| 68 | session.add(existing) |
| 69 | else: |
| 70 | session.add( |
| 71 | cls( |
| 72 | group_id=group_id, |
| 73 | bot_id=bot_id, |
| 74 | bot_self_id=bot_self_id, |
| 75 | last_active_time=current_time, |
| 76 | ) |
nothing calls this directly
no outgoing calls
no test coverage detected