2023-04-20 11:47:47 +05:00
|
|
|
const moment = require("moment");
|
|
|
|
|
|
|
|
moment.relativeTimeThreshold("ss", 5);
|
|
|
|
moment.relativeTimeThreshold("s", 60);
|
|
|
|
moment.relativeTimeThreshold("m", 60);
|
|
|
|
moment.relativeTimeThreshold("h", 60);
|
|
|
|
moment.relativeTimeThreshold("d", 24);
|
|
|
|
moment.relativeTimeThreshold("M", 12);
|
2022-01-04 02:18:28 +05:00
|
|
|
|
|
|
|
module.exports = {
|
2023-02-05 12:05:35 +05:00
|
|
|
/**
|
2023-11-05 16:03:23 +05:00
|
|
|
* Calls a Callback for Each Element in Collection Asynchronously
|
|
|
|
* @param {Array} collection Collection
|
|
|
|
* @param {Function} callback Function to Perform on Collection
|
2023-02-05 12:05:35 +05:00
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2023-04-20 11:47:47 +05:00
|
|
|
async asyncForEach(collection, callback) {
|
2023-02-05 12:05:35 +05:00
|
|
|
const allPromises = collection.map(async key => {
|
|
|
|
await callback(key);
|
|
|
|
});
|
|
|
|
|
|
|
|
return await Promise.all(allPromises);
|
|
|
|
},
|
|
|
|
|
2022-12-24 18:25:41 +05:00
|
|
|
/**
|
2023-11-05 16:03:23 +05:00
|
|
|
* Sorts an Array by Key
|
|
|
|
* @param {Array} array Array to Sort
|
2022-12-24 18:25:41 +05:00
|
|
|
* @param {Number} key Key
|
2023-11-05 16:03:23 +05:00
|
|
|
* @returns {Array} Sorted Array
|
2022-12-24 18:25:41 +05:00
|
|
|
*/
|
2022-01-04 02:18:28 +05:00
|
|
|
sortByKey(array, key) {
|
|
|
|
return array.sort(function (a, b) {
|
|
|
|
const x = a[key];
|
|
|
|
const y = b[key];
|
2023-07-05 00:58:06 +05:00
|
|
|
return x < y ? 1 : x > y ? -1 : 0;
|
2022-01-04 02:18:28 +05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2022-12-24 18:25:41 +05:00
|
|
|
/**
|
2023-11-05 16:03:23 +05:00
|
|
|
* Shuffles the Array
|
|
|
|
* @param {Array} pArray Array to Shuffle
|
|
|
|
* @returns {Array} Shuffled Array
|
2022-12-24 18:25:41 +05:00
|
|
|
*/
|
2022-01-04 02:18:28 +05:00
|
|
|
shuffle(pArray) {
|
|
|
|
const array = [];
|
2023-11-05 16:03:23 +05:00
|
|
|
|
2022-01-04 02:18:28 +05:00
|
|
|
pArray.forEach(element => array.push(element));
|
2023-11-05 16:03:23 +05:00
|
|
|
|
2022-01-04 02:18:28 +05:00
|
|
|
let currentIndex = array.length,
|
2023-07-05 00:58:06 +05:00
|
|
|
temporaryValue,
|
|
|
|
randomIndex;
|
2022-01-04 02:18:28 +05:00
|
|
|
|
2022-12-15 21:02:38 +05:00
|
|
|
while (currentIndex !== 0) {
|
2022-01-04 02:18:28 +05:00
|
|
|
randomIndex = Math.floor(Math.random() * currentIndex);
|
|
|
|
currentIndex -= 1;
|
2022-12-24 18:25:41 +05:00
|
|
|
|
2022-01-04 02:18:28 +05:00
|
|
|
temporaryValue = array[currentIndex];
|
|
|
|
array[currentIndex] = array[randomIndex];
|
|
|
|
array[randomIndex] = temporaryValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
},
|
|
|
|
|
2022-12-24 18:25:41 +05:00
|
|
|
/**
|
2023-11-05 16:03:23 +05:00
|
|
|
* Returns a Random Number Between min (inclusive) And max (inclusive)
|
|
|
|
* @param {Number} min Min value (inclusive)
|
|
|
|
* @param {Number} max Max value (inclusive)
|
2022-12-24 18:25:41 +05:00
|
|
|
* @returns {Number} Integer
|
|
|
|
*/
|
2022-01-04 02:18:28 +05:00
|
|
|
randomNum(min, max) {
|
2022-12-24 18:25:41 +05:00
|
|
|
min = Math.ceil(min);
|
|
|
|
max = Math.floor(max);
|
2023-10-10 20:44:42 +05:00
|
|
|
|
2022-12-24 18:25:41 +05:00
|
|
|
return Math.floor(Math.random() * (max - min + 1) + min);
|
2022-12-15 21:02:38 +05:00
|
|
|
},
|
2023-04-20 11:47:47 +05:00
|
|
|
|
|
|
|
/**
|
2023-11-05 16:03:23 +05:00
|
|
|
* Beautifies the given Date
|
|
|
|
* @param {import("../base/Client")} client Discord Client
|
2023-04-20 11:47:47 +05:00
|
|
|
* @param {Date} date Date
|
2023-11-05 16:03:23 +05:00
|
|
|
* @param {String | null} format Format for Moment
|
2023-04-20 11:47:47 +05:00
|
|
|
* @param {String} locale Language
|
2023-11-05 16:03:23 +05:00
|
|
|
* @returns {String} Beautified Date
|
2023-04-20 11:47:47 +05:00
|
|
|
*/
|
2024-02-09 23:26:57 +05:00
|
|
|
printDate(client, date, format = null, locale = client.defaultLanguage.name) {
|
2023-05-30 15:28:41 +05:00
|
|
|
const languageData = client.languages.find(language => language.name === locale);
|
2023-04-20 11:47:47 +05:00
|
|
|
if (format === "" || format === null) format = languageData.defaultMomentFormat;
|
2023-10-10 20:44:42 +05:00
|
|
|
|
2023-07-05 00:58:06 +05:00
|
|
|
return moment(new Date(date)).locale(languageData.moment).format(format);
|
2023-04-20 11:47:47 +05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2023-11-05 16:03:23 +05:00
|
|
|
* Converts the Given Time
|
|
|
|
* @param {import("../base/Client")} client Discord Client
|
2023-04-20 11:47:47 +05:00
|
|
|
* @param {String} time Time
|
|
|
|
* @param {Boolean} type Type (To now = true or from now = false)
|
2023-11-05 16:03:23 +05:00
|
|
|
* @param {Boolean} prefix Include Prefix
|
2023-04-20 11:47:47 +05:00
|
|
|
* @param {String} locale Language
|
|
|
|
* @returns {String} Time
|
|
|
|
*/
|
2024-02-09 23:26:57 +05:00
|
|
|
convertTime(client, time, type = false, prefix = true, locale = client.defaultLanguage.name) {
|
2023-05-30 15:28:41 +05:00
|
|
|
const languageData = client.languages.find(language => language.name === locale);
|
2023-04-20 11:47:47 +05:00
|
|
|
const m = moment(time).locale(languageData.moment);
|
2023-10-10 20:44:42 +05:00
|
|
|
|
2023-11-05 16:03:23 +05:00
|
|
|
return type ? m.toNow(!prefix) : m.fromNow(!prefix);
|
2023-04-20 11:47:47 +05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2023-11-05 16:03:23 +05:00
|
|
|
* Get a Noun for Number
|
2023-04-20 11:47:47 +05:00
|
|
|
* @param {Number} number Number
|
|
|
|
* @param {String} one String for one
|
|
|
|
* @param {String} two String for two
|
|
|
|
* @param {String} five String for five
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
getNoun(number, one, two, five) {
|
|
|
|
let n = Math.abs(number);
|
|
|
|
n %= 100;
|
|
|
|
if (n >= 5 && n <= 20) return five;
|
|
|
|
n %= 10;
|
2023-07-03 14:25:25 +05:00
|
|
|
|
2023-04-20 11:47:47 +05:00
|
|
|
if (n === 1) return one;
|
|
|
|
if (n >= 2 && n <= 4) return two;
|
|
|
|
|
|
|
|
return five;
|
|
|
|
},
|
2023-07-05 00:58:06 +05:00
|
|
|
};
|