UNPKG

n1cat-discord-script-manager

Version:

A Discord.js plugin for dynamic script management and execution

76 lines (66 loc) 2.34 kB
const { REST, Routes } = require("discord.js"); const fs = require("fs"); const path = require("path"); require("dotenv").config(); const commands = []; const commandsPath = path.join(__dirname, "commands"); const commandFiles = fs .readdirSync(commandsPath) .filter((file) => file.endsWith(".js") && file !== "interaction.js"); for (const file of commandFiles) { const filePath = path.join(commandsPath, file); const command = require(filePath); if ("data" in command && "execute" in command) { commands.push(command.data.toJSON()); } else { console.log( `[警告] 命令 ${filePath} 缺少必要的 "data" 或 "execute" 屬性。` ); } } const rest = new REST().setToken(process.env.TOKEN); (async () => { try { // 清除全局命令 console.log("正在清除全局命令..."); await rest.put(Routes.applicationCommands(process.env.CLIENT_ID), { body: [], }); console.log("已清除全局命令"); // 清除所有伺服器的命令 if (process.env.GUILD_IDS) { const guildIds = process.env.GUILD_IDS.split(","); console.log(`正在清除 ${guildIds.length} 個伺服器的命令...`); for (const guildId of guildIds) { try { await rest.put( Routes.applicationGuildCommands(process.env.CLIENT_ID, guildId), { body: [] } ); console.log(`已清除伺服器 ${guildId} 的命令`); } catch (error) { console.error( `清除伺服器 ${guildId} 的命令時發生錯誤:`, error.message ); } } } // 重新註冊命令 // console.log(`開始重新載入 ${commands.length} 個應用程式 (/) 命令。`); // const data = await rest.put( // Routes.applicationCommands(process.env.CLIENT_ID), // { body: commands } // ); console.log(`成功重新載入 ${data.length} 個應用程式 (/) 命令。`); } catch (error) { console.error("錯誤詳情:", error); if (error.code === 50001) { console.error("錯誤:機器人沒有 'applications.commands' 權限"); } else if (error.code === 50035) { console.error("錯誤:無效的請求格式"); } else if (error.code === 50013) { console.error("錯誤:機器人缺少必要的權限"); } } })();