koishi-plugin-onebot-verifier 1.0.6 → 1.0.8
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 +1 -0
- package/lib/index.js +49 -19
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -45,7 +45,8 @@ var usage = `
|
|
|
45
45
|
var Config = import_koishi.Schema.intersect([
|
|
46
46
|
import_koishi.Schema.object({
|
|
47
47
|
notifyTarget: import_koishi.Schema.string().description("通知目标(guild/private:number)").required(),
|
|
48
|
-
debugMode: import_koishi.Schema.boolean().description("输出调试日志").default(false)
|
|
48
|
+
debugMode: import_koishi.Schema.boolean().description("输出调试日志").default(false),
|
|
49
|
+
kickBan: import_koishi.Schema.boolean().description("被踢自动处理").default(false)
|
|
49
50
|
}).description("基础配置"),
|
|
50
51
|
import_koishi.Schema.object({
|
|
51
52
|
timeout: import_koishi.Schema.number().description("请求超时时长").default(360).min(0),
|
|
@@ -78,10 +79,12 @@ var Config = import_koishi.Schema.intersect([
|
|
|
78
79
|
function apply(ctx, config = {}) {
|
|
79
80
|
const logger = new import_koishi.Logger("onebot-verifier");
|
|
80
81
|
const activeTasks = /* @__PURE__ */ new Map();
|
|
82
|
+
const inviterMap = /* @__PURE__ */ new Map();
|
|
81
83
|
const executeAction = /* @__PURE__ */ __name(async (session, kind, pass, reason = "", remark = "") => {
|
|
82
84
|
try {
|
|
83
85
|
const eventData = session.event?._data || {};
|
|
84
86
|
if (config.debugMode) logger.info(`[执行操作] 类型:${kind} 结果:${pass ? "同意" : "拒绝"} 原因:${reason || "无"}`);
|
|
87
|
+
if (pass && kind === "guild" && session.guildId && session.userId) inviterMap.set(session.guildId, session.userId);
|
|
85
88
|
if (!pass && kind === "guild" && session.guildId && (session.event?.type === "guild-added" || eventData.notice_type === "group_increase")) {
|
|
86
89
|
if (reason) {
|
|
87
90
|
try {
|
|
@@ -111,14 +114,26 @@ function apply(ctx, config = {}) {
|
|
|
111
114
|
const eventData = session.event?._data || {};
|
|
112
115
|
const userInfo = session.userId ? await session.bot.getUser?.(session.userId)?.catch(() => null) : null;
|
|
113
116
|
const groupInfo = kind !== "friend" && session.guildId ? await session.bot.getGuild?.(session.guildId)?.catch(() => null) : null;
|
|
114
|
-
const
|
|
117
|
+
const adminId = eventData.operator_id || session.event.operator?.id;
|
|
118
|
+
const adminInfo = adminId && session.userId && adminId !== session.userId ? await session.bot.getUser?.(adminId)?.catch(() => null) : null;
|
|
115
119
|
const infoLines = [];
|
|
116
120
|
if (userInfo?.avatar) infoLines.push(`<image url="${userInfo.avatar}"/>`);
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (kind
|
|
121
|
-
if (
|
|
121
|
+
let typeName = "";
|
|
122
|
+
if (kind === "friend") typeName = "好友申请";
|
|
123
|
+
else if (kind === "member") typeName = "加群请求";
|
|
124
|
+
else if (kind === "guild") typeName = "群组邀请";
|
|
125
|
+
else if (kind === "removed") typeName = eventData.sub_type === "kick_me" ? "机器人被踢" : "机器人退群";
|
|
126
|
+
let statusText = "";
|
|
127
|
+
if (kind === "removed") {
|
|
128
|
+
if (eventData.sub_type === "kick_me" && config.kickBan) statusText = " [自动清理]";
|
|
129
|
+
} else {
|
|
130
|
+
statusText = status === "auto_pass" ? " [自动通过]" : status === "auto_reject" ? " [自动拒绝]" : " [等待处理]";
|
|
131
|
+
}
|
|
132
|
+
infoLines.push(`类型:${typeName}${statusText}`);
|
|
133
|
+
if (kind !== "guild" && kind !== "removed" || session.userId !== session.selfId) {
|
|
134
|
+
infoLines.push(`用户:${userInfo?.name || session.userId}${session.userId ? `(${session.userId})` : ""}`);
|
|
135
|
+
}
|
|
136
|
+
if (adminInfo) infoLines.push(`管理:${adminInfo.name ? `${adminInfo.name}(${adminId})` : adminId}`);
|
|
122
137
|
if (groupInfo) infoLines.push(`群组:${groupInfo.name ? `${groupInfo.name}(${session.guildId})` : session.guildId}`);
|
|
123
138
|
if (eventData.comment) infoLines.push(`验证信息:${eventData.comment}`);
|
|
124
139
|
const content = infoLines.join("\n");
|
|
@@ -207,21 +222,23 @@ function apply(ctx, config = {}) {
|
|
|
207
222
|
try {
|
|
208
223
|
if (config.debugMode) logger.info(`[收到请求] 类型: ${kind} 数据:${JSON.stringify(session.event?._data || {})}`);
|
|
209
224
|
if (kind === "member") {
|
|
210
|
-
const
|
|
211
|
-
if (
|
|
225
|
+
const rules = config.verifyRules?.filter((r) => String(r.guildId) === String(session.guildId)) || [];
|
|
226
|
+
if (rules.length > 0) {
|
|
212
227
|
const rawText = session.event?._data?.comment || "";
|
|
213
228
|
const cleanLines = rawText.split(/[\r\n]+/).map((s) => s.trim()).filter((s) => /^(回答|答案)[::]/i.test(s)).map((s) => s.replace(/^(回答|答案)[::]\s*/i, ""));
|
|
214
229
|
const verifyText = cleanLines.length > 0 ? cleanLines.join("\n") : rawText;
|
|
215
|
-
const
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
230
|
+
for (const rule of rules) {
|
|
231
|
+
const stats = (rule.minLevel ?? -1) >= 0 && session.onebot && session.userId ? await session.onebot.getStrangerInfo(session.userId, true).catch(() => ({})) : null;
|
|
232
|
+
const keywordMatch = !rule.keyword || new RegExp(rule.keyword, "i").test(verifyText);
|
|
233
|
+
const levelMatch = !stats || (stats.qqLevel ?? 0) >= rule.minLevel;
|
|
234
|
+
const isMatch = keywordMatch && levelMatch;
|
|
235
|
+
if (config.debugMode) logger.info(`[规则判定] ${rule.guildId}: 关键词="${rule.keyword}"=${keywordMatch}; 等级="${rule.minLevel}"=${levelMatch}`);
|
|
236
|
+
if (isMatch && rule.action) {
|
|
237
|
+
const isApprove = rule.action === "accept";
|
|
238
|
+
await executeAction(session, kind, isApprove, isApprove ? "" : "命中拒绝规则,自动拒绝");
|
|
239
|
+
await sendNotice(session, kind, isApprove ? "auto_pass" : "auto_reject");
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
225
242
|
}
|
|
226
243
|
}
|
|
227
244
|
return await setupManual(session, kind);
|
|
@@ -250,6 +267,19 @@ function apply(ctx, config = {}) {
|
|
|
250
267
|
ctx.on("guild-request", hookEvent("guild"));
|
|
251
268
|
ctx.on("guild-member-request", hookEvent("member"));
|
|
252
269
|
ctx.on("guild-added", hookEvent("guild"));
|
|
270
|
+
ctx.on("guild-removed", async (session) => {
|
|
271
|
+
const subType = session.event?._data?.sub_type;
|
|
272
|
+
if (subType === "kick_me") {
|
|
273
|
+
const inviterId = inviterMap.get(session.guildId);
|
|
274
|
+
if (inviterId) {
|
|
275
|
+
await session.onebot?.deleteFriend(inviterId);
|
|
276
|
+
inviterMap.delete(session.guildId);
|
|
277
|
+
}
|
|
278
|
+
await session.execute(`analyse.clear -g ${session.guildId}`).catch(() => {
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
await sendNotice(session, "removed");
|
|
282
|
+
});
|
|
253
283
|
ctx.middleware(async (session, next) => {
|
|
254
284
|
if (typeof session.content !== "string" || !session.quote?.id) return next();
|
|
255
285
|
const activeTask = activeTasks.get(session.quote.id);
|