koishi-plugin-group-control 0.1.3 → 0.2.1
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 +9 -0
- package/lib/index.js +160 -0
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -46,6 +46,14 @@ export interface GroupConfig {
|
|
|
46
46
|
quitCommandEnabled: boolean;
|
|
47
47
|
quitCommandAuthority: number;
|
|
48
48
|
}
|
|
49
|
+
export interface GroupInviteConfig {
|
|
50
|
+
enabled: boolean;
|
|
51
|
+
adminQQs: string[];
|
|
52
|
+
inviteWaitMessage: string;
|
|
53
|
+
inviteRequestMessage: string;
|
|
54
|
+
autoApprove: boolean;
|
|
55
|
+
showDetailedLog: boolean;
|
|
56
|
+
}
|
|
49
57
|
export interface FrequencyConfig {
|
|
50
58
|
enabled: boolean;
|
|
51
59
|
limit: number;
|
|
@@ -60,6 +68,7 @@ export interface FrequencyConfig {
|
|
|
60
68
|
export interface Config {
|
|
61
69
|
basic: GroupConfig;
|
|
62
70
|
frequency: FrequencyConfig;
|
|
71
|
+
invite: GroupInviteConfig;
|
|
63
72
|
}
|
|
64
73
|
export declare const Config: Schema<Config>;
|
|
65
74
|
export declare function apply(ctx: Context, config: Config): void;
|
package/lib/index.js
CHANGED
|
@@ -50,6 +50,16 @@ var Config = import_koishi.Schema.intersect([
|
|
|
50
50
|
blockedMsg: import_koishi.Schema.string().default("指令暂时被禁用,还有 {time} 秒解禁。").description("屏蔽期间接收到指令时的提示消息,支持变量{time}"),
|
|
51
51
|
whitelist: import_koishi.Schema.array(String).default([]).description("频率控制白名单群号列表,白名单内的群聊不受频率限制")
|
|
52
52
|
}).description("指令频率控制")
|
|
53
|
+
}),
|
|
54
|
+
import_koishi.Schema.object({
|
|
55
|
+
invite: import_koishi.Schema.object({
|
|
56
|
+
enabled: import_koishi.Schema.boolean().default(false).description("启用群聊邀请审核功能"),
|
|
57
|
+
adminQQs: import_koishi.Schema.array(String).default([]).description("管理员QQ号列表,用于接收邀请审核请求"),
|
|
58
|
+
inviteWaitMessage: import_koishi.Schema.string().default("已收到您的群聊邀请,正在等待管理员审核,请耐心等待。").description("发送给邀请者的等待审核提示消息"),
|
|
59
|
+
inviteRequestMessage: import_koishi.Schema.string().default('收到新的群聊邀请请求:\n群名称:{groupName}\n群号:{groupId}\n邀请者:{userName} (QQ: {userId})\n\n请引用此消息回复"同意"或"拒绝"进行审核。').description("发送给管理员的邀请请求消息模板,支持变量{groupName}, {groupId}, {userName}, {userId}"),
|
|
60
|
+
autoApprove: import_koishi.Schema.boolean().default(false).description("是否自动同意邀请(仅在没有指定管理员时)"),
|
|
61
|
+
showDetailedLog: import_koishi.Schema.boolean().default(false).description("是否显示详细日志")
|
|
62
|
+
}).description("群聊邀请审核")
|
|
53
63
|
})
|
|
54
64
|
]);
|
|
55
65
|
function isBlacklistEnabled(config) {
|
|
@@ -127,6 +137,156 @@ function apply(ctx, config) {
|
|
|
127
137
|
blockExpiryTime: "integer",
|
|
128
138
|
firstWarningTime: "integer"
|
|
129
139
|
}, { primary: ["platform", "guildId"] });
|
|
140
|
+
if (config.invite.enabled) {
|
|
141
|
+
const pendingInvites = /* @__PURE__ */ new Map();
|
|
142
|
+
ctx.on("guild-request", async (session) => {
|
|
143
|
+
const { guildId, userId, platform } = session;
|
|
144
|
+
const flag = session.messageId || session.flag || session.event?.flag;
|
|
145
|
+
let userName = userId;
|
|
146
|
+
try {
|
|
147
|
+
const userInfo = await session.bot.getUser(userId);
|
|
148
|
+
userName = userInfo?.nickname || userInfo?.name || userId;
|
|
149
|
+
} catch (error) {
|
|
150
|
+
console.error("获取用户信息失败:", error);
|
|
151
|
+
}
|
|
152
|
+
let groupName = guildId;
|
|
153
|
+
try {
|
|
154
|
+
const guildInfo = await session.bot.getGuild(guildId);
|
|
155
|
+
groupName = guildInfo?.name || guildInfo?.group_name || guildId;
|
|
156
|
+
} catch (error) {
|
|
157
|
+
console.error("获取群信息失败:", error);
|
|
158
|
+
}
|
|
159
|
+
try {
|
|
160
|
+
const waitMessage = config.invite.inviteWaitMessage.replace("{groupName}", groupName).replace("{groupId}", guildId).replace("{userName}", userName).replace("{userId}", userId);
|
|
161
|
+
await session.bot.sendMessage(userId, waitMessage, platform);
|
|
162
|
+
} catch (error) {
|
|
163
|
+
console.error("发送等待审核提示失败:", error);
|
|
164
|
+
}
|
|
165
|
+
if (!config.invite.adminQQs || config.invite.adminQQs.length === 0) {
|
|
166
|
+
if (config.invite.autoApprove) {
|
|
167
|
+
try {
|
|
168
|
+
await session.bot.internal.setGroupAddRequest({
|
|
169
|
+
flag,
|
|
170
|
+
sub_type: "invite",
|
|
171
|
+
approve: true,
|
|
172
|
+
reason: ""
|
|
173
|
+
});
|
|
174
|
+
if (config.invite.showDetailedLog) {
|
|
175
|
+
console.log(`自动同意群聊邀请: 群号 ${guildId}, 邀请者 ${userId}`);
|
|
176
|
+
}
|
|
177
|
+
} catch (error) {
|
|
178
|
+
console.error("自动同意群聊邀请失败:", error);
|
|
179
|
+
}
|
|
180
|
+
} else {
|
|
181
|
+
if (config.invite.showDetailedLog) {
|
|
182
|
+
console.log(`未配置管理员且未启用自动同意,忽略群聊邀请: 群号 ${guildId}, 邀请者 ${userId}`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const inviteId = `${guildId}_${userId}_${Date.now()}`;
|
|
188
|
+
pendingInvites.set(inviteId, {
|
|
189
|
+
groupId: guildId,
|
|
190
|
+
userId,
|
|
191
|
+
userName,
|
|
192
|
+
time: Date.now(),
|
|
193
|
+
flag
|
|
194
|
+
// 保存flag用于后续处理请求
|
|
195
|
+
});
|
|
196
|
+
const requestMessage = config.invite.inviteRequestMessage.replace("{groupName}", groupName).replace("{groupId}", guildId).replace("{userName}", userName).replace("{userId}", userId);
|
|
197
|
+
for (const adminQQ of config.invite.adminQQs) {
|
|
198
|
+
try {
|
|
199
|
+
const messageId = await session.bot.sendMessage(adminQQ, requestMessage, platform);
|
|
200
|
+
if (config.invite.showDetailedLog) {
|
|
201
|
+
console.log(`发送群聊邀请请求给管理员 ${adminQQ}: 群号 ${guildId}, 邀请者 ${userId}`);
|
|
202
|
+
}
|
|
203
|
+
} catch (error) {
|
|
204
|
+
console.error(`发送邀请请求给管理员 ${adminQQ} 失败:`, error);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
ctx.on("message", async (session) => {
|
|
209
|
+
const { userId, content, platform } = session;
|
|
210
|
+
if (!config.invite.adminQQs.includes(userId)) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const hasQuote = session.elements.some((element) => element.type === "quote");
|
|
214
|
+
if (!hasQuote) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
const trimmedContent = content.trim();
|
|
218
|
+
if (trimmedContent !== "同意" && trimmedContent !== "拒绝" && trimmedContent !== "accept" && trimmedContent !== "reject") {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
const quoteElement = session.elements.find((element) => element.type === "quote");
|
|
222
|
+
if (!quoteElement) {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
const quoteMessageContent = quoteElement.attrs.content || quoteElement.attrs.text || "";
|
|
226
|
+
const groupIdMatch = quoteMessageContent.match(/群号:(\d+)/i);
|
|
227
|
+
const userIdMatch = quoteMessageContent.match(/QQ:\s*(\d+)/i);
|
|
228
|
+
if (groupIdMatch && userIdMatch) {
|
|
229
|
+
const extractedGroupId = groupIdMatch[1];
|
|
230
|
+
const extractedUserId = userIdMatch[1];
|
|
231
|
+
let targetInviteId = null;
|
|
232
|
+
for (const [inviteId, inviteData] of pendingInvites) {
|
|
233
|
+
if (inviteData.groupId === extractedGroupId && inviteData.userId === extractedUserId) {
|
|
234
|
+
targetInviteId = inviteId;
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (targetInviteId) {
|
|
239
|
+
const inviteData = pendingInvites.get(targetInviteId);
|
|
240
|
+
if (inviteData) {
|
|
241
|
+
if (trimmedContent === "同意" || trimmedContent === "accept") {
|
|
242
|
+
try {
|
|
243
|
+
await session.bot.internal.setGroupAddRequest({
|
|
244
|
+
flag: inviteData.flag,
|
|
245
|
+
sub_type: "invite",
|
|
246
|
+
approve: true,
|
|
247
|
+
reason: ""
|
|
248
|
+
});
|
|
249
|
+
if (config.invite.showDetailedLog) {
|
|
250
|
+
console.log(`管理员 ${userId} 同意群聊邀请: 群号 ${inviteData.groupId}, 邀请者 ${inviteData.userId}`);
|
|
251
|
+
}
|
|
252
|
+
await session.bot.sendMessage(userId, `已同意加入群 ${inviteData.groupId}`, platform);
|
|
253
|
+
try {
|
|
254
|
+
await session.bot.sendMessage(inviteData.userId, `您的群聊邀请已通过管理员审核,机器人已加入群聊。`, platform);
|
|
255
|
+
} catch (error) {
|
|
256
|
+
console.error("通知邀请者审核结果失败:", error);
|
|
257
|
+
}
|
|
258
|
+
} catch (error) {
|
|
259
|
+
console.error("处理同意邀请失败:", error);
|
|
260
|
+
await session.bot.sendMessage(userId, `处理同意邀请失败: ${error.message}`, platform);
|
|
261
|
+
}
|
|
262
|
+
} else if (trimmedContent === "拒绝" || trimmedContent === "reject") {
|
|
263
|
+
try {
|
|
264
|
+
await session.bot.internal.setGroupAddRequest({
|
|
265
|
+
flag: inviteData.flag,
|
|
266
|
+
sub_type: "invite",
|
|
267
|
+
approve: false,
|
|
268
|
+
reason: "已拒绝"
|
|
269
|
+
});
|
|
270
|
+
if (config.invite.showDetailedLog) {
|
|
271
|
+
console.log(`管理员 ${userId} 拒绝群聊邀请: 群号 ${inviteData.groupId}, 邀请者 ${inviteData.userId}`);
|
|
272
|
+
}
|
|
273
|
+
await session.bot.sendMessage(userId, `已拒绝加入群 ${inviteData.groupId}`, platform);
|
|
274
|
+
try {
|
|
275
|
+
await session.bot.sendMessage(inviteData.userId, `您的群聊邀请未通过管理员审核,机器人将不会加入该群聊。`, platform);
|
|
276
|
+
} catch (error) {
|
|
277
|
+
console.error("通知邀请者审核结果失败:", error);
|
|
278
|
+
}
|
|
279
|
+
} catch (error) {
|
|
280
|
+
console.error("处理拒绝邀请失败:", error);
|
|
281
|
+
await session.bot.sendMessage(userId, `处理拒绝邀请失败: ${error.message}`, platform);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
pendingInvites.delete(targetInviteId);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
}
|
|
130
290
|
ctx.on("guild-added", async (session) => {
|
|
131
291
|
const { guildId, platform } = session;
|
|
132
292
|
if (!config.basic.enableBlacklist) {
|
package/package.json
CHANGED