-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcommonFunctions.js
130 lines (113 loc) · 3.2 KB
/
commonFunctions.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
module.exports = {
minecraftToAnsi: (text) => {
colors = {
'0': 30,
'1': 34,
'2': 32,
'3': 36,
'4': 31,
'5': 35,
'6': 33,
'7': 37,
'8': 30,
'9': 34,
'a': 32,
'b': 36,
'c': 31,
'd': 35,
'e': 33,
'f': 37,
}
formats = {
'l': 1,
'm': 0,
'n': 4,
'r': 0,
}
result = '```ansi\n'
splitText = text.split('§');
if (splitText.length == 1) return text;
color = 30;
format = 0;
if (text.startsWith('§')) {
if (colors[text.charAt(1)] != null) {
color = colors[text.charAt(1)];
}
if (formats[text.charAt(1)] != null) {
format = formats[text.charAt(1)];
}
result += `\u001b[${format};${color}m` + String(splitText[0]).substring(1);
} else {
result += splitText[0];
}
for (var i = 1; i < splitText.length; i++) {
if (colors[splitText[i].charAt(0)] != null) {
color = colors[splitText[i].charAt(0)];
}
if (formats[splitText[i].charAt(0)] != null) {
format = formats[splitText[i].charAt(0)];
}
result += `\u001b[${format};${color}m` + splitText[i].substring(1);
}
result += '```';
return result;
},
getDescription: (response) => {
var description = '';
if (response == null) {
description = ''; // zero width space
} else if (response.extra != null && response.extra.length > 0) {
if (response.extra[0].extra == null) {
for (var i = 0; i < response.extra.length; i++) {
description += response.extra[i].text;
}
} else {
for (var i = 0; i < response.extra[0].extra.length; i++) {
description += response.extra[0].extra[i].text;
}
}
} else if (response.text != null) {
description = response.text;
} else if (response.translate != null) {
description = response.translate;
} else if (Array.isArray(response)) {
for (var i = 0; i < response.length; i++) {
description += response[i].text;
}
} else if (response != null) {
description = response;
} else {
description = 'Couldn\'t get description.';
}
if (description.length > 150) {
description = description.substring(0, 150) + '...';
}
// Convert Minecraft color and formatting codes to ANSI format
description = module.exports.minecraftToAnsi(description);
if (description == '') {
description = ''; // zero width space
}
return String(description);
},
getVersion: (rawVersion) => {
version = '';
if (rawVersion == null) {
version = ''; // zero width space
} else if (rawVersion.name == null) {
version = rawVersion;
} else {
version = rawVersion.name;
}
version = String(version);
if (version.length > 150) {
version = version.substring(0, 150) + '...';
}
// Convert Minecraft color and formatting codes to ANSI format
version = module.exports.minecraftToAnsi(version);
if (version == '') {
version = ''; //zero width space
}
return version;
},
thousandsSeparators: (num) => num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
}