Built-in Functions
For async/await, we can use this to pause the bot and handle certain events before proceeding.
// `await client.wait(1000);` to "pause" for 1 second.
client.wait = require("util").promisify(setTimeout);
A built-in Capitalization String Function for words in a sentence format.
// <String>.toProperCase() returns a proper-cased string such as:
// "A quick brown fox jumps the lazy dog".toProperCase()
// returns "A Quick Brown Fox Jumps The Lazy Dog"
String.prototype.toProperCase = function () {
return this.replace(/([^\W_]+[^\s-]*) */g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
};
A built-in Array.random() function to return a random element of an Array
Array.prototype.random = function () {
return this[Math.floor(Math.random() * this.length)];
};
Handling unhandledRejection, events that fails because of missing .catch(e => {...}); to prevent bot from crashing in middle of production.
process.on("unhandledRejection", (e) => {
console.log(e);
});
Last updated