import { Client, ClientEvents, ColorResolvable, EmbedBuilder, EmbedField, GatewayIntentBits, Message } from "discord.js"; import { Config } from "../utils/Config"; import { Logger } from "../utils/Logger"; export class BotClient extends Client { public prefix = Config.prefix; public color: ColorResolvable = Config.embedColor; public constructor() { super({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildVoiceStates, ], }); } public async start() { this.login(Config.token); } /** * 이벤트 핸들러 등록 * * 지정한 이벤트가 발생했을때 해당 핸들러를 호출함 * * 'func'의 내용은 기본적으로 'client.on'을 따름 * * 'extra'를 입력할 경우 추가되어 같이 전달 * * @example * client.onEvent('ready', (client, info) => { * Logger.ready(client?.user.username, '봇이 준비되었습니다.', info) // 출력: OOO 봇이 준비되었습니다. 추가 정보 * }, ['추가 정보']); * * @param event 이벤트명 * @param func 이벤트 핸들러 함수 * @param extra 추가로 전달할 목록 */ public readonly onEvent = (event: keyof ClientEvents, func: Function, ...extra: any[]) => this.on(event, (...args) => func(...args, ...extra)); public mkembed(data: { title?: string; description?: string; url?: string; image?: string; thumbnail?: string; author?: { name: string, iconURL?: string, url?: string }; addFields?: EmbedField[]; timestamp?: number | Date | undefined | null; footer?: { text: string, iconURL?: string }; color?: ColorResolvable; }): EmbedBuilder { if (!data.color) data.color = this.color; const embed = new EmbedBuilder(); if (data.title) embed.setTitle(data.title); if (data.description) embed.setDescription(data.description); if (data.url) embed.setURL(data.url); if (data.image) embed.setImage(data.image); if (data.thumbnail) embed.setThumbnail(data.thumbnail); if (data.author) embed.setAuthor({ name: data.author.name, iconURL: data.author.iconURL, url: data.author.url }); if (data.addFields) embed.addFields(data.addFields); if (data.timestamp) embed.setTimestamp(data.timestamp); if (data.footer) embed.setFooter({ text: data.footer.text, iconURL: data.footer.iconURL }); if (data.color) embed.setColor(data.color); return embed; } public msgDelete(message: Message, time: number, customTime?: boolean): void { setTimeout(async () => { try { const msg = await message.fetch(true).catch(() => undefined); if (msg?.deletable) msg.delete().catch((err) => { Logger.warn(`[BotClient] 메세지 삭제 실패: ${String(err)}`); }); } catch {} }, Math.max(100, time * (customTime ? 1 : 6000))); } }