JaBa/helpers/functions.js

129 lines
3.5 KiB
JavaScript
Raw Normal View History

const { Permissions } = require("discord.js");
2022-01-04 02:18:28 +05:00
const languages = require("../languages/language-meta.json").map((l) => l.moment).filter((l) => l !== "en");
languages.forEach((lang) => {
require(`moment/locale/${lang}.js`);
});
module.exports = {
/**
* Gets message prefix
* @param {object} message The Discord message
2022-01-24 18:58:48 +05:00
* @param {object} data Server data
2022-01-04 02:18:28 +05:00
* @returns The prefix
*/
getPrefix(message, data) {
if (message.channel.type !== "DM") {
2022-01-04 02:18:28 +05:00
const prefixes = [
`<@!${message.client.user.id}> `,
`<@${message.client.user.id}> `,
`<@!${message.client.user.id}>`,
`<@${message.client.user.id}>`,
message.client.user.username.toLowerCase(),
data.guild.prefix
];
let prefix = null;
prefixes.forEach((p) => {
if (message.content.startsWith(p) || message.content.toLowerCase().startsWith(p)) prefix = p;
});
return prefix;
} else {
return true;
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
},
// This function return an actual link to the support server
2022-01-04 02:18:28 +05:00
async supportLink(client) {
const guild = client.guilds.cache.get(client.config.support.id);
const member = guild.me;
const channel = guild.channels.cache.find((ch) => ch.permissionsFor(member.id).has(Permissions.FLAGS.CREATE_INSTANT_INVITE) && ch.type === "GUILD_TEXT" || ch.type === "GUILD_VOICE");
2022-01-04 02:18:28 +05:00
if (channel) {
const invite = await channel.createInvite({
maxAge: 0
}).catch(() => {});
return invite ? invite.url : null;
} else {
return "";
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
},
// This function sort an array
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));
});
},
// This function return a shuffled array
shuffle(pArray) {
const array = [];
pArray.forEach(element => array.push(element));
let currentIndex = array.length,
temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
},
// This function return a random number between min and max
randomNum(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
},
convertTime(guild, time) {
const absoluteSeconds = Math.floor((time / 1000) % 60);
const absoluteMinutes = Math.floor((time / (1000 * 60)) % 60);
const absoluteHours = Math.floor((time / (1000 * 60 * 60)) % 24);
const absoluteDays = Math.floor(time / (1000 * 60 * 60 * 24));
const d = absoluteDays ?
absoluteDays === 1 ?
2022-01-13 00:26:23 +05:00
guild.translate("time:ONE_DAY") :
guild.translate("time:DAYS", {
amount: absoluteDays
}) :
2022-01-04 02:18:28 +05:00
null;
const h = absoluteHours ?
absoluteHours === 1 ?
2022-01-13 00:26:23 +05:00
guild.translate("time:ONE_HOUR") :
guild.translate("time:HOURS", {
amount: absoluteHours
}) :
2022-01-04 02:18:28 +05:00
null;
const m = absoluteMinutes ?
absoluteMinutes === 1 ?
2022-01-13 00:26:23 +05:00
guild.translate("time:ONE_MINUTE") :
guild.translate("time:MINUTES", {
amount: absoluteMinutes
}) :
2022-01-04 02:18:28 +05:00
null;
const s = absoluteSeconds ?
absoluteSeconds === 1 ?
2022-01-13 00:26:23 +05:00
guild.translate("time:ONE_SECOND") :
guild.translate("time:SECONDS", {
amount: absoluteSeconds
}) :
2022-01-04 02:18:28 +05:00
null;
const absoluteTime = [];
if (d) absoluteTime.push(d);
if (h) absoluteTime.push(h);
if (m) absoluteTime.push(m);
if (s) absoluteTime.push(s);
return absoluteTime.join(", ");
}
};