-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.js
86 lines (74 loc) · 2.35 KB
/
helper.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
const permissions = require('./permissions.js');
/**
* Formats the list so it will look nice when converted to markdown.
*
* @param {Array} array
* @param {Object} command
* @return {Boolean|String}
*/
module.exports.formatList = function (array, command) {
if (array == null) {
return false;
}
return array.map(line => {
let parts = line.split('` -');
if (parts.length <= 1) {
return formatLine(line, command);
}
return formatLine(parts[0], command) + `\n\t- ${parts[1]}`;
}).join('\n');
};
function formatLine(line, command) {
return '\t' + line.replace(/\:command/, command.trigger)
.replace(/`/g, '')
.trim();
}
/**
* Formats a list of middelwares by their group, parts,
* and sub-parts if the middleware requires it.
*
* @param {Array} middlewares
* @return {String|null}
*/
module.exports.formatMiddleware = function (middlewares) {
if (middlewares.length == 0) {
return false;
}
let map = [];
for (let middleware of middlewares) {
let parts = middleware.split(':');
let result = null;
switch (parts.shift().toLowerCase()) {
case 'require':
result = formateRequire('and', ...parts[0].split(','));
if (result != null) {
map.push(result);
}
break;
case 'requireOne':
result = formateRequire('or', ...parts[0].split(','));
if (result != null) {
map.push(result);
}
break;
}
}
return map;
}
function formateRequire(seperator, type, ...middlewares) {
let multiple = middlewares.length > 1 ? 's' : '';
middlewares = middlewares.filter(permission => {
return permissions.hasOwnProperty(permission);
}).map(permission => {
return permissions[permission];
}).join(`** ${seperator} **`);
switch (type) {
case 'user':
return `The user needs **${middlewares}** permission${multiple} to run this command.`;
case 'bot':
return `The bot needs **${middlewares}** permission${multiple} to run this command.`;
case 'all':
return `The bot and the user needs the **${middlewares}** permission${multiple} to run this command.`;
}
return null;
}