-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathutils.js
More file actions
94 lines (85 loc) · 2.24 KB
/
utils.js
File metadata and controls
94 lines (85 loc) · 2.24 KB
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
'use strict';
const fs = require('fs');
const path = require('path');
const ejs = require('ejs');
const TemplatesDir = path.resolve(__dirname, '../../templates');
const config = require('../services/config.js');
const generateString = () => {
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
};
const hasGuild = (guilds) => {
if (!config.discord.enabled) {
return true;
}
if (config.discord.allowedGuilds.length === 0) {
return true;
}
if (guilds.length === 0) {
return false;
}
for (let i = 0; i < guilds.length; i++) {
const guild = guilds[i];
if (config.discord.allowedGuilds.includes(guild)) {
return true;
}
}
return false;
};
const hasRole = (userRoles, requiredRoles) => {
if (!config.discord.enabled) {
return true;
}
if (requiredRoles.length === 0) {
return true;
}
if (userRoles.length === 0) {
return false;
}
for (let i = 0; i < userRoles.length; i++) {
const role = userRoles[i];
if (requiredRoles.includes(role)) {
return true;
}
}
return false;
};
const zeroPad = (num, places) => String(num).padStart(places, '0');
const fileExists = async (path) => {
return new Promise((resolve, reject) => {
try {
fs.exists(path, (exists) => {
resolve(exists);
});
} catch (e) {
return reject(e);
}
});
};
const render = async (name, data) => {
const filePath = path.resolve(TemplatesDir, name + '.ejs');
if (!await fileExists(filePath)) {
throw `Template ${filePath} does not exist!`;
}
return new Promise((resolve, reject) => {
try {
ejs.renderFile(filePath, data, (err, str) => {
if (err) {
console.error('Template render error:', err);
//return reject(err);
}
resolve(str);
});
} catch (e) {
console.error('Template error:', e);
return reject(e);
}
});
};
module.exports = {
generateString,
hasGuild,
hasRole,
zeroPad,
fileExists,
render
};