from __future__ import annotations from typing import Optional from Plugins.WPSAPI import GuideEntry from .WPSRedPacketBase import WPSRedPacketBase class WPSPasswordRedPacket(WPSRedPacketBase): """口令红包入口插件""" def get_guide_subtitle(self) -> str: return "发布口令红包,需要准确口令才能领取" def collect_command_entries(self): return ( GuideEntry( title="口令红包", identifier="口令红包 <金额> <人数> <口令>", description="设置好口令后,领取者需在抢红包时附带同样的口令。", ), ) def wake_up(self) -> None: self.register_plugin("口令红包") async def callback(self, message: str, chat_id: int, user_id: int) -> Optional[str]: payload = self.parse_message_after_at(message).strip() tokens = [token.strip() for token in payload.split() if token.strip()] service = self.red_packet_service() try: amount, slots, remaining = self.parse_amount_and_slots(tokens) if not remaining: raise ValueError("需要提供口令参数") password = " ".join(remaining) self.validate_amount_and_slots(amount, slots) self.ensure_sufficient_points(user_id, amount) packet = service.create_password_packet(chat_id, user_id, amount, slots, password) hint = ( f"- 提示:抢红包时使用 ``抢红包 {packet.packet_id} {password}`` " "(口令必须完全一致)。" ) return await self.send_markdown_message( self.format_packet_message(packet, hint), chat_id, user_id, ) except ValueError as exc: return await self.send_markdown_message(self.format_error(str(exc)), chat_id, user_id) __all__ = ["WPSPasswordRedPacket"]