-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
202 lines (171 loc) · 5.73 KB
/
util.js
File metadata and controls
202 lines (171 loc) · 5.73 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const DISCORD_CHAR_LIMIT = 2000;
const MY_CHAR_LIMIT = 10000;
const ALPHABET = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''),
LCALPHABET = 'abcdefghijklmnopqrstuvwxyz'.split('');
const config = require('./config.json');
module.exports.MY_CHAR_LIMIT = 1000;
/**
* Checks if an object is empty, that is, if it is {}.
*
* @param obj The object.
* @returns {boolean} Whether the object is empty.
*/
module.exports.objectIsEmpty = obj => {
// https://stackoverflow.com/a/32108184/1175276
return Object.keys(obj).length === 0 && obj.constructor === Object;
};
/**
* Determines if the message was sent by a server admin.
*
* @param msg The message sent.
* @returns {boolean} true if sent by an admin, false otherwise.
*/
const sentByAdmin = (msg) => {
return msg.member.hasPermission('ADMINISTRATOR');
};
/**
* Determines if the message was sent by a me (user running the bot).
*
* @param msg The message sent.
* @returns {boolean} true if sent by me, false otherwise.
*/
const sentByMe = (msg) => {
return msg.member.id === config.admin_snowflake;
};
/**
* Determines if the message was sent by a server admin, or by me (user running the bot).
*
* @param msg The message sent.
* @returns {boolean} true if sent by an admin or me, false otherwise.
*/
module.exports.sentByAdminOrMe = (msg) => {
return sentByAdmin(msg) || sentByMe(msg);
};
// export
module.exports.sentByAdmin = sentByAdmin;
module.exports.sentByMe = sentByMe;
/**
* Removes *all* whitespace from a string.
*
* @param str The string.
*/
module.exports.removeWhitespace = (str) => {
return str.replace(/ /g, '');
};
/**
* Rounds a float to a specified number of digits.
*
* @param n The number to round.
* @param digits The number of digits (not decimal places) to round to.
* @returns {number} The rounded value.
*/
module.exports.roundTo = (n, digits) => {
if (digits === undefined) {
digits = 0;
}
const multiplicator = Math.pow(10, digits);
n = parseFloat((n * multiplicator).toFixed(11));
const test = (Math.round(n) / multiplicator);
return +(test.toFixed(digits));
};
/**
* Formats a number with commas, e.g. 1234567 => 1,234,567
* @param x The number.
* @returns {string} The number formatted with commas.
*/
module.exports.numberWithCommas = (x) => {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
};
/**
* Generates a random integer in a specified range.
*
* @param min The lower bound for the range.
* @param max The upper bound for the range.
* @returns {number} The random integer.
*/
module.exports.randomInRange = (min, max) => {
return Math.floor(Math.random() * (max + 1 - min)) + min;
};
/**
* Provides the link to a youtube video, given its youtube ID.
*
* @param id The video id.
* @returns {string} The link.
*/
module.exports.youtubeIDToLink = (id) => {
return 'https://www.youtube.com/watch?v=' + id;
};
/**
* Send a message that may or may not be longer than Discord's char limit. If it is not longer then just send it; if it
* is longer then split it into several sub-limit chunks, sending each one individually, and wrapping them all with a
* wrapper if needed (for example ``` ```) for code. However, if the message is way too long (longer than "my" char
* limit) then don't send it.
*
* @param channel The current channel (access using msg.channel from commands).
* @param text The text of the message to send.
* @param surround The wrapper for the message(s).
* @returns {boolean} true if the message was sent, false if it was not (due to excessive size).
*/
module.exports.safeSendMsg = (channel, text, surround = '') => {
// TODO: Implement optional "smart" mode where it tries to not cut off in the middle of words
let localCharLim = DISCORD_CHAR_LIMIT - 2 * surround.length;
if (text.length > MY_CHAR_LIMIT) {
return false;
} else if (text.length < localCharLim) {
channel.send(surround + text + surround);
return true;
} else {
let textTemp = text;
for (let i = 0; i <= textTemp.length % localCharLim; i++) {
channel.send(surround + textTemp.slice(0, localCharLim) + surround);
textTemp = textTemp.slice(localCharLim);
}
return true;
}
};
module.exports.TypeCheck = {
string: p => true,
char: p => p.length === 1,
int: p => Number.isInteger(parseInt(p)),
number: p => !isNaN(p),
all: p => true
};
module.exports.sendErrorMessage = (channel, errorMsg) => {
channel.send(`Error: ${errorMsg}`);
};
module.exports.isPM = msg => {
return msg.channel.type === 'dm' || !msg.guild;
};
const arraysEqual = (a, b) => {
/*
Array-aware equality checker:
Returns whether arguments a and b are == to each other;
however if they are equal-lengthed arrays, returns whether their
elements are pairwise == to each other recursively under this
definition.
*/
if (a instanceof Array && b instanceof Array) {
if (a.length !== b.length) // assert same length
return false;
for (let i = 0; i < a.length; i++) // assert each element equal
if (!arraysEqual(a[i], b[i]))
return false;
return true;
} else {
return a === b; // if not both arrays, should be the same
}
};
module.exports.isLetter = char => {
return ALPHABET.indexOf(char) !== -1;
};
module.exports.isPhrase = str => {
return !str.split('').some(elem => ALPHABET.indexOf(elem) === -1 && elem !== ' ');
};
module.exports.letterToIndex = char => {
return LCALPHABET.indexOf(char.toLowerCase());
};
module.exports.indexToLetter = num => LCALPHABET[num % 26];
module.exports.arraysEqual = arraysEqual;
module.exports.tagUser = (user) => {
return `<@${user.id}>`;
};