From 6b70b67f0f107ef0b21aea215c0a52e2715639b5 Mon Sep 17 00:00:00 2001 From: HewkawAr Date: Sun, 4 Aug 2024 22:47:27 +0700 Subject: [PATCH 1/3] Register Commands Once Time --- functions/handelCommands.js | 14 +++++------- index.js | 4 ++++ utils/commands.js | 45 +++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 utils/commands.js 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..86513ce 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(); +// Init 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..6a90f29 --- /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 From 1882141000293fa3a474bf09a740c9d2b1705592 Mon Sep 17 00:00:00 2001 From: HewkawAr Date: Sun, 4 Aug 2024 22:49:44 +0700 Subject: [PATCH 2/3] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 86513ce..874c35b 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,7 @@ const package = require("./package.json"); require("dotenv").config(); -// Init Bot Commands +// Register Bot Commands handleCommands(); // Sharding Manager From 80bb46021daa29077e1a90830eca9c1a171f6f3c Mon Sep 17 00:00:00 2001 From: HewkawAr Date: Sun, 4 Aug 2024 22:55:57 +0700 Subject: [PATCH 3/3] Update commands.js --- utils/commands.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/commands.js b/utils/commands.js index 6a90f29..e402e90 100644 --- a/utils/commands.js +++ b/utils/commands.js @@ -42,4 +42,4 @@ module.exports = async function () { console.error(error); } })(); -} \ No newline at end of file +}; \ No newline at end of file