|
| 1 | + |
| 2 | +const { Serverid, TOKEN } = require("../info.js"); |
| 3 | + |
| 4 | +const { Discord, Client, Intents, Collection } = require("discord.js"); |
| 5 | + |
| 6 | +const fs = require("fs"); |
| 7 | + |
| 8 | +module.exports = async function loadCommands(client) { |
| 9 | + const { REST } = require("@discordjs/rest"); |
| 10 | + const { Routes } = require("discord-api-types/v9"); |
| 11 | + const rest = new REST({ version: "9" }).setToken(TOKEN); |
| 12 | + |
| 13 | + const ascii = require("ascii-table"); |
| 14 | + const table = new ascii("CommandHandler.js"); |
| 15 | + |
| 16 | + table.setHeading("Command", "status", "Area"); |
| 17 | + |
| 18 | + const Commands = []; |
| 19 | + const commands = []; |
| 20 | + let cmdnum = 0; |
| 21 | + |
| 22 | + client.commands = new Collection(); |
| 23 | + |
| 24 | + try { |
| 25 | + const commandFiles = fs |
| 26 | + .readdirSync("./commands") |
| 27 | + .map((folder) => |
| 28 | + fs |
| 29 | + .readdirSync(`./commands/${folder}`) |
| 30 | + .filter((file) => file.endsWith(".js")) |
| 31 | + .map((file) => `../commands/${folder}/${file}`) |
| 32 | + ) |
| 33 | + .flat(); |
| 34 | + |
| 35 | + if (!commandFiles == "") { |
| 36 | + for (const file of commandFiles) { |
| 37 | + const command = require(`${file}`); |
| 38 | + if (Object.keys(command).length === 0) continue; |
| 39 | + commands.push(command.data.toJSON()); |
| 40 | + client.commands.set(command.data.name, command); |
| 41 | + |
| 42 | + await rest.put( |
| 43 | + Routes.applicationGuildCommands(client.user.id, Serverid), |
| 44 | + { |
| 45 | + body: commands, |
| 46 | + } |
| 47 | + ); |
| 48 | + if (command.global == true) { |
| 49 | + await rest.put(Routes.applicationGuildCommands(client.user.id), { |
| 50 | + body: commands, |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + if (command.data.name) { |
| 55 | + ++cmdnum; |
| 56 | + table.addRow( |
| 57 | + command.data.name, |
| 58 | + `[ ✔ ]`, |
| 59 | + `[${command.global ? "global" : "guild"}]` |
| 60 | + ); |
| 61 | + } else { |
| 62 | + table.addRow(file, `[ ✖ ]`, `[Error]`); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + if (commandFiles == "") { |
| 67 | + table.addRow("No Cmds", `[ ✖ ]`, `[Error]`); |
| 68 | + } |
| 69 | + |
| 70 | + if (!commandFiles == "") { |
| 71 | + let msg1 = "\n" + table.toString(); |
| 72 | + let msg2 = |
| 73 | + "\n「 Commands 」|「 " + |
| 74 | + cmdnum + |
| 75 | + "/" + |
| 76 | + client.commands.size + |
| 77 | + " 」"; |
| 78 | + console.log(msg1 + msg2); |
| 79 | + } |
| 80 | + } catch (error) { |
| 81 | + console.error(error.message); |
| 82 | + } |
| 83 | +}; |
0 commit comments