From c453b82e9fb6a2600f502015f8a73c49a67238a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Fri, 18 Apr 2025 12:12:18 +0800 Subject: [PATCH] feat: #954 --- src/onebot/action/index.ts | 2 ++ src/onebot/action/router.ts | 2 +- src/onebot/action/system/CleanCache.ts | 38 ++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/onebot/action/system/CleanCache.ts diff --git a/src/onebot/action/index.ts b/src/onebot/action/index.ts index b2d8ef9b..cbc14a2f 100644 --- a/src/onebot/action/index.ts +++ b/src/onebot/action/index.ts @@ -114,6 +114,7 @@ import { TransGroupFile } from './extends/TransGroupFile'; import { RenameGroupFile } from './extends/RenameGroupFile'; import { GetRkeyServer } from './packet/GetRkeyServer'; import { GetRkeyEx } from './packet/GetRkeyEx'; +import { CleanCache } from './system/CleanCache'; export function createActionMap(obContext: NapCatOneBot11Adapter, core: NapCatCore) { @@ -241,6 +242,7 @@ export function createActionMap(obContext: NapCatOneBot11Adapter, core: NapCatCo new ClickInlineKeyboardButton(obContext, core), new GetPrivateFileUrl(obContext, core), new GetUnidirectionalFriendList(obContext, core), + new CleanCache(obContext, core), ]; type HandlerUnion = typeof actionHandlers[number]; diff --git a/src/onebot/action/router.ts b/src/onebot/action/router.ts index d25723eb..711edef6 100644 --- a/src/onebot/action/router.ts +++ b/src/onebot/action/router.ts @@ -54,7 +54,7 @@ export const ActionName = { GetStatus: 'get_status', GetVersionInfo: 'get_version_info', // Reboot : 'set_restart', - // CleanCache : 'clean_cache', + CleanCache : 'clean_cache', Exit: 'bot_exit', // go-cqhttp SetQQProfile: 'set_qq_profile', diff --git a/src/onebot/action/system/CleanCache.ts b/src/onebot/action/system/CleanCache.ts new file mode 100644 index 00000000..d583c736 --- /dev/null +++ b/src/onebot/action/system/CleanCache.ts @@ -0,0 +1,38 @@ +import { OneBotAction } from '@/onebot/action/OneBotAction'; +import { ActionName } from '@/onebot/action/router'; +import { unlink, readdir } from 'fs/promises'; +import { join } from 'path'; + +export class CleanCache extends OneBotAction { + override actionName = ActionName.CleanCache; + + async _handle() { + try { + // 获取临时文件夹路径 + const tempPath = this.core.NapCatTempPath; + + // 读取文件夹中的所有文件 + const files = await readdir(tempPath); + + // 删除每个文件 + const deletePromises = files.map(async (file) => { + const filePath = join(tempPath, file); + try { + await unlink(filePath); + this.core.context.logger.log(`已删除文件: ${filePath}`); + } catch (err: unknown) { + this.core.context.logger.log(`删除文件 ${filePath} 失败: ${(err as Error).message}`); + + } + }); + + // 等待所有删除操作完成 + await Promise.all(deletePromises); + + this.core.context.logger.log(`临时文件夹清理完成: ${tempPath}`); + } catch (err: unknown) { + this.core.context.logger.log(`清理缓存失败: ${(err as Error).message}`); + throw err; + } + } +} \ No newline at end of file