-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
84 lines (78 loc) · 2.48 KB
/
utils.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
const fs = require('fs');
const { exec } = require('child_process');
const constants = require('./constants.js');
const { Client, Contact } = require('whatsapp-web.js');
function importModules(dir, reload = false) {
var modules = {};
fs.readdirSync(dir).forEach(file => {
path = fs.realpathSync(`${dir}/${file}`);
if (fs.lstatSync(path).isDirectory()) {
modules = Object.assign(modules, importModules(path));
} else {
if (file.endsWith('.js')) {
if (reload) {
delete require.cache[require.resolve(path)];
}
modules[path] = require(path);
}
}
});
return modules;
}
function importNpmPlugins() {
var plugins = {};
exec('npm list --depth=0 --json', (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
var npmList = JSON.parse(stdout);
for (const moduleName in Object.keys(npmList.dependencies)) {
if (moduleName.startsWith(constants.NPM_PLUGIN_PREFIX)) {
plugins[moduleName] = require(moduleName);
}
}
});
return plugins;
}
async function messageGroupAdminCheck(message) {
const contact = await message.getContact();
const chat = await message.getChat();
return contactGroupAdminCheck(contact, chat);
}
async function contactGroupAdminCheck(contact, chat) {
var contactId
if (contact instanceof Contact) {
contactId = contact.id._serialized;
} else {
contactId = contact;
}
const participants = chat.participants;
const callerParticipant = participants.find(participant => participant.id._serialized === contactId);
try {
return (callerParticipant.isAdmin || callerParticipant.isSuperAdmin);
} catch (err) {
if (err instanceof TypeError) {
throw ReferenceError;
}
}
}
async function contactBotAdminCheck(contact, nconf) {
var contactId
if (contact instanceof Contact) {
contactId = contact.id.user;
} else {
contactId = contact;
}
// check if contact is in the admin list in nconf
if (nconf.get("BOT_ADMINS").includes(contactId)) {
return true;
}
}
module.exports = {
importModules: importModules,
importNpmPlugins: importNpmPlugins,
contactGroupAdminCheck: contactGroupAdminCheck,
messageGroupAdminCheck: messageGroupAdminCheck,
contactBotAdminCheck: contactBotAdminCheck
};