UNPKG

n1cat-discord-script-manager

Version:

A Discord.js plugin for dynamic script management and execution

151 lines (135 loc) 4.7 kB
const { SlashCommandBuilder, PermissionFlagsBits } = require("discord.js"); 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 scriptManager = global.client?.scriptManager; const debugMode = scriptManager?.options?.debug ?? false; // 如果是錯誤或 debug 模式開啟,則輸出詳細日誌 if (isError || debugMode) { console.log(`[${caller.file}:${caller.line}] ${message}`); } } module.exports = { data: new SlashCommandBuilder() .setName("listscripts") .setDescription("列出所有已載入的腳本") .setDefaultMemberPermissions(PermissionFlagsBits.Administrator), async execute(interaction) { const logger = Logger.createContextLogger({ module: "ListScripts", debug: interaction.client.config?.debug || false, }); logger.log("開始執行 listscripts 命令"); try { // 檢查互動是否已經被回應 if (interaction.replied || interaction.deferred) { logger.log("互動已被回應,跳過處理"); return; } // 使用 reply 代替 deferReply await interaction.reply({ content: "正在載入腳本列表...", ephemeral: true, }); const scriptManager = interaction.client.scriptManager; if (!scriptManager) { logger.error("找不到腳本管理器", { clientExists: !!interaction.client, clientProperties: Object.keys(interaction.client), }); await interaction.editReply({ content: "❌ 找不到腳本管理器。請檢查 ScriptManager 是否正確初始化。", }); return; } const scripts = Array.from(scriptManager.scripts.entries()); logger.log(`找到腳本數量: ${scripts.length}`); if (scripts.length === 0) { logger.log("沒有找到任何腳本"); await interaction.editReply({ content: "📝 目前沒有載入任何腳本", }); return; } const scriptList = scripts .map( ([name, script]) => `📜 ${name} (${script.enabled === false ? "已停用" : "啟用中"})` ) .join("\n"); logger.log("準備發送腳本列表"); await interaction.editReply({ content: `📝 已載入的腳本:\n${scriptList}`, }); logger.log("腳本列表已發送"); } catch (error) { logger.error(`列出腳本時出錯: ${error.message}`, { showStack: true, clientExists: !!interaction.client, clientProperties: interaction.client ? Object.keys(interaction.client) : "N/A", scriptManagerExists: !!interaction.client?.scriptManager, interactionState: { replied: interaction.replied, deferred: interaction.deferred, }, }); console.error("詳細錯誤信息:", { error: error.message, code: error.code, interactionId: interaction.id, timestamp: new Date().toISOString(), }); // 只在還沒有回應時才發送錯誤消息 try { if (!interaction.replied && !interaction.deferred) { await interaction.reply({ content: `❌ 執行命令時發生錯誤:${error.message}`, ephemeral: true, }); } else if (interaction.replied || interaction.deferred) { await interaction.editReply({ content: `❌ 執行命令時發生錯誤:${error.message}`, }); } } catch (replyError) { logger.error(`發送錯誤回應時出錯: ${replyError.message}`, { showStack: true, }); console.error("發送錯誤回應時的詳細錯誤:", { error: replyError.message, code: replyError.code, interactionId: interaction.id, interactionState: { replied: interaction.replied, deferred: interaction.deferred, }, timestamp: new Date().toISOString(), }); } } }, };