UNPKG

n1cat-discord-script-manager

Version:

A Discord.js plugin for dynamic script management and execution

70 lines (61 loc) 1.99 kB
const fs = require("fs"); const path = require("path"); const Logger = require("../utils/logger"); // 用於追蹤函式調用的輔助函數 function getCallerInfo() { const stack = new Error().stack; const callerLine = stack.split("\n")[3]; // 跳過 Error 和 getCallerInfo 的堆疊 const match = callerLine.match(/at\s+(.+?)\s+\((.+?):(\d+):(\d+)\)/); if (match) { const [, functionName, file, line, column] = match; return { function: functionName, file: file.split("/").pop(), // 只取檔案名稱 line, column, }; } return { function: "unknown", file: "unknown", line: "unknown", column: "unknown", }; } // 日誌輸出函數 function log(message, isError = false) { const caller = getCallerInfo(); const debugMode = this.options?.debug ?? false; // 如果是錯誤或 debug 模式開啟,則輸出詳細日誌 if (isError || debugMode) { console.log(`[${caller.file}:${caller.line}] ${message}`); } } async function loadCommands(client, commandsPath) { const logger = Logger.createContextLogger({ module: "CommandHandler", debug: client.config?.debug || false, }); try { logger.log(`Loading slash commands from folder: ${commandsPath}`); const commandFiles = fs .readdirSync(commandsPath) .filter((file) => file.endsWith(".js")); for (const file of commandFiles) { try { const command = require(path.join(commandsPath, file)); if ("data" in command && "execute" in command) { client.commands.set(command.data.name, command); logger.log(`Loaded command: ${command.data.name}`); } else { logger.error(`Command ${file} is missing required properties`); } } catch (error) { logger.error(`Error loading command ${file}: ${error.message}`); } } } catch (error) { logger.error(`Error loading commands: ${error.message}`); } } module.exports = { loadCommands };