-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUtil.js
146 lines (146 loc) · 5.36 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const { ReactionCollector } = require("../");
/**
* Utils class
*/
module.exports = class Utils {
static reactionMenu(message, pages = [], options = {}) {
const {agree = false, mapFunc = (x) => x, fast = false} = options;
const getMessage = (content) => {
const obj = { content: "", embed: {} };
switch (typeof content) {
case "string":
return { content };
case "object":
if (content.title || content.type ||
content.description || content.url ||
content.timestamp || content.color ||
content.footer || content.image ||
content.thumbnail || content.video ||
content.provider || content.author ||
content.fields) obj.embed = content;
if (content.content) obj.content = content.content;
if (content.embed) obj.embed = content.embed;
return obj;
}
};
return new Promise(async (resolve) => {
const contents = pages.map(mapFunc);
const msg = await message.channel.createMessage(getMessage(contents[0]));
let page = 0;
if (message.channel.guild && !message.channel.permissionsOf(message._client.user.id).has("addReactions")) return resolve([pages[0], 0]);
if (contents.length <= 1) return resolve([pages[0], 0]);
const collector = new ReactionCollector(msg, (emoji, userID) => ["◀", "▶", "⏹"].concat(agree ? "✅" : [], fast ? ["⏮", "⏭"] : []).includes(emoji.name) && userID === message.author.id);
const edit = () => msg.edit(getMessage(contents[page]));
collector.on("collect", (emoji) => {
if (emoji.name === "◀") {
if (page === 0) return;
page--;
edit();
}
if (emoji.name === "▶") {
if (page === contents.length - 1) return;
page++;
edit();
}
if (agree && emoji.name === "✅") {
collector.stop();
return resolve([pages[page], page]);
}
if (emoji.name === "⏹") {
collector.stop();
return resolve([]);
}
if (fast && emoji.name === "⏮") {
if (page === 0) return;
page = 0;
edit();
}
if (fast && emoji.name === "⏭") {
if (page === pages.length - 1) return;
page = pages.length - 1;
edit();
}
});
collector.on("remove", (emoji) => {
if (emoji.name === "◀") {
if (page === 0) return;
page--;
edit();
}
if (emoji.name === "▶") {
if (page === contents.length - 1) return;
page++;
edit();
}
if (fast && emoji.name === "⏮") {
if (page === 0) return;
page = 0;
edit();
}
if (fast && emoji.name === "⏭") {
if (page === pages.length - 1) return;
page = pages.length - 1;
edit();
}
});
collector.on("end", () => {
msg.removeReactions().catch(() => {});
});
if (fast) await msg.addReaction("⏮");
await msg.addReaction("◀");
if (agree) await msg.addReaction("✅");
await msg.addReaction("▶");
if (fast) await msg.addReaction("⏭");
await msg.addReaction("⏹");
});
}
static get colors() {
return {
"&0": 0x000000,
"&1": 0x0000AA,
"&2": 0x00AA00,
"&3": 0x00AAAA,
"&4": 0xAA0000,
"&5": 0xAA00AA,
"&6": 0xFFAA00,
"&7": 0xAAAAAA,
"&8": 0x555555,
"&a": 0x55FF55,
"&b": 0x55FFFF,
"&c": 0xFF5555,
"&d": 0xFF55FF,
"&e": 0xFFFF55,
"&f": 0xFFFFFF
};
}
static declOfNum (number, titles) {
const n = Math.abs(number) % 100;
const n1 = n % 10;
if (n > 10 && n < 20) { return titles[2]; }
if (n1 > 1 && n1 < 5) { return titles[1]; }
if (n1 === 1) { return titles[0]; }
return titles[2];
}
static timeObj (milliseconds) {
let days, hours, minutes, seconds;
seconds = Math.floor(milliseconds / 1000);
minutes = Math.floor(seconds / 60);
seconds = seconds % 60;
hours = Math.floor(minutes / 60);
minutes = minutes % 60;
days = Math.floor(hours / 24);
hours = hours % 24;
return {
days,
hours,
minutes,
seconds
};
}
static applyDescription (array, { mapFunc = (x) => x, separator = ", " }) {
do {
array.pop();
} while (array.map(mapFunc).join(separator).length > 2048);
return array.map(mapFunc).join(separator);
}
};