-
Notifications
You must be signed in to change notification settings - Fork 1
/
spam-detection.js
55 lines (48 loc) · 1.67 KB
/
spam-detection.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
module.exports = {
messageLog: [],
message: '',
/**
* @param message - Discord.js message object
* @param maxLog - Maximum amount of recorded messages log
*/
log: function(message, maxLog){
this.message = message
this.messageLog.push({
author: message.author.id,
content: message.content,
timeStamp: Date.now()
})
//cleaning up the log
if (this.messageLog.length > maxLog) this.messageLog.shift()
},
/**
* @param amount - Treshold of messages in interval
* @param interval - The interval (in millisecond)
*/
tooQuick: function(amount, interval){
// getting the messages of last message' author from log
let msg = this.messageLog.filter(log =>log.author == this.message.author.id)
// ignore it if the logs are bellow treshold
if(msg.length < amount) return false
// space-time continum manipulation...
let lastTimeStamp = msg[msg.length - amount].timeStamp
let currentTimeStamp = msg[msg.length - 1].timeStamp
let msgInterval = currentTimeStamp - lastTimeStamp
if( msgInterval <= interval) return true
return false
},
/**
* @param amount - Max treshold of same messages
*/
sameMessages: function(amount){
// getting the messages of last message' author from log.. again
let msg = this.messageLog.filter(log => log.author == this.message.author.id)
let msgContent = msg.map(log => log.content).join(' ')
// message that just sent
let currentMsg = this.message.content
// check if its same with other messages
let occurance = (msgContent.match(new RegExp(currentMsg, "g")) || []).length
if (occurance >= amount) return true
return false
}
}