forked from TheBastionBot/Bastion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordFilter.js
68 lines (60 loc) · 1.96 KB
/
wordFilter.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
/**
* @file wordFilter
* @author Sankarsan Kampa (a.k.a k3rn31p4nic)
* @license GPL-3.0
*/
/**
* Handles filtering of specific words in messages
* @param {Message} message Discord.js message object
* @returns {Promise<true>} If the message was filtered
*/
module.exports = async message => {
try {
// If the user has Manage Server permission, return
if (message.member && message.member.hasPermission('MANAGE_GUILD')) return;
// Fetch filter data from database
let guildModel = await message.client.database.models.guild.findOne({
attributes: [ 'guildID', 'filteredWords' ],
where: {
guildID: message.guild.id,
filterWords: true
},
include: [
{
model: message.client.database.models.textChannel,
attributes: [ 'channelID', 'ignoreWordFilter' ]
},
{
model: message.client.database.models.role,
attributes: [ 'roleID', 'ignoreWordFilter' ]
}
]
});
// If word filter is disabled, return
if (!guildModel) return;
// If the channel is whitelisted, return
if (guildModel.textChannels.
filter(channel => channel.dataValues.ignoreWordFilter).
map(channel => channel.dataValues.channelID).
includes(message.channel.id)) return;
// If the user is in a whitelisted role, return
let whitelistedRoles = guildModel.roles.
filter(role => role.dataValues.ignoreWordFilter).
map(role => role.dataValues.roleID);
for (let role of whitelistedRoles) {
if (message.member.roles.has(role)) return;
}
let filteredWords = guildModel.dataValues.filteredWords ? guildModel.dataValues.filteredWords : [];
for (let word of filteredWords) {
if (message.content.toLowerCase().split(' ').includes(word.toLowerCase())) {
if (message.deletable) {
message.delete().catch(() => {});
}
return true;
}
}
}
catch (e) {
message.client.log.error(e);
}
};