-
Notifications
You must be signed in to change notification settings - Fork 61
/
utility.js
132 lines (114 loc) · 3.79 KB
/
utility.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
/*
* creadit @dragonfire535 github: https://github.com/dragonfire535/xiao/blob/45dfdb0ccda888ecc5398fb4207265d44b69b3cc/util/Util.js
*/
const request = require('node-superfetch');
const crypto = require('crypto');
const Discord = require('discord.js');
module.exports = {
delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
},
shuffle(array) {
const arr = array.slice(0);
for (let i = arr.length - 1; i >= 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return arr;
},
list(arr, conj = 'and') {
const len = arr.length;
return `${arr.slice(0, -1).join(', ')}${len > 1 ? `${len > 2 ? ',' : ''} ${conj} ` : ''}${arr.slice(-1)}`;
},
shorten(text, maxLen = 2000) {
return text.length > maxLen ? `${text.substr(0, maxLen - 3)}...` : text;
},
duration(ms) {
const sec = Math.floor((ms / 1000) % 60).toString();
const min = Math.floor((ms / (1000 * 60)) % 60).toString();
const hrs = Math.floor(ms / (1000 * 60 * 60)).toString();
return `${hrs.padStart(2, '0')}:${min.padStart(2, '0')}:${sec.padStart(2, '0')}`;
},
randomRange(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
},
trimArray(arr, maxLen = 10) {
if (arr.length > maxLen) {
const len = arr.length - maxLen;
arr = arr.slice(0, maxLen);
arr.push(`${len} more...`);
}
return arr;
},
base64(text, mode = 'encode') {
if (mode === 'encode') return Buffer.from(text).toString('base64');
if (mode === 'decode') return Buffer.from(text, 'base64').toString('utf8') || null;
throw new TypeError(`${mode} is not a supported base64 mode.`);
},
// http://blog.stevenlevithan.com/archives/javascript-roman-numeral-converter by Iván -DrSlump- Montes
romanize(num) {
const lookup = { M:1000, CM:900, D:500, CD:400, C:100, XC:90, L:50, XL:40, X:10, IX:9, V:5, IV:4, I:1 };
let roman = '';
let i;
for (i in lookup) {
while (num >= lookup[i]) {
roman += i;
num -= lookup[i];
}
}
return roman;
},
// http://blog.stevenlevithan.com/archives/javascript-roman-numeral-converter by Iván -DrSlump- Montes
deromanize(roman) {
roman = roman.toUpperCase();
const lookup = { I:1, V:5, X:10, L:50, C:100, D:500, M:1000 };
let arabic = 0;
let i = roman.length;
while (i--) {
if (lookup[roman[i]] < lookup[roman[i+1]]) {
arabic -= lookup[roman[i]];
}
else {
arabic += lookup[roman[i]];
}
}
return arabic;
},
hash(text, algorithm) {
return crypto.createHash(algorithm).update(text).digest('hex');
},
checkRole(newXP, role, message, config) {
const user = message.member;
const level = Math.floor(newXP / config.levelXP);
if (newXP >= config.VIP * config.levelXP && !user.roles.has(role.id)) {
user.addRole(role);
let VIPembed = new Discord.RichEmbed()
.setTitle('**Congratulations**, you are offically a **VIP**!!!')
.setAuthor(`${message.author.username}`, `${message.author.displayAvatarURL}`)
.setDescription('VIP are granted extra permission to manage some parts of the server.')
.setThumbnail(`${message.author.displayAvatarURL}`)
.addField('XP', newXP, true)
.addField('Level', level, true)
.addField('New Role', 'VIP', true)
.setTimestamp(new Date())
.setFooter('VIP Announcement');
return message.channel.send(VIPembed);
}
},
parseComplexLastArgs(args, maxLength, standard) {
let length = maxLength;
if(args.length < maxLength + 1) {
length -= (maxLength + 1 - args.length);
}
const possibleArgs = args.slice(-1 * length);
for(let i = 0; i < length; i++) {
const arg = possibleArgs.slice(i).join('_').toUpperCase();
if (standard[arg]) {
return { 'value': arg , 'index': args.length - (length - i) };
}
}
return null;
},
};