-
Notifications
You must be signed in to change notification settings - Fork 8
/
functions.js
181 lines (137 loc) · 6.48 KB
/
functions.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
const Discord = require('discord.js'),
sm = require('string-similarity'),
ms = require('parse-ms');
module.exports = {
parseTime: function(milliseconds, from, seconds) {
var string = '', obj;
if (!from) obj = ms(Date.now() - milliseconds);
else obj = ms(milliseconds)
if (obj.days === 1) string += ` ${obj.days} day `
else if (obj.days > 1) string += ` ${obj.days} days `
if (obj.hours === 1) string += `${obj.hours} hour `
else if (obj.hours > 1) string += `${obj.hours} hours `
if (obj.minutes === 1) string += `${obj.minutes} minute `
else if (obj.minutes > 1) string += `${obj.minutes} minutes `
if (seconds && obj.seconds === 1) string += `${obj.seconds} second `
else if (seconds && obj.seconds > 1) string += `${obj.seconds} seconds `
if (string === '') string = 'Just now'
else string += 'ago'
return string;
},
fetchLastAudit: async function(guild, type) {
const getInfo = new Promise((resolve, error) => {
setTimeout(function(){ // The timeout is to make sure audit log has updated
if (type) {
guild.fetchAuditLogs({limit: 1, type: type}).then(item => {
resolve(item.entries.first())
}).catch(e => {
console.log(`Invalid permissions in ${guild.name}`);
return false;
})
} else {
guild.fetchAuditLogs({limit: 1}).then(item => {
resolve(item.entries.first())
}).catch(e => {
console.log(`Invalid permissions in ${guild.name}`);
return false;
})
}
}, 500)
});
return getInfo;
},
checkPings: async function(client, guild, id) {
// Fetch Limits
let limits = client.db.get(`actionLimits_${guild.id}`);
if (!limits) limits = {};
let currentLimit = {};
currentLimit.minute = limits.pingsPM || 5;
currentLimit.hour = limits.pingPH || 15;
// Fetch Data
let pings = client.guildPings.get(guild.id);
pings = pings.filter(p => p.user.id === id);
let pingsPM = pings.slice(parseInt(`-${currentLimit.minute}`)).reverse();
let pingsPH = pings.slice(parseInt(`-${currentLimit.hour}`)).reverse();
let reached = null;
if (pingsPM[currentLimit.minute - 1] && Date.now() - pingsPM[currentLimit.minute - 1].timestamp < 60000) reached = 'Minute';
else if (pingsPH[currentLimit.hour - 1] && Date.now() - pingsPH[currentLimit.hour - 1].timestamp < 3.6e+6) reached = 'Hour';
if (!reached) return;
else {
// Add Muted Role
let muted = guild.roles.find(r => r.name === 'Muted');
if (muted) guild.members.get(id).roles.add(muted);
if (client.isNode) return;
// Post Announcement
let channel = guild.channels.find(c => c.name === 'mutes' || c.name === 'mute-repeals');
if (channel) {
let msg = '';
let user = client.users.get(id);
if (reached === 'Minute') for (var i in pingsPM) msg += `\`${module.exports.parseTime(pingsPM[i].timestamp)}\` | **${pingsPM[i].user.tag}** *pinged* **${pingsPM[i].target}**\n`
else for (var i in pingsPH) msg += `\`${module.exports.parseTime(pingsPH[i].timestamp)}\` | **${pingsPH[i].user.tag}** *pinged* **${pingsPH[i].target}**\n`;
const embed = new Discord.MessageEmbed()
.setColor(client.color)
.setTitle(`Ping Limit Reached! - ${reached}`)
.addField('Recent Actions', msg)
.addField('Limit Reached By', user, true)
.addField('Automatic Action Taken', '**Muted Role Added**', true);
// Send Embed
user.send(embed).catch(err => console.log(err));
channel.send(embed).catch(err => console.log(err));
}
}
},
check: async function(client, guild, execID, type) {
// Fetch Limits
let limits = client.db.get(`actionLimits_${guild.id}`);
if (!limits) limits = {};
let currentLimit = {};
// Types
if (type === 'userRemovals') {
currentLimit.minute = limits.removalsPM || 5;
currentLimit.hour = limits.removalsPH || 15;
} else if (type === 'roleDeletions') {
currentLimit.minute = limits.roleDeletionsPM || 4;
currentLimit.hour = limits.roleDeletionsPH || 12;
} else if (type === 'channelDeletions') {
currentLimit.minute = limits.channelDeletionsPM || 4;
currentLimit.hour = limits.channelDeletionsPH || 12;
}
// Fetch Data
let data = client.db.get(`${type}_${guild.id}_${execID}`);
if (!data) data = [];
// Splice Limits
let dataMinute = data.slice(parseInt(`-${currentLimit.minute}`)).reverse();
let dataHour = data.slice(parseInt(`-${currentLimit.hour}`)).reverse();
// Check Reached
let reached = null;
if (dataMinute[currentLimit.minute - 1] && Date.now() - dataMinute[currentLimit.minute - 1].timestamp < 60000) reached = 'Minute';
if (dataHour[currentLimit.hour - 1] && Date.now() - dataHour[currentLimit.hour - 1].timestamp < 3.6e+6) reached = 'Hour';
// Act Accordingly
if (!reached) return;
else {
// Remove Roles
guild.members.get(execID).roles.set([]);
if (client.isNode) return;
// Output Message
let msg = '',
action = '',
executor = client.users.get(execID);
// Check Types
if (type === 'userRemovals') type = '*kicked/banned*';
if (type === 'roleDeletions') type = '*deleted*';
if (type === 'channelDeletions') type = '*deleted*';
if (reached === 'Minute') for (var i in dataMinute) msg += `\`${module.exports.parseTime(dataMinute[i].timestamp)}\` | **${dataMinute[i].executor.tag}** ${type} **${dataMinute[i].target.tag}**\n`
else for (var i in dataHour) msg += `\`${module.exports.parseTime(dataHour[i].timestamp)}\` | **${dataHour[i].executor.tag}** ${type} **${dataHour[i].target.tag}**\n`;
// Form Embed
const embed = new Discord.MessageEmbed()
.setColor(client.color)
.setTitle(`Limit Reached! - ${reached}`)
.addField('Recent Actions', msg)
.addField('Limit Reached By', executor, true)
.addField('Automatic Action Taken', '**All Roles Removed**', true);
// Send Embed
executor.send(embed).catch(err => console.log(err));
guild.owner.send(embed).catch(err => console.log(err));
}
}
}