mirror of
https://github.com/JonnyBro/JaBa.git
synced 2025-01-31 22:54:10 +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";
|
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) => {
|
export const getLocale = async (guildId: string) => {
|
||||||
const client = useClient();
|
const client = useClient();
|
||||||
const guild = await client.getGuildData(guildId);
|
const guild = await client.getGuildData(guildId);
|
||||||
|
@ -10,18 +17,18 @@ export const getLocale = async (guildId: string) => {
|
||||||
const getAppEmojis = () => {
|
const getAppEmojis = () => {
|
||||||
const client = useClient();
|
const client = useClient();
|
||||||
|
|
||||||
return client.application?.emojis.cache;
|
return client.application.emojis.cache;
|
||||||
};
|
};
|
||||||
|
|
||||||
const formatReply = (prefixEmoji: string, message: string) => {
|
const formatReply = (prefixEmoji: string, message: string) => {
|
||||||
const emojis = getAppEmojis()!;
|
const emojis = getAppEmojis();
|
||||||
return prefixEmoji ? `${emojis.find(e => e.name === prefixEmoji).toString()} ${message}` : `${message}`;
|
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 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 client = useClient();
|
||||||
const locale = options.locale || client.configService.get("defaultLang");
|
const locale = options.locale || client.configService.get("defaultLang");
|
||||||
const translated = client.translate(key, {
|
const translated = client.translate(key, {
|
||||||
|
@ -31,33 +38,39 @@ export const replyTranslated = async (context, key, args, options = {}) => {
|
||||||
|
|
||||||
const content = formatReply(options.prefixEmoji, translated);
|
const content = formatReply(options.prefixEmoji, translated);
|
||||||
|
|
||||||
const isInteraction = context instanceof BaseInteraction;
|
if (context instanceof BaseInteraction) {
|
||||||
|
if (!context.isRepliable()) return;
|
||||||
|
|
||||||
if (options.edit) {
|
if (options.edit) {
|
||||||
return isInteraction
|
await context.editReply({ content });
|
||||||
? await context.editReply({
|
return;
|
||||||
content,
|
}
|
||||||
ephemeral: options.ephemeral || false,
|
await context.reply({
|
||||||
})
|
|
||||||
: await context.edit({ content, allowedMentions: { repliedUser: options.mention || false } });
|
|
||||||
}
|
|
||||||
return isInteraction
|
|
||||||
? await context.reply({
|
|
||||||
content,
|
content,
|
||||||
ephemeral: options.ephemeral || false,
|
ephemeral: options.ephemeral || false,
|
||||||
})
|
});
|
||||||
: await context.reply({
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.edit) {
|
||||||
|
await context.edit({
|
||||||
content,
|
content,
|
||||||
allowedMentions: { repliedUser: options.mention || false },
|
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";
|
options.prefixEmoji = "success";
|
||||||
return await replyTranslated(context, key, args, options);
|
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";
|
options.prefixEmoji = "error";
|
||||||
return await replyTranslated(context, key, args, options);
|
return await replyTranslated(context, key, args, options);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue