-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecall.js
449 lines (403 loc) · 16 KB
/
recall.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/**
更新时间:2025/02/03
制作人:xinixinxin, rainbowwarmth
注意:
需要机器人有管理员权限
*/
import plugin from '../../lib/plugins/plugin.js'
import fs from 'fs'
import path from 'path'
import { parse, stringify } from 'yaml'
export class recall extends plugin {
constructor() {
super({
name: '自动撤回',
dsc: '自动撤回含有特定违禁词的消息',
event: 'message',
priority: 100,
rule: [
{
reg: "^#开启群撤回$",
fnc: 'enableRecall',
permission: 'admin'
},
{
reg: "^#关闭群撤回$",
fnc: 'disableRecall',
permission: 'admin'
},
{
reg: "^#违禁词添加 (.+)$",
fnc: 'addBannedWord',
permission: 'admin'
},
{
reg: "^#违禁词删除 (.+)$",
fnc: 'deleteBannedWord',
permission: 'admin'
},
{
reg: "^#查看违禁词$",
fnc: 'viewBannedWords',
permission: 'admin'
},
{
reg: "^#设置违禁词处理方式 (.+)$",
fnc: 'setBannedWordAction',
permission: 'admin'
},
{
reg: "^#设置禁言时间 (.+)$",
fnc: 'setMuteDuration',
permission: 'admin'
},
{
reg: "^#设置违规推送 (.+)$",
fnc: 'setViolationPush',
permission: 'admin'
},
{
reg: ".*",
fnc: 'recallMessage',
log: false
}
]
})
}
/**
* 处理群消息撤回开启
*/
async enableRecall(e) {
const botId = e.self_id
const groupId = e.group_id
const botConfigDir = path.join('./data/recallGroups', botId.toString())
if (!fs.existsSync(botConfigDir)) {
fs.mkdirSync(botConfigDir, { recursive: true })
}
const filePath = path.join(botConfigDir, `${groupId}.yaml`)
if (!fs.existsSync(filePath)) {
fs.writeFileSync(filePath, '', 'utf8')
} else {
if (!fs.lstatSync(filePath).isFile()) {
fs.rmdirSync(filePath)
fs.writeFileSync(filePath, '', 'utf8')
}
}
if (fs.existsSync(filePath) && fs.lstatSync(filePath).isFile()) {
try {
let config = parse(fs.readFileSync(filePath, 'utf8'))
if (config === null) {
config = {}
}
config.group_id = groupId
config.recall_enabled = true
config.keywords = config.keywords || []
config.action = config.action || '3'
config.mute_duration = config.mute_duration || 60
config.push_set = false
fs.writeFileSync(filePath, stringify(config), 'utf8')
e.reply('本群已开启自动撤回功能。')
} catch (error) {
logger.error(`读取配置文件 ${filePath} 时出错: ${error.message}`)
return true
}
} else {
const defaultConfig = {
group_id: groupId,
recall_enabled: true,
keywords: [],
action: '3',
mute_duration: 60,
push_set: false
}
fs.writeFileSync(filePath, stringify(defaultConfig), 'utf8')
e.reply('已为本群开启自动撤回功能。')
}
logger.mark(`群 ${groupId} 已开启自动撤回功能。`)
return false
}
/**
* 处理群消息撤回关闭
*/
async disableRecall(e) {
const botId = e.self_id
const groupId = e.group_id
const botConfigDir = path.join('./data/recallGroups', botId.toString())
if (!fs.existsSync(botConfigDir)) {
fs.mkdirSync(botConfigDir, { recursive: true })
}
const filePath = path.join(botConfigDir, `${groupId}.yaml`)
if (fs.existsSync(filePath)) {
let config = parse(fs.readFileSync(filePath, 'utf8'))
config.recall_enabled = false;
fs.writeFileSync(filePath, stringify(config))
e.reply('本群已关闭自动撤回功能。')
} else {
e.reply('本群尚未开启自动撤回功能。')
}
logger.mark(`群 ${groupId} 已关闭自动撤回功能。`)
return false
}
/**
* 添加群违禁词
*/
async addBannedWord(e) {
const botId = e.self_id
const groupId = e.group_id
const botConfigDir = path.join('./data/recallGroups', botId.toString())
if (!groupId) {
return true
}
const filePath = path.join(botConfigDir, `${groupId}.yaml`)
if (fs.existsSync(filePath)) {
let config = parse(fs.readFileSync(filePath, 'utf8'))
const newBannedWord = e.msg.match(/^#违禁词添加 (.+)$/)[1].trim()
if (typeof newBannedWord !== 'string' || newBannedWord.length === 0) {
e.reply('违禁词无效。');
logger.mark(`群 ${groupId} 添加违禁词失败,违禁词无效:${newBannedWord}`)
return false;
}
if (!config.keywords.includes(newBannedWord)) {
config.keywords.push(newBannedWord);
fs.writeFileSync(filePath, stringify(config))
e.reply(`违禁词 "${newBannedWord}" 已添加。`)
logger.mark(`群 ${groupId} 添加违禁词:${newBannedWord}`)
} else {
e.reply(`违禁词 "${newBannedWord}" 已存在。`)
logger.mark(`群 ${groupId} 违禁词已存在:${newBannedWord}`)
}
} else {
e.reply('本群尚未开启自动撤回功能,请先使用 #开启群撤回 命令。')
logger.mark(`群 ${groupId} 添加违禁词失败,尚未开启自动撤回功能。`)
}
return false
}
/**
* 删除群违禁词
*/
async deleteBannedWord(e) {
const botId = e.self_id
const groupId = e.group_id
const botConfigDir = path.join('./data/recallGroups', botId.toString())
if (!groupId) {
return true
}
const filePath = path.join(botConfigDir, `${groupId}.yaml`)
if (fs.existsSync(filePath)) {
let config = parse(fs.readFileSync(filePath, 'utf8'))
const bannedWordToDelete = e.msg.match(/^#违禁词删除 (.+)$/)[1].trim()
if (typeof bannedWordToDelete !== 'string' || bannedWordToDelete.length === 0) {
e.reply('违禁词无效。');
logger.mark(`群 ${groupId} 删除违禁词失败,违禁词无效:${bannedWordToDelete}`)
return false;
}
if (config.keywords.includes(bannedWordToDelete)) {
config.keywords = config.keywords.filter(kw => kw !== bannedWordToDelete)
fs.writeFileSync(filePath, stringify(config), 'utf8')
e.reply(`违禁词 "${bannedWordToDelete}" 已删除。`)
logger.mark(`群 ${groupId} 删除违禁词:${bannedWordToDelete}`)
} else {
e.reply(`违禁词 "${bannedWordToDelete}" 不存在。`)
logger.mark(`群 ${groupId} 删除违禁词失败,违禁词不存在:${bannedWordToDelete}`)
}
} else {
e.reply('本群尚未开启自动撤回功能,请先使用 #开启群撤回 命令。')
logger.mark(`群 ${groupId} 删除违禁词失败,尚未开启自动撤回功能。`)
}
return false
}
/**
* 查看群违禁词
*/
async viewBannedWords(e) {
const botId = e.self_id
const groupId = e.group_id
const botConfigDir = path.join('./data/recallGroups', botId.toString())
if (!groupId) {
return true
}
const filePath = path.join(botConfigDir, `${groupId}.yaml`)
if (fs.existsSync(filePath)) {
let config = parse(fs.readFileSync(filePath, 'utf8'))
const keywords = config.keywords
if (keywords.length > 0) {
e.reply(await Bot.makeForwardArray([`本群当前的违禁词列表:\n- ${keywords.join('\n- ')}`]))
} else {
e.reply('本群当前没有设置违禁词。')
}
logger.mark(`群 ${groupId} 查看违禁词列表: ${keywords}`)
} else {
e.reply('本群尚未开启自动撤回功能,请先使用 #开启群撤回 命令。')
logger.mark(`群 ${groupId} 查看违禁词列表失败,尚未开启自动撤回功能。`)
}
return false
}
/**
* 设置违禁词处理方式
*/
async setBannedWordAction(e) {
const botId = e.self_id
const groupId = e.group_id
const botConfigDir = path.join('./data/recallGroups', botId.toString())
const filePath = path.join(botConfigDir, `${groupId}.yaml`)
const action = e.msg.match(/^#设置违禁词处理方式 (.+)$/)[1].trim()
const validActions = ['1', '2', '3', '4', '5']
if (!validActions.includes(action)) {
e.reply(`无效的处理方式,请选择以下之一:${validActions.join(', ')}`)
return false
}
if (fs.existsSync(filePath)) {
let config = parse(fs.readFileSync(filePath, 'utf8'))
config.action = action;
fs.writeFileSync(filePath, stringify(config), 'utf8')
e.reply(`违禁词处理方式已设为:${action}`)
} else {
e.reply('本群尚未开启自动撤回功能,请先使用 #开启群撤回 命令。')
}
return false
}
/**
* 设置禁言时间
*/
async setMuteDuration(e) {
const botId = e.self_id
const groupId = e.group_id
const botConfigDir = path.join('./data/recallGroups', botId.toString())
const filePath = path.join(botConfigDir, `${groupId}.yaml`)
const muteDuration = parseInt(e.msg.match(/^#设置禁言时间 (.+)$/)[1].trim(), 10)
if (isNaN(muteDuration) || muteDuration <= 0) {
e.reply('无效的禁言时间,请输入一个正整数。')
return false
}
if (fs.existsSync(filePath)) {
let config = parse(fs.readFileSync(filePath, 'utf8'))
config.mute_duration = muteDuration;
fs.writeFileSync(filePath, stringify(config), 'utf8')
e.reply(`禁言时间已设置为:${muteDuration} 秒`)
} else {
e.reply('本群尚未开启自动撤回功能,请先使用 #开启群撤回 命令。')
}
return false
}
/**
* 设置违规推送功能
*/
async setViolationPush(e) {
const botId = e.self_id
const groupId = e.group_id
const botConfigDir = path.join('./data/recallGroups', botId.toString())
const filePath = path.join(botConfigDir, `${groupId}.yaml`)
const match = e.msg.match(/^#设置违规推送 (.+)$/)
if (!match) {
e.reply('命令格式错误,请使用:#设置违规推送 开启 或 #设置违规推送 关闭')
return false
}
const pushFlag = match[1].trim()
if (pushFlag !== '开启' && pushFlag !== '关闭') {
e.reply('无效的参数,请输入 开启 或 关闭')
return false
}
if (fs.existsSync(filePath)) {
let config
try {
config = parse(fs.readFileSync(filePath, 'utf8'))
} catch (error) {
e.reply(`读取配置文件出错: ${error.message}`)
return false;
}
config.push_set = (pushFlag === '开启')
try {
fs.writeFileSync(filePath, stringify(config), 'utf8')
e.reply(`违规推送设置已更新为:${pushFlag}`)
} catch (error) {
e.reply(`写入配置文件时出错: ${error.message}`)
}
} else {
e.reply('本群尚未开启自动撤回功能,请先使用 #开启群撤回 命令。')
}
return false
}
/**
* 处理消息撤回逻辑
*/
async recallMessage(e) {
const botId = e.self_id
const groupId = e.group_id
const senderCard = e.sender.card || e.sender.nickname
const botConfigDir = path.join('./data/recallGroups', botId.toString())
const filePath = path.join(botConfigDir, `${groupId}.yaml`)
if (e.image || e.face) {
logger.mark(`群 ${groupId} 的消息包含图片或表情,直接放行。`)
return true
}
const facePattern = /\[<face,id=\d+>\]/
if (e.msg && facePattern.test(e.msg)) {
logger.mark(`群 ${groupId} 的消息 "${e.msg}" 被过滤,包含特定表情格式。`)
return false
}
if (fs.existsSync(filePath)) {
let config
try {
config = parse(fs.readFileSync(filePath, 'utf8'))
} catch (error) {
logger.error(`读取配置文件 ${filePath} 时出错: ${error.message}`)
return true;
}
const recallEnabled = config?.recall_enabled
const keywords = config?.keywords || []
const action = config?.action || '3'
const mute_duration = config?.mute_duration || 60
const push_set = config?.push_set
if (!recallEnabled || !Array.isArray(keywords)) return true
const folderSelfId = path.basename(path.dirname(filePath))
if (folderSelfId !== botId.toString()) {
logger.mark(`当前机器人ID ${botId} 与配置文件路径中的ID ${folderSelfId} 不一致,跳过撤回任务。`)
return true
}
for (let keyword of keywords) {
if (typeof keyword === 'string' && e.msg && e.msg.includes(keyword)) {
if (e.group && typeof e.group.recallMsg === 'function') {
switch (action) {
case '1':
await e.group.kickMember(e.user_id)
if (push_set) {
e.reply(`${senderCard} 已被踢出群聊,触发违禁词:${keyword}`)
}
break
case '2':
await e.group.muteMember(e.user_id, mute_duration);
if (push_set) {
e.reply(`${senderCard} 已被禁言,触发违禁词:${keyword}`)
}
break
case '3':
await e.group.recallMsg(e.message_id)
if (push_set) {
e.reply(`消息已撤回,触发违禁词:${keyword}`)
}
break;
case '4':
await e.group.kickMember(e.user_id)
await e.group.recallMsg(e.message_id)
if (push_set) {
e.reply(`${senderCard} 已被踢出并消息已撤回,触发违禁词:${keyword}`)
}
break
case '5':
await e.group.muteMember(e.user_id, mute_duration)
await e.group.recallMsg(e.message_id)
if (push_set) {
e.reply(`${senderCard} 已被禁言并消息已撤回,触发违禁词:${keyword}`)
}
break
}
return false
} else {
logger.mark(`无法撤回消息,群组对象未定义或 recallMsg 方法不可用。`)
}
}
}
}
return false
}
}