From 4a5f4edeb829975eaba39c8c3c9bd4871c007c05 Mon Sep 17 00:00:00 2001 From: Slincnik Date: Sat, 11 Jan 2025 13:52:56 +0300 Subject: [PATCH] refactor: refactored func to help send msg in interaction --- src/events/Ready.js | 3 + src/helpers/extenders.js | 145 ++++++++++++++++----------------------- 2 files changed, 63 insertions(+), 85 deletions(-) diff --git a/src/events/Ready.js b/src/events/Ready.js index a757011a..db26db4f 100644 --- a/src/events/Ready.js +++ b/src/events/Ready.js @@ -15,6 +15,9 @@ export const data = { export async function run(client) { logger.ready(client.user.tag + " is online!"); + // Fetching all app emojis, because we need to use them + await client.application.emojis.fetch(); + const taskPath = resolve(client.configService.get("paths.tasks")); const cronTasks = await loadCronTasks(taskPath); diff --git a/src/helpers/extenders.js b/src/helpers/extenders.js index 33cc2c01..32460c0e 100644 --- a/src/helpers/extenders.js +++ b/src/helpers/extenders.js @@ -1,105 +1,80 @@ -import { Message, BaseInteraction, User, GuildMember } from "discord.js"; +import { BaseInteraction } from "discord.js"; +import useClient from "../utils/use-client.js"; + +export const getLocale = guildId => { + const client = useClient(); + const guild = client.getGuildData(guildId); + return guild.language; +}; + +const getAppEmojis = () => { + const client = useClient(); + + return client.application.emojis.cache; +}; /** * - * @param {Message|BaseInteraction} context - * @returns {string} Guild's language - */ -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 {import('../structures/client.js').ExtendedClient} client * @param {string} prefixEmoji * @param {string} message - * @returns {string} Formatted message */ -function formatReply(client, prefixEmoji, message) { - return prefixEmoji ? `${client.customEmojis[prefixEmoji]} | ${message}` : message; -} +const formatReply = (prefixEmoji, message) => { + const emojis = getAppEmojis(); + return prefixEmoji ? `${emojis.find(e => e.name === prefixEmoji).toString()} ${message}` : `${message}`; +}; /** * - * @param {Message|BaseInteraction} context - * @param {string} key - * @param {any[]} args - * @param {string} options + * @param {import("discord.js").User} user * @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); +export const getUsername = user => (user.discriminator === "0" ? user.username : user.tag); - 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 } }); -} +/** + * + * @param {import("discord.js").Message | import("discord.js").BaseInteraction} context + * @param {string} key + * @param {unknown[]} args + * @param {*} options + */ +export const replyTranslated = async (context, key, args, options = {}) => { + const client = useClient(); + const locale = options.locale || client.configService.get("defaultLang"); + const translated = client.translate(key, { + lng: locale, + ...args, + }); -User.prototype.getUsername = function () { - return this.discriminator === "0" ? this.username : this.tag; + const content = formatReply(options.prefixEmoji, translated); + + const isInteraction = context instanceof BaseInteraction; + + if (options.edit) { + return isInteraction + ? await context.editReply({ + content, + ephemeral: options.ephemeral || false, + }) + : await context.edit({ content, allowedMentions: { repliedUser: options.mention || false } }); + } + return isInteraction + ? await context.reply({ + content, + ephemeral: options.ephemeral || false, + }) + : await context.reply({ + content, + allowedMentions: { repliedUser: options.mention || false }, + }); }; -GuildMember.prototype.getUsername = function () { - return this.user.getUsername(); -}; - -BaseInteraction.prototype.getLocale = function () { - return getLocale(this); -}; - -BaseInteraction.prototype.translate = function (key, args, locale) { - return translate(this.client, key, args, locale || this.getLocale()); -}; - -BaseInteraction.prototype.replyT = async function (key, args, options = {}) { - return await replyTranslated(this, key, args, options); -}; - -BaseInteraction.prototype.success = async function (key, args, options = {}) { +export const replySuccess = async (context, key, args, options) => { options.prefixEmoji = "success"; - return await this.replyT(key, args, options); + return await replyTranslated(context, key, args, options); }; -BaseInteraction.prototype.error = async function (key, args, options = {}) { +export const replyError = async (context, key, args, options) => { options.prefixEmoji = "error"; - return await this.replyT(key, args, options); -}; - -Message.prototype.getLocale = function () { - return getLocale(this); -}; - -Message.prototype.translate = function (key, args, locale) { - return translate(this.client, key, args, locale || this.getLocale()); -}; - -Message.prototype.replyT = async function (key, args, options = {}) { - return await replyTranslated(this, key, args, options); -}; - -Message.prototype.success = async function (key, args, options = {}) { - options.prefixEmoji = "success"; - return await this.replyT(key, args, options); -}; - -Message.prototype.error = async function (key, args, options = {}) { - options.prefixEmoji = "error"; - return await this.replyT(key, args, options); + return await replyTranslated(context, key, args, options); };