forked from Fuseteam/xiaobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.js
106 lines (92 loc) · 2.88 KB
/
Util.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
const crypto = require('crypto');
const yes = ['yes', 'y', 'ye', 'yeah', 'yup', 'yea', 'ya'];
const no = ['no', 'n', 'nah', 'nope', 'nop'];
module.exports = class Util {
static delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
static 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;
}
static list(arr, conj = 'and') {
const len = arr.length;
return `${arr.slice(0, -1).join(', ')}${len > 1 ? `${len > 2 ? ',' : ''} ${conj} ` : ''}${arr.slice(-1)}`;
}
static shorten(text, maxLen = 2000) {
return text.length > maxLen ? `${text.substr(0, maxLen - 3)}...` : text;
}
static randomRange(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
static trimArray(arr, maxLen = 10) {
if (arr.length > maxLen) {
const len = arr.length - maxLen;
arr = arr.slice(0, maxLen);
arr.push(`${len} more...`);
}
return arr;
}
static firstUpperCase(text, split = ' ') {
return text.split(split).map(word => `${word.charAt(0).toUpperCase()}${word.slice(1)}`).join(' ');
}
static formatNumber(number) {
return Number.parseFloat(number).toLocaleString(undefined, { maximumFractionDigits: 2 });
}
static 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.`);
}
static hash(text, algorithm) {
return crypto.createHash(algorithm).update(text).digest('hex');
}
static today(timeZone) {
const now = new Date();
if (timeZone) now.setUTCHours(timeZone);
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
now.setMilliseconds(0);
return now;
}
static tomorrow(timeZone) {
const today = Util.today(timeZone);
today.setDate(today.getDate() + 1);
return today;
}
static async verify(channel, user, time = 30000) {
const filter = res => {
const value = res.content.toLowerCase();
return res.author.id === user.id && (yes.includes(value) || no.includes(value));
};
const verify = await channel.awaitMessages(filter, {
max: 1,
time
});
if (!verify.size) return 0;
const choice = verify.first().content.toLowerCase();
if (yes.includes(choice)) return true;
if (no.includes(choice)) return false;
return false;
}
static cleanAnilistHTML(html) {
let clean = html
.replace(/(<br>)+/g, '\n')
.replace(/'/g, '\'')
.replace(/"/g, '"')
.replace(/<\/?i>/g, '*')
.replace(/~!|!~/g, '||')
.replace(/—/g, '—');
if (clean.length > 2000) clean = `${clean.substr(0, 1995)}...`;
const spoilers = (clean.match(/\|\|/g) || []).length;
if (spoilers !== 0 && (spoilers && (spoilers % 2))) clean += '||';
return clean;
}
};