JaBa/helpers/extenders.js

106 lines
3.2 KiB
JavaScript
Raw Normal View History

2024-04-30 11:40:52 +05:00
const { Message, BaseInteraction, User, GuildMember } = require("discord.js");
2024-11-18 19:19:24 +05:00
/**
*
* @param {Message|BaseInteraction} context
2024-11-20 12:11:29 +05:00
* @returns {string} Guild's language
2024-11-18 19:19:24 +05:00
*/
function getLocale(context) {
return context.data?.guild?.language;
}
/**
*
* @param {import("../base/Client")} client
* @param {string} key
* @param {any[]} args
* @param {string} locale
* @returns {string} Localized string
*/
function translate(client, key, args, locale) {
const language = client.translations.get(locale || "en-US");
if (!language) throw "Can't find given localization.";
return language(key, args);
}
/**
*
* @param {import("../base/Client")} client
* @param {string} prefixEmoji
* @param {string} message
2024-11-20 12:11:29 +05:00
* @returns {string} Formatted message
2024-11-18 19:19:24 +05:00
*/
function formatReply(client, prefixEmoji, message) {
return prefixEmoji ? `${client.customEmojis[prefixEmoji]} | ${message}` : message;
}
/**
*
* @param {Message|BaseInteraction} context
* @param {string} key
* @param {any[]} args
* @param {string} options
* @returns
*/
async function replyTranslated(context, key, args, options = {}) {
const locale = options.locale || getLocale(context) || "en-US";
const translated = translate(context.client, key, args, locale);
const content = formatReply(context.client, options.prefixEmoji, translated);
if (options.edit) return context.editReply ? await context.editReply({ content, ephemeral: options.ephemeral || false }) : await context.edit({ content, allowedMentions: { repliedUser: options.mention || false } });
else return context.editReply ? await context.reply({ content, ephemeral: options.ephemeral || false }) : await context.reply({ content, allowedMentions: { repliedUser: options.mention || false } });
}
User.prototype.getUsername = function () {
return this.discriminator === "0" ? this.username : this.tag;
};
2022-01-04 02:18:28 +05:00
2024-04-30 11:40:52 +05:00
GuildMember.prototype.getUsername = function () {
2024-11-18 19:19:24 +05:00
return this.user.getUsername();
2024-04-30 11:40:52 +05:00
};
2023-12-14 18:58:19 +05:00
BaseInteraction.prototype.getLocale = function () {
2024-11-18 19:19:24 +05:00
return getLocale(this);
2023-12-14 18:58:19 +05:00
};
2024-04-30 11:40:52 +05:00
BaseInteraction.prototype.translate = function (key, args, locale) {
2024-11-18 19:19:24 +05:00
return translate(this.client, key, args, locale || this.getLocale());
2022-01-04 02:18:28 +05:00
};
2023-11-05 16:03:23 +05:00
BaseInteraction.prototype.replyT = async function (key, args, options = {}) {
2024-11-18 19:19:24 +05:00
return await replyTranslated(this, key, args, options);
2022-01-04 02:18:28 +05:00
};
2023-11-05 16:03:23 +05:00
BaseInteraction.prototype.success = async function (key, args, options = {}) {
options.prefixEmoji = "success";
2024-11-18 19:19:24 +05:00
return await this.replyT(key, args, options);
2022-01-04 02:18:28 +05:00
};
2023-11-05 16:03:23 +05:00
BaseInteraction.prototype.error = async function (key, args, options = {}) {
options.prefixEmoji = "error";
2024-11-18 19:19:24 +05:00
return await this.replyT(key, args, options);
2022-01-04 02:18:28 +05:00
};
2024-02-09 23:26:57 +05:00
Message.prototype.getLocale = function () {
2024-11-18 19:19:24 +05:00
return getLocale(this);
2024-02-09 23:26:57 +05:00
};
2024-04-30 11:40:52 +05:00
Message.prototype.translate = function (key, args, locale) {
2024-11-18 19:19:24 +05:00
return translate(this.client, key, args, locale || this.getLocale());
2022-01-04 02:18:28 +05:00
};
2023-11-05 16:03:23 +05:00
Message.prototype.replyT = async function (key, args, options = {}) {
2024-11-18 19:19:24 +05:00
return await replyTranslated(this, key, args, options);
2022-01-04 02:18:28 +05:00
};
2023-11-05 16:03:23 +05:00
Message.prototype.success = async function (key, args, options = {}) {
options.prefixEmoji = "success";
2024-11-18 19:19:24 +05:00
return await this.replyT(key, args, options);
2022-01-04 02:18:28 +05:00
};
2023-11-05 16:03:23 +05:00
Message.prototype.error = async function (key, args, options = {}) {
options.prefixEmoji = "error";
2024-11-18 19:19:24 +05:00
return await this.replyT(key, args, options);
2023-07-05 00:58:06 +05:00
};