koishi-plugin-yyds-verifier 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ import { Context, Schema } from "koishi";
2
+ declare module "koishi" {
3
+ interface Context {
4
+ canvas: {
5
+ createCanvas(width: number, height: number): any;
6
+ };
7
+ }
8
+ }
9
+ export declare const name = "yyds-verifier";
10
+ export declare const inject: string[];
11
+ export declare const usage = "\n## \u4F7F\u7528\u8BF4\u660E\n\n\u672C\u63D2\u4EF6\u9700\u8981\u914D\u5408 `koishi-plugin-skia-canvas` \u4F7F\u7528\uFF0C\u8BF7\u786E\u4FDD\u5DF2\u5B89\u88C5\u5E76\u542F\u7528\u8BE5\u63D2\u4EF6\u3002\n\n### \u9A8C\u8BC1\u65B9\u5F0F\n- **code**: \u6570\u5B57\u9A8C\u8BC1\u7801\uFF0C\u7528\u6237\u9700\u8981\u8F93\u5165\u663E\u793A\u7684\u6570\u5B57\n- **math**: \u7B97\u6570\u9A8C\u8BC1\uFF0C\u7528\u6237\u9700\u8981\u8BA1\u7B97\u7B80\u5355\u7684\u52A0\u6CD5\u6216\u4E58\u6CD5\n- **image**: \u56FE\u50CF\u9A8C\u8BC1\u7801\uFF0C\u7528\u6237\u9700\u8981\u8BC6\u522B\u56FE\u7247\u4E2D\u7684\u5B57\u7B26\n- **random**: \u968F\u673A\u9009\u62E9\u4EE5\u4E0A\u4E09\u79CD\u65B9\u5F0F\u4E4B\u4E00\n";
12
+ type VerifyMode = "code" | "math" | "image" | "random";
13
+ export interface Config {
14
+ mode: VerifyMode;
15
+ timeout: number;
16
+ maxAttempts: number;
17
+ permanent: boolean;
18
+ codeLength: number;
19
+ whitelist: string[];
20
+ messages: {
21
+ prompt: string;
22
+ success: string;
23
+ failure: string;
24
+ timeout: string;
25
+ kicked: string;
26
+ };
27
+ }
28
+ export declare const Config: Schema<Config>;
29
+ export declare function apply(ctx: Context, config: Config): void;
30
+ export {};
package/lib/index.js ADDED
@@ -0,0 +1,265 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __export = (target, all) => {
7
+ for (var name2 in all)
8
+ __defProp(target, name2, { get: all[name2], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ Config: () => Config,
24
+ apply: () => apply,
25
+ inject: () => inject,
26
+ name: () => name,
27
+ usage: () => usage
28
+ });
29
+ module.exports = __toCommonJS(src_exports);
30
+ var import_koishi = require("koishi");
31
+ var name = "yyds-verifier";
32
+ var inject = ["canvas"];
33
+ var usage = `
34
+ ## 使用说明
35
+
36
+ 本插件需要配合 \`koishi-plugin-skia-canvas\` 使用,请确保已安装并启用该插件。
37
+
38
+ ### 验证方式
39
+ - **code**: 数字验证码,用户需要输入显示的数字
40
+ - **math**: 算数验证,用户需要计算简单的加法或乘法
41
+ - **image**: 图像验证码,用户需要识别图片中的字符
42
+ - **random**: 随机选择以上三种方式之一
43
+ `;
44
+ var logger = new import_koishi.Logger("yyds-verifier");
45
+ var Config = import_koishi.Schema.object({
46
+ mode: import_koishi.Schema.union(["code", "math", "image", "random"]).default("code").description(
47
+ "验证方式:code=数字验证码, math=算数验证, image=图像验证码, random=随机"
48
+ ),
49
+ timeout: import_koishi.Schema.number().default(300).min(10).description("验证超时时间(秒)"),
50
+ maxAttempts: import_koishi.Schema.number().default(3).min(1).max(10).description("最大尝试次数"),
51
+ permanent: import_koishi.Schema.boolean().default(false).description("是否永久踢出(加入群黑名单,禁止再次加入)"),
52
+ codeLength: import_koishi.Schema.number().default(4).min(4).max(8).description("验证码长度(仅对 code 和 image 模式有效)"),
53
+ whitelist: import_koishi.Schema.array(String).default([]).description("白名单用户 ID 列表,这些用户入群时不需要验证"),
54
+ messages: import_koishi.Schema.object({
55
+ prompt: import_koishi.Schema.string().default(
56
+ "欢迎加入!请在 {timeout} 内完成验证。{question} 当前第 {attempt}/{maxAttempts} 次机会。"
57
+ ).description(
58
+ "验证提示消息。可用变量:{timeout} 超时时间, {question} 验证问题, {attempt} 当前次数, {maxAttempts} 最大次数"
59
+ ),
60
+ success: import_koishi.Schema.string().default("验证成功!欢迎加入本群。").description("验证成功消息"),
61
+ failure: import_koishi.Schema.string().default("验证失败,请重新输入。剩余 {remaining} 次机会。").description("验证失败消息。可用变量:{remaining} 剩余次数"),
62
+ timeout: import_koishi.Schema.string().default("验证超时,进入下一轮验证。剩余 {remaining} 次机会。").description("超时提示消息。可用变量:{remaining} 剩余次数"),
63
+ kicked: import_koishi.Schema.string().default("验证失败次数过多,已被移出群聊。").description("踢出消息")
64
+ }).description("自定义消息模板")
65
+ });
66
+ function apply(ctx, config) {
67
+ const verifyingUsers = /* @__PURE__ */ new Set();
68
+ const timeoutMs = config.timeout * 1e3;
69
+ function randomColor() {
70
+ const r = Math.floor(Math.random() * 200);
71
+ const g = Math.floor(Math.random() * 200);
72
+ const b = Math.floor(Math.random() * 200);
73
+ return `rgb(${r},${g},${b})`;
74
+ }
75
+ __name(randomColor, "randomColor");
76
+ function generateRandomCode(length) {
77
+ let code = "";
78
+ for (let i = 0; i < length; i++) {
79
+ code += Math.floor(Math.random() * 10);
80
+ }
81
+ return code;
82
+ }
83
+ __name(generateRandomCode, "generateRandomCode");
84
+ function generateCodeChallenge() {
85
+ const code = generateRandomCode(config.codeLength);
86
+ return {
87
+ prompt: `请发送验证码 ${code}`,
88
+ answer: code
89
+ };
90
+ }
91
+ __name(generateCodeChallenge, "generateCodeChallenge");
92
+ function generateMathChallenge() {
93
+ const a = Math.floor(Math.random() * 9) + 1;
94
+ const b = Math.floor(Math.random() * 9) + 1;
95
+ const ops = ["+", "×"];
96
+ const op = ops[Math.floor(Math.random() * 2)];
97
+ const answer = op === "+" ? a + b : a * b;
98
+ return {
99
+ prompt: `请计算 ${a} ${op} ${b} = ?`,
100
+ answer: String(answer)
101
+ };
102
+ }
103
+ __name(generateMathChallenge, "generateMathChallenge");
104
+ async function generateImageChallenge() {
105
+ const code = generateRandomCode(config.codeLength);
106
+ const width = 30 * config.codeLength + 20;
107
+ const height = 50;
108
+ const canvas = ctx.canvas.createCanvas(width, height);
109
+ const c = canvas.getContext("2d");
110
+ c.fillStyle = "#f0f0f0";
111
+ c.fillRect(0, 0, width, height);
112
+ for (let i = 0; i < 5; i++) {
113
+ c.strokeStyle = randomColor();
114
+ c.lineWidth = 1;
115
+ c.beginPath();
116
+ c.moveTo(Math.random() * width, Math.random() * height);
117
+ c.lineTo(Math.random() * width, Math.random() * height);
118
+ c.stroke();
119
+ }
120
+ for (let i = 0; i < 50; i++) {
121
+ c.fillStyle = randomColor();
122
+ c.beginPath();
123
+ c.arc(Math.random() * width, Math.random() * height, 1, 0, 2 * Math.PI);
124
+ c.fill();
125
+ }
126
+ c.font = "bold 32px Arial";
127
+ c.textBaseline = "middle";
128
+ for (let i = 0; i < code.length; i++) {
129
+ c.fillStyle = randomColor();
130
+ const x = 10 + i * 30 + Math.random() * 5;
131
+ const y = height / 2 + Math.random() * 10 - 5;
132
+ c.fillText(code[i], x, y);
133
+ }
134
+ const buffer = canvas.toBuffer("image/png");
135
+ return {
136
+ prompt: import_koishi.h.image(buffer, "image/png"),
137
+ answer: code
138
+ };
139
+ }
140
+ __name(generateImageChallenge, "generateImageChallenge");
141
+ async function generateChallenge(mode) {
142
+ const actualMode = mode === "random" ? ["code", "math", "image"][Math.floor(Math.random() * 3)] : mode;
143
+ switch (actualMode) {
144
+ case "code":
145
+ return generateCodeChallenge();
146
+ case "math":
147
+ return generateMathChallenge();
148
+ case "image":
149
+ return generateImageChallenge();
150
+ }
151
+ }
152
+ __name(generateChallenge, "generateChallenge");
153
+ function formatMessage(template, params) {
154
+ return template.replace(
155
+ /\{(\w+)\}/g,
156
+ (_, key) => params[key] !== void 0 ? String(params[key]) : `{${key}}`
157
+ );
158
+ }
159
+ __name(formatMessage, "formatMessage");
160
+ function formatTimeout(seconds) {
161
+ if (seconds >= 60) {
162
+ const minutes = Math.floor(seconds / 60);
163
+ return `${minutes} 分钟`;
164
+ }
165
+ return `${seconds} 秒`;
166
+ }
167
+ __name(formatTimeout, "formatTimeout");
168
+ function extractAnswer(content) {
169
+ return content.replace(/<at[^>]*\/>/g, "").trim();
170
+ }
171
+ __name(extractAnswer, "extractAnswer");
172
+ ctx.on("guild-member-added", async (session) => {
173
+ const { userId, guildId, selfId, bot } = session;
174
+ if (userId === selfId) return;
175
+ if (config.whitelist.includes(userId)) {
176
+ logger.info(`User ${userId} is in whitelist, skipping verification`);
177
+ return;
178
+ }
179
+ const userKey = `${guildId}:${userId}`;
180
+ if (verifyingUsers.has(userKey)) return;
181
+ verifyingUsers.add(userKey);
182
+ logger.info(
183
+ `New member ${userId} joined guild ${guildId}, starting verification`
184
+ );
185
+ try {
186
+ for (let attempt = 1; attempt <= config.maxAttempts; attempt++) {
187
+ const challenge = await generateChallenge(config.mode);
188
+ const timeoutDisplay = formatTimeout(config.timeout);
189
+ const remaining = config.maxAttempts - attempt;
190
+ const promptText = formatMessage(config.messages.prompt, {
191
+ timeout: timeoutDisplay,
192
+ question: typeof challenge.prompt === "string" ? challenge.prompt : "请识别图片中的验证码",
193
+ attempt,
194
+ maxAttempts: config.maxAttempts
195
+ });
196
+ const messageElements = [
197
+ (0, import_koishi.h)("at", { id: userId }),
198
+ " ",
199
+ promptText
200
+ ];
201
+ if (typeof challenge.prompt !== "string") {
202
+ messageElements.push("\n", challenge.prompt);
203
+ }
204
+ await session.send(messageElements);
205
+ logger.debug(
206
+ `Verification for ${userKey}, attempt ${attempt}, answer: ${challenge.answer}`
207
+ );
208
+ const input = await session.prompt(timeoutMs);
209
+ if (!input) {
210
+ if (remaining > 0) {
211
+ await session.send([
212
+ (0, import_koishi.h)("at", { id: userId }),
213
+ " ",
214
+ formatMessage(config.messages.timeout, { remaining })
215
+ ]);
216
+ continue;
217
+ } else {
218
+ break;
219
+ }
220
+ }
221
+ const userAnswer = extractAnswer(input);
222
+ if (userAnswer === challenge.answer) {
223
+ await session.send([
224
+ (0, import_koishi.h)("at", { id: userId }),
225
+ " ",
226
+ config.messages.success
227
+ ]);
228
+ logger.info(`User ${userId} passed verification in guild ${guildId}`);
229
+ return;
230
+ } else {
231
+ if (remaining > 0) {
232
+ await session.send([
233
+ (0, import_koishi.h)("at", { id: userId }),
234
+ " ",
235
+ formatMessage(config.messages.failure, { remaining })
236
+ ]);
237
+ continue;
238
+ } else {
239
+ break;
240
+ }
241
+ }
242
+ }
243
+ await session.send([
244
+ (0, import_koishi.h)("at", { id: userId }),
245
+ " ",
246
+ config.messages.kicked
247
+ ]);
248
+ await bot.kickGuildMember(guildId, userId, config.permanent);
249
+ logger.info(`Kicked user ${userId} from guild ${guildId}`);
250
+ } catch (error) {
251
+ logger.error(`Verification error for ${userId}:`, error);
252
+ } finally {
253
+ verifyingUsers.delete(userKey);
254
+ }
255
+ });
256
+ }
257
+ __name(apply, "apply");
258
+ // Annotate the CommonJS export names for ESM import in node:
259
+ 0 && (module.exports = {
260
+ Config,
261
+ apply,
262
+ inject,
263
+ name,
264
+ usage
265
+ });
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "koishi-plugin-yyds-verifier",
3
+ "description": "入群验证插件,支持数字验证码、算数验证、图像验证码等多种验证方式",
4
+ "version": "0.0.3",
5
+ "main": "lib/index.js",
6
+ "typings": "lib/index.d.ts",
7
+ "files": [
8
+ "lib",
9
+ "dist"
10
+ ],
11
+ "license": "MIT",
12
+ "keywords": [
13
+ "chatbot",
14
+ "koishi",
15
+ "plugin",
16
+ "verification",
17
+ "captcha"
18
+ ],
19
+ "peerDependencies": {
20
+ "koishi": "^4.18.10",
21
+ "koishi-plugin-skia-canvas": "^0.4.8"
22
+ }
23
+ }
package/readme.md ADDED
@@ -0,0 +1,139 @@
1
+ # koishi-plugin-yyds-verifier
2
+
3
+ [![npm](https://img.shields.io/npm/v/koishi-plugin-yyds-verifier?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-yyds-verifier)
4
+
5
+ 入群验证插件,支持多种验证方式,有效防止机器人和恶意用户入群。
6
+
7
+ ## 功能特性
8
+
9
+ - **多种验证方式**
10
+ - 数字验证码:用户需要输入显示的数字
11
+ - 算数验证:用户需要计算简单的加法或乘法(如 3+5=? 或 4×7=?)
12
+ - 图像验证码:用户需要识别图片中的字符
13
+ - 随机模式:随机选择以上三种方式之一
14
+
15
+ - **白名单功能**:指定用户入群时无需验证
16
+
17
+ - **可配置参数**
18
+ - 验证超时时间
19
+ - 最大尝试次数
20
+ - 验证码长度
21
+ - 自定义消息模板
22
+
23
+ - **安全特性**
24
+ - 验证失败自动踢出
25
+ - 可选永久踢出(加入群黑名单)
26
+
27
+ ## 安装
28
+
29
+ ### 前置依赖
30
+
31
+ 本插件需要 `koishi-plugin-skia-canvas` 作为依赖,请先安装:
32
+
33
+ ```bash
34
+ npm install koishi-plugin-skia-canvas
35
+ ```
36
+
37
+ ### 安装插件
38
+
39
+ ```bash
40
+ npm install koishi-plugin-yyds-verifier
41
+ ```
42
+
43
+ ## 配置项
44
+
45
+ | 配置项 | 类型 | 默认值 | 说明 |
46
+ |--------|------|--------|------|
47
+ | mode | string | `'code'` | 验证方式:`code`=数字验证码, `math`=算数验证, `image`=图像验证码, `random`=随机 |
48
+ | timeout | number | `300` | 验证超时时间(秒) |
49
+ | maxAttempts | number | `3` | 最大尝试次数 |
50
+ | permanent | boolean | `false` | 是否永久踢出(加入群黑名单) |
51
+ | codeLength | number | `4` | 验证码长度(仅对 code 和 image 模式有效) |
52
+ | whitelist | string[] | `[]` | 白名单用户 ID 列表 |
53
+ | messages | object | - | 自定义消息模板 |
54
+
55
+ ### 消息模板
56
+
57
+ | 配置项 | 默认值 | 可用变量 |
58
+ |--------|--------|----------|
59
+ | messages.prompt | `'欢迎加入!请在 {timeout} 内完成验证。{question} 当前第 {attempt}/{maxAttempts} 次机会。'` | `{timeout}` `{question}` `{attempt}` `{maxAttempts}` |
60
+ | messages.success | `'验证成功!欢迎加入本群。'` | - |
61
+ | messages.failure | `'验证失败,请重新输入。剩余 {remaining} 次机会。'` | `{remaining}` |
62
+ | messages.timeout | `'验证超时,进入下一轮验证。剩余 {remaining} 次机会。'` | `{remaining}` |
63
+ | messages.kicked | `'验证失败次数过多,已被移出群聊。'` | - |
64
+
65
+ ## 使用示例
66
+
67
+ ### 基础配置
68
+
69
+ ```yaml
70
+ plugins:
71
+ yyds-verifier:
72
+ mode: code
73
+ timeout: 300
74
+ maxAttempts: 3
75
+ ```
76
+
77
+ ### 使用算数验证
78
+
79
+ ```yaml
80
+ plugins:
81
+ yyds-verifier:
82
+ mode: math
83
+ timeout: 120
84
+ maxAttempts: 3
85
+ ```
86
+
87
+ ### 使用图像验证码
88
+
89
+ ```yaml
90
+ plugins:
91
+ yyds-verifier:
92
+ mode: image
93
+ timeout: 180
94
+ codeLength: 4
95
+ ```
96
+
97
+ ### 配置白名单
98
+
99
+ ```yaml
100
+ plugins:
101
+ yyds-verifier:
102
+ mode: random
103
+ whitelist:
104
+ - '123456789'
105
+ - '987654321'
106
+ ```
107
+
108
+ ## 验证流程
109
+
110
+ ```
111
+ 用户入群
112
+
113
+ 检查白名单 → 在白名单中 → 跳过验证
114
+ ↓ 不在白名单
115
+ 生成验证挑战
116
+
117
+ 发送验证提示
118
+
119
+ 等待用户输入 → 超时 → 还有机会? → 是 → 下一轮
120
+ ↓ ↓ 否
121
+ 验证答案 踢出用户
122
+
123
+ 正确 → 验证成功
124
+ ↓ 错误
125
+ 还有机会? → 是 → 提示错误,继续等待
126
+ ↓ 否
127
+ 踢出用户
128
+ ```
129
+
130
+ ## 注意事项
131
+
132
+ 1. 机器人需要有踢人权限才能正常工作
133
+ 2. 建议将验证超时时间设置在 60-300 秒之间
134
+ 3. 图像验证码模式需要 `koishi-plugin-skia-canvas` 支持
135
+ 4. 白名单中的用户 ID 需要是字符串格式
136
+
137
+ ## 许可证
138
+
139
+ MIT