fix: types and func

This commit is contained in:
Slincnik 2025-01-15 00:01:24 +03:00
parent 01b12023a1
commit ca17bcaf44
No known key found for this signature in database

View file

@ -1,6 +1,13 @@
import { BaseInteraction, User } from "discord.js";
import { BaseInteraction, CacheType, Interaction, InteractionReplyOptions, Message, User } from "discord.js";
import useClient from "@/utils/use-client.js";
interface Options extends InteractionReplyOptions {
prefixEmoji: string;
locale?: string;
edit?: boolean;
mention?: boolean;
}
export const getLocale = async (guildId: string) => {
const client = useClient();
const guild = await client.getGuildData(guildId);
@ -10,18 +17,18 @@ export const getLocale = async (guildId: string) => {
const getAppEmojis = () => {
const client = useClient();
return client.application?.emojis.cache;
return client.application.emojis.cache;
};
const formatReply = (prefixEmoji: string, message: string) => {
const emojis = getAppEmojis()!;
return prefixEmoji ? `${emojis.find(e => e.name === prefixEmoji).toString()} ${message}` : `${message}`;
const emojis = getAppEmojis();
const emoji = emojis.find(emoji => emoji.name === prefixEmoji);
return prefixEmoji ? `${emoji?.toString()} ${message}` : `${message}`;
};
export const getUsername = (user: User) => (user.discriminator === "0" ? user.username : user.tag);
export const replyTranslated = async (context, key, args, options = {}) => {
export const replyTranslated = async <T extends CacheType = CacheType>(context: Interaction<T> | Message, key: string, args: unknown[], options: Options) => {
const client = useClient();
const locale = options.locale || client.configService.get("defaultLang");
const translated = client.translate(key, {
@ -31,33 +38,39 @@ export const replyTranslated = async (context, key, args, options = {}) => {
const content = formatReply(options.prefixEmoji, translated);
const isInteraction = context instanceof BaseInteraction;
if (context instanceof BaseInteraction) {
if (!context.isRepliable()) return;
if (options.edit) {
return isInteraction
? await context.editReply({
content,
ephemeral: options.ephemeral || false,
})
: await context.edit({ content, allowedMentions: { repliedUser: options.mention || false } });
await context.editReply({ content });
return;
}
return isInteraction
? await context.reply({
await context.reply({
content,
ephemeral: options.ephemeral || false,
})
: await context.reply({
});
return;
}
if (options.edit) {
await context.edit({
content,
allowedMentions: { repliedUser: options.mention || false },
});
return;
}
await context.reply({
content,
allowedMentions: { repliedUser: options.mention || false },
});
};
export const replySuccess = async (context, key, args, options) => {
export const replySuccess = async <T extends CacheType = CacheType>(context: Interaction<T> | Message, key: string, args: unknown[], options: Options) => {
options.prefixEmoji = "success";
return await replyTranslated(context, key, args, options);
};
export const replyError = async (context, key, args, options) => {
export const replyError = async <T extends CacheType = CacheType>(context: Interaction<T> | Message, key: string, args: unknown[], options: Options) => {
options.prefixEmoji = "error";
return await replyTranslated(context, key, args, options);
};