mirror of
https://github.com/JonnyBro/JaBa.git
synced 2025-01-19 17:03:47 +05:00
fix: types and func
This commit is contained in:
parent
01b12023a1
commit
ca17bcaf44
1 changed files with 34 additions and 21 deletions
|
@ -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);
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue