refactor: refactored func to help send msg in interaction

This commit is contained in:
Slincnik 2025-01-11 13:52:56 +03:00
parent 31e75bb08b
commit 4a5f4edeb8
No known key found for this signature in database
2 changed files with 63 additions and 85 deletions

View file

@ -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);

View file

@ -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);
};