/
moderation.js
485 lines (439 loc) · 19.4 KB
/
moderation.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
'use strict'
// 3rdparty libraries
const _ = require('lodash')
const XRegExp = require('xregexp')
// bot libraries
import constants from '../constants'
import { permission } from '../permissions';
import { command, default_permission, parser } from '../decorators';
const Message = require('../message')
import System from './_interface'
const commons = require('../commons');
var log = global.log
class Moderation extends System {
constructor () {
const settings = {
lists: {
whitelist: [],
blacklist: [],
moderateSubscribers: true,
timeout: 120
},
links: {
enabled: true,
moderateSubscribers: true,
includeSpaces: false,
includeClips: true,
timeout: 120
},
symbols: {
enabled: true,
moderateSubscribers: true,
triggerLength: 15,
maxSymbolsConsecutively: 10,
maxSymbolsPercent: 50,
timeout: 120
},
longMessage: {
enabled: true,
moderateSubscribers: true,
triggerLength: 300,
timeout: 120
},
caps: {
enabled: true,
moderateSubscribers: true,
triggerLength: 15,
maxCapsPercent: 50,
timeout: 120
},
spam: {
enabled: true,
moderateSubscribers: true,
triggerLength: 15,
maxLength: 50,
timeout: 300
},
color: {
enabled: true,
moderateSubscribers: true,
timeout: 120
},
emotes: {
enabled: true,
moderateSubscribers: true,
maxCount: 15,
timeout: 120
},
warnings: {
warningCount: 3,
announce: true,
shouldClearChat: true
},
}
super({ settings })
this.addMenu({ category: 'settings', name: 'systems', id: 'systems' })
}
sockets () {
this.socket.on('connection', (socket) => {
socket.on('lists.get', async (cb) => {
cb(null, {
blacklist: this.settings.lists.blacklist,
whitelist: this.settings.lists.whitelist
})
})
socket.on('lists.set', async (data) => {
this.settings.lists.blacklist = data.blacklist.filter(entry => entry.trim() !== '')
this.settings.lists.whitelist = data.whitelist.filter(entry => entry.trim() !== '')
})
})
}
async timeoutUser (sender, text, warning, msg, time, type) {
let [warningsAllowed, warningsTimeout, announceTimeouts, warnings, silent] = await Promise.all([
this.settings.warnings.warningCount,
this.settings.warnings.shouldClearChat,
this.settings.warnings.announce,
global.db.engine.find(global.systems.moderation.collection.warnings, { username: sender.username }),
this.isSilent(type)
])
text = text.trim()
// cleanup warnings
let wasCleaned = false
for (let warning of _.filter(warnings, (o) => _.now() - o.timestamp > 1000 * 60 * 60)) {
await global.db.engine.remove(global.systems.moderation.collection.warnings, { _id: warning._id.toString() })
wasCleaned = true
}
if (wasCleaned) warnings = await global.db.engine.find(global.systems.moderation.collection.warnings, { username: sender.username })
if (warningsAllowed === 0) {
msg = await new Message(msg.replace(/\$count/g, -1)).parse()
log.timeout(`${sender.username} [${type}] ${time}s timeout | ${text}`)
commons.timeout(sender.username, msg, time)
return
}
const isWarningCountAboveThreshold = warnings.length >= parseInt(warningsAllowed, 10)
if (isWarningCountAboveThreshold) {
msg = await new Message(warning.replace(/\$count/g, parseInt(warningsAllowed, 10) - warnings.length)).parse()
log.timeout(`${sender.username} [${type}] ${time}s timeout | ${text}`)
commons.timeout(sender.username, msg, time)
await global.db.engine.remove(global.systems.moderation.collection.warnings, { username: sender.username })
} else {
await global.db.engine.insert(global.systems.moderation.collection.warnings, { username: sender.username, timestamp: _.now() })
const warningsLeft = parseInt(warningsAllowed, 10) - warnings.length
warning = await new Message(warning.replace(/\$count/g, warningsLeft < 0 ? 0 : warningsLeft)).parse()
if (warningsTimeout) {
log.timeout(`${sender.username} [${type}] 1s timeout, warnings left ${warningsLeft < 0 ? 0 : warningsLeft} | ${text}`)
commons.timeout(sender.username, warning, 1)
}
if (announceTimeouts && !silent) {
global.tmi.delete('bot', sender.id)
commons.sendMessage('$sender, ' + warning, sender)
}
}
}
async whitelist (text) {
let ytRegex, clipsRegex, spotifyRegex
// check if spotify -or- alias of spotify contain open.spotify.com link
if (await global.integrations.spotify.isEnabled()) {
// we can assume its first command in array (spotify have only one command)
const command = (await global.integrations.spotify.commands())[0].command
const alias = await global.db.engine.findOne(global.systems.alias.collection.data, { command })
if (!_.isEmpty(alias) && alias.enabled && await global.systems.alias.isEnabled()) {
spotifyRegex = new RegExp('^(' + command + '|' + alias.alias + ') \\S+open\\.spotify\\.com\\/track\\/(\\w+)(.*)?', 'gi')
} else {
spotifyRegex = new RegExp('^(' + command + ') \\S+open\\.spotify\\.com\\/track\\/(\\w+)(.*)?', 'gi')
}
text = text.replace(spotifyRegex, '')
}
// check if songrequest -or- alias of songrequest contain youtube link
if (await global.systems.songs.isEnabled()) {
let alias = await global.db.engine.findOne(global.systems.alias.collection.data, { command: '!songrequest' })
if (!_.isEmpty(alias) && alias.enabled && await global.systems.alias.isEnabled()) {
ytRegex = new RegExp('^(!songrequest|' + alias.alias + ') \\S+(?:youtu.be\\/|v\\/|e\\/|u\\/\\w+\\/|embed\\/|v=)([^#&?]*).*', 'gi')
} else {
ytRegex = /^(!songrequest) \S+(?:youtu.be\/|v\/|e\/|u\/\w+\/|embed\/|v=)([^#&?]*).*/gi
}
text = text.replace(ytRegex, '')
}
const includeClips = this.settings.links.includeClips
if (!includeClips) {
clipsRegex = /.*(clips.twitch.tv\/)(\w+)/
text = text.replace(clipsRegex, '')
}
text = ` ${text} `
let whitelist = this.settings.lists.whitelist
for (let value of whitelist.map(o => o.trim().replace(/\*/g, '[\\pL0-9\\S]*').replace(/\+/g, '[\\pL0-9\\S]+'))) {
if (value.length > 0) {
let regexp
if (value.startsWith('domain:')) {
regexp = XRegExp(` [\\S]*${XRegExp.escape(value.replace('domain:', ''))}[\\S]* `, 'gi')
} else { // default regexp behavior
regexp = XRegExp(` [^\\s\\pL0-9\\w]?${value}[^\\s\\pL0-9\\w]? `, 'gi')
}
// we need to change 'text' to ' text ' for regexp to correctly work
text = XRegExp.replace(` ${text} `, regexp, '').trim()
}
}
return text
}
@command('!permit')
@default_permission(permission.CASTERS)
async permitLink (opts) {
try {
var parsed = opts.parameters.match(/^@?([\S]+) ?(\d+)?$/)
let count = 1
if (!_.isNil(parsed[2])) count = parseInt(parsed[2], 10)
for (let i = 0; i < count; i++) await global.db.engine.insert(this.collection.permits, { username: parsed[1].toLowerCase() })
let m = await commons.prepare('moderation.user-have-link-permit', { username: parsed[1].toLowerCase(), link: commons.getLocalizedName(count, 'core.links'), count: count })
commons.sendMessage(m, opts.sender)
} catch (e) {
commons.sendMessage(global.translate('moderation.permit-parse-failed'), opts.sender)
}
}
@parser({ priority: constants.MODERATION })
async containsLink (opts) {
let [isEnabled, isEnabledForSubs, isEnabledForSpaces, timeout, isOwner, isMod, whitelisted] = await Promise.all([
this.settings.links.enabled,
this.settings.links.moderateSubscribers,
this.settings.links.includeSpaces,
this.settings.links.timeout,
commons.isOwner(opts.sender),
commons.isModerator(opts.sender),
this.whitelist(opts.message)
])
if (isOwner || isMod || !isEnabled || (typeof opts.sender.badges.subscriber !== 'undefined' && !isEnabledForSubs)) {
return true
}
const urlRegex = isEnabledForSpaces
? /(www)? ??\.? ?[a-zA-Z0-9]+([a-zA-Z0-9-]+) ??\. ?(aero|bet|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|shop|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|money|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zr|zw)\b/ig
: /[a-zA-Z0-9]+([a-zA-Z0-9-]+)?\.(aero|bet|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|shop|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|money|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zr|zw)\b/ig
if (whitelisted.search(urlRegex) >= 0) {
let permit = await global.db.engine.findOne(this.collection.permits, { username: opts.sender.username })
if (!_.isEmpty(permit)) {
await global.db.engine.remove(this.collection.permits, { _id: permit._id.toString() })
return true
} else {
this.timeoutUser(opts.sender, whitelisted,
global.translate('moderation.user-is-warned-about-links'),
global.translate('moderation.user-have-timeout-for-links'),
timeout, 'links')
return false
}
} else {
return true
}
}
@parser({ priority: constants.MODERATION })
async symbols (opts) {
let [isEnabled, isEnabledForSubs, whitelisted, isOwner, isMod, timeout, triggerLength, maxSymbolsConsecutively, maxSymbolsPercent] = await Promise.all([
this.settings.symbols.enabled,
this.settings.symbols.moderateSubscribers,
this.whitelist(opts.message),
commons.isOwner(opts.sender),
commons.isModerator(opts.sender),
this.settings.symbols.timeout,
this.settings.symbols.triggerLength,
this.settings.symbols.maxSymbolsConsecutively,
this.settings.symbols.maxSymbolsPercent
])
var msgLength = whitelisted.trim().length
var symbolsLength = 0
if (isOwner || isMod || msgLength < triggerLength || !isEnabled || (typeof opts.sender.badges.subscriber !== 'undefined' && !isEnabledForSubs)) {
return true
}
var out = whitelisted.match(/([^\s\u0500-\u052F\u0400-\u04FF\w]+)/g)
for (var item in out) {
if (out.hasOwnProperty(item)) {
var symbols = out[item]
if (symbols.length >= maxSymbolsConsecutively) {
this.timeoutUser(opts.sender, opts.message,
global.translate('moderation.user-is-warned-about-symbols'),
global.translate('moderation.user-have-timeout-for-symbols'),
timeout, 'symbols')
return false
}
symbolsLength = symbolsLength + symbols.length
}
}
if (Math.ceil(symbolsLength / (msgLength / 100)) >= maxSymbolsPercent) {
this.timeoutUser(opts.sender, opts.message, global.translate('moderation.warnings.symbols'), global.translate('moderation.symbols'), timeout, 'symbols')
return false
}
return true
}
@parser({ priority: constants.MODERATION })
async longMessage (opts) {
let [isEnabled, isEnabledForSubs, isOwner, isMod, whitelisted, timeout, triggerLength] = await Promise.all([
this.settings.longMessage.enabled,
this.settings.longMessage.moderateSubscribers,
commons.isOwner(opts.sender),
commons.isModerator(opts.sender),
this.whitelist(opts.message),
this.settings.longMessage.timeout,
this.settings.longMessage.triggerLength
])
var msgLength = whitelisted.trim().length
if (isOwner || isMod || msgLength < triggerLength || !isEnabled || (typeof opts.sender.badges.subscriber !== 'undefined' && !isEnabledForSubs)) {
return true
} else {
this.timeoutUser(opts.sender, opts.message,
global.translate('moderation.user-is-warned-about-long-message'),
global.translate('moderation.user-have-timeout-for-long-message'),
timeout, 'longmessage')
return false
}
}
@parser({ priority: constants.MODERATION })
async caps (opts) {
let [isEnabled, isEnabledForSubs, isOwner, isMod, whitelisted, timeout, triggerLength, maxCapsPercent] = await Promise.all([
this.settings.caps.enabled,
this.settings.caps.moderateSubscribers,
commons.isOwner(opts.sender),
commons.isModerator(opts.sender),
this.whitelist(opts.message),
this.settings.caps.timeout,
this.settings.caps.triggerLength,
this.settings.caps.maxCapsPercent
])
let emotesCharList = []
if (Symbol.iterator in Object(opts.sender.emotes)) {
for (let emote of opts.sender.emotes) {
for (let i of _.range(parseInt(emote.start, 10), parseInt(emote.end, 10) + 1)) {
emotesCharList.push(i)
}
}
}
var msgLength = whitelisted.trim().length
var capsLength = 0
const regexp = /[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-./:;<=>?@[\]^_`{|}~]/gi
whitelisted = whitelisted.trim()
for (let i = 0; i < whitelisted.length; i++) {
// if is emote or symbol - continue
if (_.includes(emotesCharList, i) || !_.isNull(whitelisted.charAt(i).match(regexp))) {
msgLength = parseInt(msgLength, 10) - 1
continue
} else if (!_.isFinite(parseInt(whitelisted.charAt(i), 10)) && whitelisted.charAt(i).toUpperCase() === whitelisted.charAt(i) && whitelisted.charAt(i) !== ' ') {
capsLength += 1
}
}
if (isOwner || isMod || msgLength < triggerLength || !isEnabled || (typeof opts.sender.badges.subscriber !== 'undefined' && !isEnabledForSubs)) {
return true
}
if (Math.ceil(capsLength / (msgLength / 100)) >= maxCapsPercent) {
this.timeoutUser(opts.sender, opts.message,
global.translate('moderation.user-is-warned-about-caps'),
global.translate('moderation.user-have-timeout-for-caps'),
timeout, 'caps')
return false
}
return true
}
@parser({ priority: constants.MODERATION })
async spam (opts) {
let [isEnabled, isEnabledForSubs, isOwner, isMod, whitelisted, timeout, triggerLength, maxLength] = await Promise.all([
this.settings.spam.enabled,
this.settings.spam.moderateSubscribers,
commons.isOwner(opts.sender),
commons.isModerator(opts.sender),
this.whitelist(opts.message),
this.settings.spam.timeout,
this.settings.spam.triggerLength,
this.settings.spam.maxLength
])
var msgLength = whitelisted.trim().length
if (isOwner || isMod || msgLength < triggerLength || !isEnabled || (typeof opts.sender.badges.subscriber !== 'undefined' && !isEnabledForSubs)) {
return true
}
var out = whitelisted.match(/(.+)(\1+)/g)
for (var item in out) {
if (out.hasOwnProperty(item) && out[item].length >= maxLength) {
this.timeoutUser(opts.sender, opts.message,
global.translate('moderation.user-have-timeout-for-spam'),
global.translate('moderation.user-is-warned-about-spam'),
timeout, 'spam')
return false
}
}
return true
}
@parser({ priority: constants.MODERATION })
async color (opts) {
let [isEnabled, isEnabledForSubs, isOwner, isMod, timeout] = await Promise.all([
this.settings.color.enabled,
this.settings.color.moderateSubscribers,
commons.isOwner(opts.sender),
commons.isModerator(opts.sender),
this.settings.color.timeout
])
if (isOwner || isMod || !isEnabled || (typeof opts.sender.badges.subscriber !== 'undefined' && !isEnabledForSubs)) {
return true
}
if (opts.sender['message-type'] === 'action') {
this.timeoutUser(opts.sender, opts.message,
global.translate('moderation.user-is-warned-about-color'),
global.translate('moderation.user-have-timeout-for-color'),
timeout, 'color')
return false
} else return true
}
@parser({ priority: constants.MODERATION })
async emotes (opts) {
if (!(Symbol.iterator in Object(opts.sender.emotes))) return true
let [isEnabled, isEnabledForSubs, isOwner, isMod, timeout, maxCount] = await Promise.all([
this.settings.emotes.enabled,
this.settings.emotes.moderateSubscribers,
commons.isOwner(opts.sender),
commons.isModerator(opts.sender),
this.settings.emotes.timeout,
this.settings.emotes.maxCount
])
var count = opts.sender.emotes.length
if (isOwner || isMod || !isEnabled || (typeof opts.sender.badges.subscriber !== 'undefined' && !isEnabledForSubs)) {
return true
}
if (count > maxCount) {
this.timeoutUser(opts.sender, opts.message,
global.translate('moderation.user-is-warned-about-emotes'),
global.translate('moderation.user-have-timeout-for-emotes'),
timeout, 'emotes')
return false
} else return true
}
@parser({ priority: constants.MODERATION })
async blacklist (opts) {
let [isEnabledForSubs, isOwner, isMod, timeout, blacklist] = await Promise.all([
this.settings.lists.moderateSubscribers,
commons.isOwner(opts.sender),
commons.isModerator(opts.sender),
this.settings.lists.timeout,
this.settings.lists.blacklist
])
if (isOwner || isMod || (typeof opts.sender.badges.subscriber !== 'undefined' && !isEnabledForSubs)) {
return true
}
let isOK = true
for (let value of blacklist.map(o => o.trim().replace(/\*/g, '[\\pL0-9]*').replace(/\+/g, '[\\pL0-9]+'))) {
if (value.length > 0) {
const regexp = XRegExp(` [^\\s\\pL0-9\\w]?${value}[^\\s\\pL0-9\\w]? `, 'gi')
// we need to change 'text' to ' text ' for regexp to correctly work
if (XRegExp.exec(` ${opts.message} `, regexp)) {
isOK = false
this.timeoutUser(opts.sender, opts.message,
global.translate('moderation.user-is-warned-about-blacklist'),
global.translate('moderation.user-have-timeout-for-blacklist'),
timeout, 'blacklist')
break
}
}
}
return isOK
}
async isSilent (name) {
let item = await global.db.engine.findOne(this.collection.messagecooldown, { key: name })
if (_.isEmpty(item) || (_.now() - item.value) >= 60000) {
await global.db.engine.update(this.collection.messagecooldown, { key: name }, { value: _.now() })
return false
}
return true
}
}
module.exports = new Moderation()