JaBa/helpers/functions.js

149 lines
4.1 KiB
JavaScript
Raw Normal View History

2022-12-24 18:25:41 +05:00
const { PermissionsBitField, ChannelType } = require("discord.js");
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 = {
2022-12-24 18:25:41 +05:00
/**
* Create invite link to guild
* @param {import("../base/JaBa")} client Discord client
* @param {String} guildId Guild's ID
* @returns {String} Invite Link
*/
async createInvite(client, guildId) {
const guild = client.guilds.cache.get(guildId);
const member = guild.members.me;
2022-08-08 18:19:56 +05:00
const channel = guild.channels.cache.find(ch => ch.permissionsFor(member.id).has(PermissionsBitField.Flags.CreateInstantInvite) && ch.type === ChannelType.GuildText || ch.type === "GUILD_VOICE");
2022-12-24 18:25:41 +05:00
if (channel) return (await channel.createInvite()).url || "No channels found or missing permissions";
2022-01-04 02:18:28 +05:00
},
/**
* Calls a callback for each element in collection async
* @param {Array} collection
* @param {Function} callback
* @returns {Promise}
*/
async asyncForEach(collection, callback) {
const allPromises = collection.map(async key => {
await callback(key);
});
return await Promise.all(allPromises);
},
2022-12-24 18:25:41 +05:00
/**
2023-07-02 21:02:55 +05:00
* Sorts array by key
2022-12-24 18:25:41 +05:00
* @param {Array} array Array to sort
* @param {Number} key Key
* @returns {Array} Sorted array
*/
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];
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
});
},
2022-12-24 18:25:41 +05:00
/**
2023-07-02 21:02:55 +05:00
* Shuffles the array
2022-12-24 18:25:41 +05:00
* @param {*} pArray Array to shuffle
* @returns {Array} Shuffled array
*/
2022-01-04 02:18:28 +05:00
shuffle(pArray) {
const array = [];
pArray.forEach(element => array.push(element));
let currentIndex = array.length,
temporaryValue, randomIndex;
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
/**
* Returns a random integer between min (inclusive) and max (inclusive)
* @param {Number} min Min
* @param {Number} max Max
* @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);
return Math.floor(Math.random() * (max - min + 1) + min);
},
/**
* Beautify date
2023-05-30 15:28:41 +05:00
* @param {import("../base/JaBa")} client Discord client
* @param {Date} date Date
* @param {String | null} format Format for moment
* @param {String} locale Language
* @returns {String} Beautified date
*/
2023-05-30 15:28:41 +05:00
printDate(client, date, format = "", locale = client.defaultLanguage) {
const languageData = client.languages.find(language => language.name === locale);
if (format === "" || format === null) format = languageData.defaultMomentFormat;
return moment(new Date(date))
.locale(languageData.moment)
.format(format);
},
/**
2023-07-02 21:02:55 +05:00
* Converts given time
2023-05-30 15:28:41 +05:00
* @param {import("../base/JaBa")} client Discord client
* @param {String} time Time
* @param {Boolean} type Type (To now = true or from now = false)
* @param {Boolean} noPrefix Use prefix?
* @param {String} locale Language
* @returns {String} Time
*/
2023-05-30 15:28:41 +05:00
convertTime(client, time, type = false, noPrefix = false, locale = this.defaultLanguage) {
const languageData = client.languages.find(language => language.name === locale);
const m = moment(time).locale(languageData.moment);
return (type ? m.toNow(noPrefix) : m.fromNow(noPrefix));
},
/**
2023-07-02 21:02:55 +05:00
* Get a noun for number
* @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;
if (n === 1) return one;
if (n >= 2 && n <= 4) return two;
return five;
},
2023-07-02 21:02:55 +05:00
/**
* Returns just a username if user has a new username, else returns user's tag
* @param {import("discord.js").User} user Number
* @returns
*/
getUsername(user) {
return user.discriminator === "0" ? user.username : user.tag;
},
2022-01-04 02:18:28 +05:00
};