diff --git a/src/helpers/extenders.ts b/src/helpers/extenders.ts index c44bfe82..cb1aae20 100644 --- a/src/helpers/extenders.ts +++ b/src/helpers/extenders.ts @@ -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 (context: Interaction | 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 } }); - } - return isInteraction - ? await context.reply({ + if (options.edit) { + await context.editReply({ content }); + return; + } + 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 (context: Interaction | 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 (context: Interaction | Message, key: string, args: unknown[], options: Options) => { options.prefixEmoji = "error"; return await replyTranslated(context, key, args, options); };