diff --git a/functions/handelCommands.js b/functions/handelCommands.js index ff7147f..49974e0 100644 --- a/functions/handelCommands.js +++ b/functions/handelCommands.js @@ -29,17 +29,13 @@ module.exports = (client) => { (async () => { try { - console.log(`[${client.shard.ids}] Started refreshing application (/) commands.`); - - const data = await rest.put( - Routes.applicationCommands(process.env.CLIENT_ID), { - body: client.commandArray - }, - ); + const data = await rest.get(Routes.applicationCommands(process.env.CLIENT_ID), { + query: new URLSearchParams({ + with_localizations: true + }) + }); client.commandsData = data; - - console.log(`[${client.shard.ids}] Successfully reloaded ${data.length} application (/) commands.`); } catch (error) { console.error(error); } diff --git a/index.js b/index.js index ef9cff1..874c35b 100644 --- a/index.js +++ b/index.js @@ -3,11 +3,15 @@ const { REST } = require("@discordjs/rest"); const { Routes } = require("discord-api-types/v10"); const express = require("express"); const cors = require("cors"); +const handleCommands = require("./utils/commands"); const package = require("./package.json"); require("dotenv").config(); +// Register Bot Commands +handleCommands(); + // Sharding Manager const manager = new ShardingManager("bot.js", { token: process.env.TOKEN, diff --git a/utils/commands.js b/utils/commands.js new file mode 100644 index 0000000..e402e90 --- /dev/null +++ b/utils/commands.js @@ -0,0 +1,45 @@ +const { Collection, REST } = require("discord.js"); +const { Routes } = require("discord-api-types/v10"); +const fs = require("fs"); + +module.exports = async function () { + const commandFolders = fs.readdirSync("./commands"); + const commands = new Collection(); + const commandArray = []; + const commandNames = new Set(); + for (const folder of commandFolders) { + const commandFiles = fs.readdirSync(`./commands/${folder}`).filter(file => file.endsWith(".js")); + for (const file of commandFiles) { + const command = require(`../commands/${folder}/${file}`); + const commandName = command.data.name; + + if (commandNames.has(commandName)) { + console.error(`Duplicate command name found: ${commandName}`); + continue; + } + + commandNames.add(commandName); + commands.set(commandName, command); + commandArray.push(command.data.toJSON()); + } + } + + const rest = new REST({ + version: "10" + }).setToken(process.env.TOKEN); + + (async () => { + try { + console.log("Started refreshing application (/) commands."); + + const data = await rest.put( + Routes.applicationCommands(process.env.CLIENT_ID), { + body: commandArray + }); + + console.log(`Successfully reloaded ${data.length} application (/) commands.`); + } catch (error) { + console.error(error); + } + })(); +}; \ No newline at end of file