| 86 | } |
| 87 | |
| 88 | class DbdjPlugin extends Plugin { |
| 89 | description: string = `点兵点将\n<code>${mainPrefix}dbdj 消息数 人数 文案</code> - 从最近的消息中随机抽取指定人数的用户`; |
| 90 | cmdHandlers: Record< |
| 91 | string, |
| 92 | (msg: Api.Message, trigger?: Api.Message) => Promise<void> |
| 93 | > = { |
| 94 | dbdj: async (msg: Api.Message, trigger?: Api.Message) => { |
| 95 | const startAt = Date.now(); |
| 96 | const replyAndDeleteMsg = async (message: string) => { |
| 97 | const replyTarget = trigger || msg; |
| 98 | await replyTarget.reply({ |
| 99 | message, |
| 100 | parseMode: "html", |
| 101 | linkPreview: false, |
| 102 | }); |
| 103 | try { |
| 104 | await msg.delete(); |
| 105 | } catch {} |
| 106 | }; |
| 107 | |
| 108 | try { |
| 109 | const parts = msg.message.trim().split(/\s+/); |
| 110 | // 期望格式: .dbdj 消息数 人数 文案... |
| 111 | const countStr = parts[1]; |
| 112 | const pickStr = parts[2]; |
| 113 | const note = parts.slice(3).join(" "); |
| 114 | |
| 115 | const scanCount = toInt(countStr); |
| 116 | const pickCount = toInt(pickStr); |
| 117 | |
| 118 | if (!scanCount || !pickCount || scanCount <= 0 || pickCount <= 0) { |
| 119 | await replyAndDeleteMsg( |
| 120 | `用法: <code>${mainPrefix}dbdj 消息数 人数 文案</code>\n例如: <code>${mainPrefix}dbdj 50 2 恭喜发财</code>`, |
| 121 | ); |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | await msg.edit({ |
| 126 | text: `点兵点将...`, |
| 127 | parseMode: "html", |
| 128 | }); |
| 129 | |
| 130 | const client = msg.client!; |
| 131 | const offsetId = (msg.id || 1) - 1; // 从命令消息之前开始 |
| 132 | const messages = await safeGetMessages(client, msg.peerId, { |
| 133 | offsetId, |
| 134 | limit: scanCount, |
| 135 | }); |
| 136 | |
| 137 | // 收集有效用户: 仅统计来自用户的消息, 排除自身(out)、无 fromId 的消息 |
| 138 | const uniqueUserIds: number[] = []; |
| 139 | const seen = new Set<number>(); |
| 140 | const filtered = new Set<number>(); |
| 141 | |
| 142 | for (const m of messages) { |
| 143 | // 跳过自己发送的消息 |
| 144 | // if ((m as any).out) continue; |
| 145 | const from = (m as any).fromId as any; |
nothing calls this directly
no test coverage detected