From b12b3c2e2e698bb9c64e311eb6031de719e114b9 Mon Sep 17 00:00:00 2001 From: kimpure Date: Sun, 15 Feb 2026 11:49:21 +0000 Subject: [PATCH] feat: Add command cache(auto deleate command) --- packages/bot/command.ts | 6 ++--- packages/bot/index.ts | 55 +++++++++++++++++++++++++++++------------ packages/index.ts | 1 - 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/packages/bot/command.ts b/packages/bot/command.ts index dbecee8..7f149fc 100644 --- a/packages/bot/command.ts +++ b/packages/bot/command.ts @@ -34,8 +34,8 @@ export function defineCommand( } } -export const commandDirectory = join(__dirname, "commands"); -export const commandMap = requireDirectorySync(commandDirectory); +const commandDirectory = join(__dirname, "commands"); +export const defineCommands = requireDirectorySync(commandDirectory); export const commandExecuteNameHashMap: { [key: string]: DiscordCommandExecute -} = Object.fromEntries(commandMap.map(command => [command.data.name, command.execute])); +} = Object.fromEntries(defineCommands.map(command => [command.data.name, command.execute])); diff --git a/packages/bot/index.ts b/packages/bot/index.ts index aec2d04..6dde7ae 100644 --- a/packages/bot/index.ts +++ b/packages/bot/index.ts @@ -1,7 +1,11 @@ import { Client, Events, GatewayIntentBits, REST, Routes } from "discord.js"; -import { commandMap, DiscordCommand } from "./command"; +import { commandExecuteNameHashMap, defineCommands, DiscordCommand } from "./command"; import { eventMap } from "./event"; import { OutputHandler } from "../utils/outputHandler"; +import { APPLICATION_ID } from "../env"; +import { mkdir, readFile, writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { cwd } from "node:process"; export class DiscordBot { rest: REST; @@ -19,25 +23,44 @@ export class DiscordBot { this.rest = new REST({ version: "10" }).setToken(token); } - private async putCommands(commands: DiscordCommand[]): Promise { - if (!this.client.isReady()) - throw new Error("Client is not ready"); - - await this.rest.put( - Routes.applicationCommands(this.client.application.id), - { - body: commands.map((command) => command.data.toJSON()), - } - ); - } - public async registerCommands(): Promise { try { if (!this.client.isReady()) { - await this.client.once(Events.ClientReady, () => this.putCommands(commandMap)); - } else { - await this.putCommands(commandMap); + await this.client.once(Events.ClientReady, () => {}); } + + const commandsCachePath = join(cwd(), "cache", "commands"); + await mkdir(commandsCachePath); + const commandsCache: { [key: string]: string } = JSON.parse(await readFile(join(commandsCachePath, "list.json"), "utf-8") || "{}"); + + for (const [command, id] of Object.entries(commandsCache)) { + if (commandExecuteNameHashMap[command]) { + continue; + } + await this.deleteCommand(id); + } + + await this.rest.put( + Routes.applicationCommands(APPLICATION_ID), + { + body: defineCommands.map((command) => command.data.toJSON()), + } + ); + + await writeFile( + join(commandsCachePath, "list.json"), + JSON.stringify( + ( + (await this.rest.get(Routes.applicationCommands(APPLICATION_ID))) as { + name: string, + id: string + }[] + ).reduce>((acc, cur) => { + acc[cur.name] = cur.id; + return acc; + }, {}) + ) + ); } catch(err) { OutputHandler.errorLog("[Command Register Error]", err); } diff --git a/packages/index.ts b/packages/index.ts index d10276c..87f2988 100644 --- a/packages/index.ts +++ b/packages/index.ts @@ -1,7 +1,6 @@ import { Routes } from "discord.js"; import { DiscordBot } from "./bot"; import { APPLICATION_ID, DISCORD_TOKEN } from "./env"; -import remove from "./cli/remove_command"; export const bot = new DiscordBot(DISCORD_TOKEN);