From 6ac023df84762daa0fc0815dbf274e410dcef80b Mon Sep 17 00:00:00 2001 From: Alexander Ballauf Date: Sun, 28 May 2023 20:42:58 +0200 Subject: [PATCH] Chore: lint fixes --- commands/core/module.js | 207 +++++++++---------- commands/core/reload.js | 51 ++--- commands/dvc/dvc.js | 4 +- handlers/commandHandler.js | 36 ++-- handlers/deployCommands.js | 402 +++++++++++++++++++------------------ handlers/eventHandler.js | 24 +-- 6 files changed, 370 insertions(+), 354 deletions(-) diff --git a/commands/core/module.js b/commands/core/module.js index 6fc3958..84c4e55 100644 --- a/commands/core/module.js +++ b/commands/core/module.js @@ -6,126 +6,131 @@ const { reloadGuildCommands } = require('../../handlers/deployCommands'); const { PermissionFlagsBits } = require('discord.js'); async function loadModules(guildId) { - try { - const guildModulesFilePath = path.join(guildModulesFolderPath, `${guildId}.json`); - const fileExists = await fs.promises.access(guildModulesFilePath).then(() => true).catch(() => false); - if (fileExists) { - const data = await fs.promises.readFile(guildModulesFilePath, 'utf8'); - return JSON.parse(data); - } else { - return {}; - } - } catch (error) { - console.error('Error loading modules:', error); - return {}; - } + try { + const guildModulesFilePath = path.join(guildModulesFolderPath, `${guildId}.json`); + const fileExists = await fs.promises.access(guildModulesFilePath).then(() => true).catch(() => false); + if (fileExists) { + const data = await fs.promises.readFile(guildModulesFilePath, 'utf8'); + return JSON.parse(data); + } + else { + return {}; + } + } + catch (error) { + console.error('Error loading modules:', error); + return {}; + } } async function saveModules(guildId, modules) { - try { - const guildModulesFilePath = path.join(guildModulesFolderPath, `${guildId}.json`); - await fs.promises.writeFile(guildModulesFilePath, JSON.stringify(modules, null, 2)); - } catch (error) { - console.error('Error saving modules:', error); - } + try { + const guildModulesFilePath = path.join(guildModulesFolderPath, `${guildId}.json`); + await fs.promises.writeFile(guildModulesFilePath, JSON.stringify(modules, null, 2)); + } + catch (error) { + console.error('Error saving modules:', error); + } } function getModuleChoices() { - const commandsFolderPath = path.join(__dirname, '..', '..', 'commands'); - const moduleChoices = []; + const commandsFolderPath = path.join(__dirname, '..', '..', 'commands'); + const moduleChoices = []; - fs.readdirSync(commandsFolderPath).forEach(folder => { - const moduleFolderPath = path.join(commandsFolderPath, folder); - if (fs.statSync(moduleFolderPath).isDirectory()) { - const commandFiles = fs.readdirSync(moduleFolderPath).filter(file => file.endsWith('.js')); - if (commandFiles.length > 0 && folder !== 'core') { - moduleChoices.push({ - name: folder.charAt(0).toUpperCase() + folder.slice(1), - value: folder, - }); - } - } - }); + fs.readdirSync(commandsFolderPath).forEach(folder => { + const moduleFolderPath = path.join(commandsFolderPath, folder); + if (fs.statSync(moduleFolderPath).isDirectory()) { + const commandFiles = fs.readdirSync(moduleFolderPath).filter(file => file.endsWith('.js')); + if (commandFiles.length > 0 && folder !== 'core') { + moduleChoices.push({ + name: folder.charAt(0).toUpperCase() + folder.slice(1), + value: folder, + }); + } + } + }); - return moduleChoices; + return moduleChoices; } function createGuildModules(guildId) { - const guildModulesFilePath = path.join(guildModulesFolderPath, `${guildId}.json`); - if (!fs.existsSync(guildModulesFolderPath)) { - fs.mkdirSync(guildModulesFolderPath, { recursive: true }); - } - if (!fs.existsSync(guildModulesFilePath)) { - fs.writeFileSync(guildModulesFilePath, JSON.stringify({}, null, 2)); - } + const guildModulesFilePath = path.join(guildModulesFolderPath, `${guildId}.json`); + if (!fs.existsSync(guildModulesFolderPath)) { + fs.mkdirSync(guildModulesFolderPath, { recursive: true }); + } + if (!fs.existsSync(guildModulesFilePath)) { + fs.writeFileSync(guildModulesFilePath, JSON.stringify({}, null, 2)); + } } module.exports = { - cooldown: 180, - data: new SlashCommandBuilder() - .setName('module') - .setDescription('Enable, disable, or show modules') - .setDMPermission(false) - .setDefaultMemberPermissions(PermissionFlagsBits.Administrator) - .addSubcommand(subcommand => - subcommand - .setName('add') - .setDescription('Enable a module') - .addStringOption(option => - option.setName('module') - .setDescription('Module to enable') - .setRequired(true) - .addChoices(...getModuleChoices()) - ) - ) - .addSubcommand(subcommand => - subcommand - .setName('remove') - .setDescription('Disable a module') - .addStringOption(option => - option.setName('module') - .setDescription('Module to disable') - .setRequired(true) - .addChoices(...getModuleChoices()) - ) - ) - .addSubcommand(subcommand => - subcommand - .setName('show') - .setDescription('Show active modules for the guild') - ), - category: 'core', - async execute(interaction) { - const subcommand = interaction.options.getSubcommand(); - const guildId = interaction.guildId; - const modules = await loadModules(guildId); // Load modules asynchronously + cooldown: 180, + data: new SlashCommandBuilder() + .setName('module') + .setDescription('Enable, disable, or show modules') + .setDMPermission(false) + .setDefaultMemberPermissions(PermissionFlagsBits.Administrator) + .addSubcommand(subcommand => + subcommand + .setName('add') + .setDescription('Enable a module') + .addStringOption(option => + option.setName('module') + .setDescription('Module to enable') + .setRequired(true) + .addChoices(...getModuleChoices()), + ), + ) + .addSubcommand(subcommand => + subcommand + .setName('remove') + .setDescription('Disable a module') + .addStringOption(option => + option.setName('module') + .setDescription('Module to disable') + .setRequired(true) + .addChoices(...getModuleChoices()), + ), + ) + .addSubcommand(subcommand => + subcommand + .setName('show') + .setDescription('Show active modules for the guild'), + ), + category: 'core', + async execute(interaction) { + const subcommand = interaction.options.getSubcommand(); + const guildId = interaction.guildId; + const modules = await loadModules(guildId); - createGuildModules(guildId); + createGuildModules(guildId); - if (subcommand === 'add') { - const module = interaction.options.getString('module'); + if (subcommand === 'add') { + const module = interaction.options.getString('module'); - if (module === 'core') { - interaction.reply('The core module cannot be enabled or disabled per guild.'); - return; - } + if (module === 'core') { + interaction.reply('The core module cannot be enabled or disabled per guild.'); + return; + } - modules[module] = true; - await saveModules(guildId, modules); // Save modules before deploying commands - reloadGuildCommands(guildId); // Deploy commands after saving modules + modules[module] = true; + await saveModules(guildId, modules); + reloadGuildCommands(guildId); - interaction.reply({ content: `Module \`${module}\` has been enabled for this guild.`, ephemeral: true }); - } else if (subcommand === 'remove') { - const module = interaction.options.getString('module'); + interaction.reply({ content: `Module \`${module}\` has been enabled for this guild.`, ephemeral: true }); + } + else if (subcommand === 'remove') { + const module = interaction.options.getString('module'); - delete modules[module]; - await saveModules(guildId, modules); // Save modules before deploying commands - reloadGuildCommands(guildId); // Deploy commands after saving modules + delete modules[module]; + await saveModules(guildId, modules); + reloadGuildCommands(guildId); - interaction.reply({ content: `Module \`${module}\` has been disabled for this guild.`, ephemeral: true }); - } else if (subcommand === 'show') { - const activeModules = Object.keys(modules).filter(key => modules[key]).join(', ') || '---'; - interaction.reply({ content: `Active modules for Guild ID ${guildId}: ${activeModules}`, ephemeral: true }); - } - }, + interaction.reply({ content: `Module \`${module}\` has been disabled for this guild.`, ephemeral: true }); + } + else if (subcommand === 'show') { + const activeModules = Object.keys(modules).filter(key => modules[key]).join(', ') || '---'; + interaction.reply({ content: `Active modules for Guild ID ${guildId}: ${activeModules}`, ephemeral: true }); + } + }, }; diff --git a/commands/core/reload.js b/commands/core/reload.js index f2ab101..eb43558 100644 --- a/commands/core/reload.js +++ b/commands/core/reload.js @@ -3,30 +3,31 @@ const { ownerId } = require('../../config/config.json'); const deployCommands = require('../../handlers/deployCommands'); module.exports = { - cooldown: 180, - data: new SlashCommandBuilder() - .setName('reload') - .setDescription('Reloads all commands.') - .setDMPermission(false), - category: 'core', - async execute(interaction) { - if (interaction.member.id !== ownerId) { - return interaction.reply({ - content: 'Only the bot owner is allowed to use this command!', - ephemeral: true, - }); - } + cooldown: 180, + data: new SlashCommandBuilder() + .setName('reload') + .setDescription('Reloads all commands.') + .setDMPermission(false), + category: 'core', + async execute(interaction) { + if (interaction.member.id !== ownerId) { + return interaction.reply({ + content: 'Only the bot owner is allowed to use this command!', + ephemeral: true, + }); + } - try { - await interaction.deferReply({ ephemeral: true }); - setTimeout(async () => { - await interaction.editReply('Reloading commands...'); - deployCommands(); - await interaction.editReply('All commands have been reloaded!'); - }, 5000); - } catch (error) { - console.error(error); - await interaction.editReply(`There was an error while reloading commands:\n\`${error.message}\``); - } - }, + try { + await interaction.deferReply({ ephemeral: true }); + setTimeout(async () => { + await interaction.editReply('Reloading commands...'); + deployCommands(); + await interaction.editReply('All commands have been reloaded!'); + }, 5000); + } + catch (error) { + console.error(error); + await interaction.editReply(`There was an error while reloading commands:\n\`${error.message}\``); + } + }, }; diff --git a/commands/dvc/dvc.js b/commands/dvc/dvc.js index 55afb8b..cf4261f 100644 --- a/commands/dvc/dvc.js +++ b/commands/dvc/dvc.js @@ -86,12 +86,12 @@ module.exports = { channel.delete(); } - await interaction.reply({ content:'Voice creator successfully removed.', ephemeral: true}); + await interaction.reply({ content:'Voice creator successfully removed.', ephemeral: true }); } } catch (error) { console.error(error); - await interaction.reply({ content:'An internal error occured.', ephemeral: true}); + await interaction.reply({ content:'An internal error occured.', ephemeral: true }); } }, }; diff --git a/handlers/commandHandler.js b/handlers/commandHandler.js index 22f5180..f636d3b 100644 --- a/handlers/commandHandler.js +++ b/handlers/commandHandler.js @@ -3,25 +3,25 @@ const path = require('node:path'); const { Collection } = require('discord.js'); const handleCommands = (client) => { - client.commands = new Collection(); - client.cooldowns = new Collection(); - const foldersPath = path.join(__dirname, '..', 'commands'); - const commandFolders = fs.readdirSync(foldersPath); + client.commands = new Collection(); + client.cooldowns = new Collection(); + const foldersPath = path.join(__dirname, '..', 'commands'); + const commandFolders = fs.readdirSync(foldersPath); - for (const folder of commandFolders) { - const commandsPath = path.join(foldersPath, folder); - const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); - for (const file of commandFiles) { - const filePath = path.join(commandsPath, file); - const command = require(filePath); - if ('data' in command && 'execute' in command) { - client.commands.set(command.data.name, command); - } - else { - console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); - } - } - } + for (const folder of commandFolders) { + const commandsPath = path.join(foldersPath, folder); + const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); + for (const file of commandFiles) { + const filePath = path.join(commandsPath, file); + const command = require(filePath); + if ('data' in command && 'execute' in command) { + client.commands.set(command.data.name, command); + } + else { + console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); + } + } + } }; module.exports = handleCommands; diff --git a/handlers/deployCommands.js b/handlers/deployCommands.js index 990b7fd..d8734f1 100644 --- a/handlers/deployCommands.js +++ b/handlers/deployCommands.js @@ -1,225 +1,235 @@ const { REST } = require('@discordjs/rest'); const { Routes } = require('discord-api-types/v9'); -const { token, clientId } = require("../config/config.json"); +const { token, clientId } = require('../config/config.json'); const fs = require('fs'); const path = require('path'); const guildModulesFolderPath = path.join(__dirname, '..', 'config', 'modules'); function createDirectoryIfNotExists(directoryPath) { - if (!fs.existsSync(directoryPath)) { - fs.mkdirSync(directoryPath, { recursive: true }); - } + if (!fs.existsSync(directoryPath)) { + fs.mkdirSync(directoryPath, { recursive: true }); + } } function createModuleFileIfNotExists(guildId) { - const guildModulesFilePath = path.join(guildModulesFolderPath, `${guildId}.json`); - if (!fs.existsSync(guildModulesFilePath)) { - fs.writeFileSync(guildModulesFilePath, '{}'); - } + const guildModulesFilePath = path.join(guildModulesFolderPath, `${guildId}.json`); + if (!fs.existsSync(guildModulesFilePath)) { + fs.writeFileSync(guildModulesFilePath, '{}'); + } } createDirectoryIfNotExists(guildModulesFolderPath); function loadModules(guildId) { - const filePath = path.join(__dirname, '..', 'config', 'modules', `${guildId}.json`); - - try { - const data = fs.readFileSync(filePath, 'utf8'); - - if (data) { - return JSON.parse(data); - } else { - console.error(`Module data for Guild ID ${guildId} is empty. Returning an empty object.`); - return {}; - } - } catch (error) { - console.error(`Error reading module data for Guild ID ${guildId}. Returning an empty object.`, error); - return {}; - } + const filePath = path.join(__dirname, '..', 'config', 'modules', `${guildId}.json`); + + try { + const data = fs.readFileSync(filePath, 'utf8'); + + if (data) { + return JSON.parse(data); + } + else { + console.error(`Module data for Guild ID ${guildId} is empty. Returning an empty object.`); + return {}; + } + } + catch (error) { + console.error(`Error reading module data for Guild ID ${guildId}. Returning an empty object.`, error); + return {}; + } } function saveModules(guildId, modules) { - try { - const guildModulesFilePath = path.join(guildModulesFolderPath, `${guildId}.json`); - fs.writeFileSync(guildModulesFilePath, JSON.stringify(modules, null, 2)); - } catch (error) { - console.error('Error saving modules:', error); - } + try { + const guildModulesFilePath = path.join(guildModulesFolderPath, `${guildId}.json`); + fs.writeFileSync(guildModulesFilePath, JSON.stringify(modules, null, 2)); + } + catch (error) { + console.error('Error saving modules:', error); + } } async function deployCommands() { - const foldersPath = path.join(__dirname, '..', 'commands'); - const moduleFolders = fs.readdirSync(foldersPath); - - const rest = new REST({ version: '9' }).setToken(token); - - try { - const globalCommands = []; - - const coreModuleFolderPath = path.join(foldersPath, 'core'); - const coreCommandFiles = fs.readdirSync(coreModuleFolderPath).filter(file => file.endsWith('.js')); - - console.log('Loading global commands...'); - - for (const file of coreCommandFiles) { - const filePath = path.join(coreModuleFolderPath, file); - const command = require(filePath); - - if ('data' in command && 'execute' in command) { - globalCommands.push(command.data.toJSON()); - console.log('\x1b[36m%s\x1b[0m', '|Loaded|', command.data.name); - } else { - console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); - } - } - - await rest.put( - Routes.applicationCommands(clientId), - { body: globalCommands }, - ); - - console.log('\x1b[32m%s\x1b[0m', `Successfully deployed ${globalCommands.length} global application (/) commands.`); - - const guilds = await rest.get(Routes.userGuilds()); - const guildIds = guilds.map(guild => guild.id); - - for (const guildId of guildIds) { - createModuleFileIfNotExists(guildId); - - const modules = loadModules(guildId); - - console.log('\x1b[31m%s\x1b[0m', `Active modules for Guild ID ${guildId}:`, Object.keys(modules).length > 0 ? Object.keys(modules).join(', ') : '---'); - - const existingCommands = await rest.get(Routes.applicationGuildCommands(clientId, guildId)); - - const commandIdsToDelete = existingCommands.map(command => command.id); - - await rest.put( - Routes.applicationGuildCommands(clientId, guildId), - { body: [] }, - ); - - console.log('\x1b[33m%s\x1b[0m', `Deleted ${commandIdsToDelete.length} guild application (/) commands for Guild ID: ${guildId}.`); - - const guildCommands = []; - - for (const moduleName in modules) { - if (moduleFolders.includes(moduleName)) { - const moduleFolderPath = path.join(foldersPath, moduleName); - const commandFiles = fs.readdirSync(moduleFolderPath).filter(file => file.endsWith('.js')); - - for (const file of commandFiles) { - const filePath = path.join(moduleFolderPath, file); - const command = require(filePath); - - if ('data' in command && 'execute' in command) { - guildCommands.push(command.data.toJSON()); - console.log('\x1b[36m%s\x1b[0m', '|Loaded|', command.data.name); - } else { - console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); - } - } - } - } - - if (guildCommands.length > 0) { - const guildData = guildCommands.map(command => { - return { - name: command.name, - description: command.description, - options: command.options ? command.options : [], - }; - }); - - await rest.put( - Routes.applicationGuildCommands(clientId, guildId), - { body: guildData }, - ); - - saveModules(guildId, modules); - - console.log('\x1b[32m%s\x1b[0m', `Successfully deployed ${guildCommands.length} guild application (/) commands for Guild ID: ${guildId}.`); - } else { - console.log(`No guild commands to update for Guild ID: ${guildId}.`); - } - } - } catch (error) { - console.error(error); - } + const foldersPath = path.join(__dirname, '..', 'commands'); + const moduleFolders = fs.readdirSync(foldersPath); + + const rest = new REST({ version: '9' }).setToken(token); + + try { + const globalCommands = []; + + const coreModuleFolderPath = path.join(foldersPath, 'core'); + const coreCommandFiles = fs.readdirSync(coreModuleFolderPath).filter(file => file.endsWith('.js')); + + console.log('Loading global commands...'); + + for (const file of coreCommandFiles) { + const filePath = path.join(coreModuleFolderPath, file); + const command = require(filePath); + + if ('data' in command && 'execute' in command) { + globalCommands.push(command.data.toJSON()); + console.log('\x1b[36m%s\x1b[0m', '|Loaded|', command.data.name); + } + else { + console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); + } + } + + await rest.put( + Routes.applicationCommands(clientId), + { body: globalCommands }, + ); + + console.log('\x1b[32m%s\x1b[0m', `Successfully deployed ${globalCommands.length} global application (/) commands.`); + + const guilds = await rest.get(Routes.userGuilds()); + const guildIds = guilds.map(guild => guild.id); + + for (const guildId of guildIds) { + createModuleFileIfNotExists(guildId); + + const modules = loadModules(guildId); + + console.log('\x1b[31m%s\x1b[0m', `Active modules for Guild ID ${guildId}:`, Object.keys(modules).length > 0 ? Object.keys(modules).join(', ') : '---'); + + const existingCommands = await rest.get(Routes.applicationGuildCommands(clientId, guildId)); + + const commandIdsToDelete = existingCommands.map(command => command.id); + + await rest.put( + Routes.applicationGuildCommands(clientId, guildId), + { body: [] }, + ); + + console.log('\x1b[33m%s\x1b[0m', `Deleted ${commandIdsToDelete.length} guild application (/) commands for Guild ID: ${guildId}.`); + + const guildCommands = []; + + for (const moduleName in modules) { + if (moduleFolders.includes(moduleName)) { + const moduleFolderPath = path.join(foldersPath, moduleName); + const commandFiles = fs.readdirSync(moduleFolderPath).filter(file => file.endsWith('.js')); + + for (const file of commandFiles) { + const filePath = path.join(moduleFolderPath, file); + const command = require(filePath); + + if ('data' in command && 'execute' in command) { + guildCommands.push(command.data.toJSON()); + console.log('\x1b[36m%s\x1b[0m', '|Loaded|', command.data.name); + } + else { + console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); + } + } + } + } + + if (guildCommands.length > 0) { + const guildData = guildCommands.map(command => { + return { + name: command.name, + description: command.description, + options: command.options ? command.options : [], + }; + }); + + await rest.put( + Routes.applicationGuildCommands(clientId, guildId), + { body: guildData }, + ); + + saveModules(guildId, modules); + + console.log('\x1b[32m%s\x1b[0m', `Successfully deployed ${guildCommands.length} guild application (/) commands for Guild ID: ${guildId}.`); + } + else { + console.log(`No guild commands to update for Guild ID: ${guildId}.`); + } + } + } + catch (error) { + console.error(error); + } } async function reloadGuildCommands(guildId) { - const foldersPath = path.join(__dirname, '..', 'commands'); - const moduleFolders = fs.readdirSync(foldersPath); - - const rest = new REST({ version: '9' }).setToken(token); - - try { - createModuleFileIfNotExists(guildId); - - const modules = loadModules(guildId); - - console.log('\x1b[31m%s\x1b[0m', `Active modules for Guild ID ${guildId}:`, Object.keys(modules).length > 0 ? Object.keys(modules).join(', ') : '---'); - - const existingCommands = await rest.get(Routes.applicationGuildCommands(clientId, guildId)); - - const commandIdsToDelete = existingCommands.map(command => command.id); - - await rest.put( - Routes.applicationGuildCommands(clientId, guildId), - { body: [] }, - ); - - console.log('\x1b[33m%s\x1b[0m', `Deleted ${commandIdsToDelete.length} guild application (/) commands for Guild ID: ${guildId}.`); - - const guildCommands = []; - - for (const moduleName in modules) { - if (moduleFolders.includes(moduleName)) { - const moduleFolderPath = path.join(foldersPath, moduleName); - const commandFiles = fs.readdirSync(moduleFolderPath).filter(file => file.endsWith('.js')); - - for (const file of commandFiles) { - const filePath = path.join(moduleFolderPath, file); - const command = require(filePath); - - if ('data' in command && 'execute' in command) { - guildCommands.push(command.data.toJSON()); - console.log('\x1b[36m%s\x1b[0m', '|Loaded|', command.data.name); - } else { - console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); - } - } - } - } - - if (guildCommands.length > 0) { - const guildData = guildCommands.map(command => { - return { - name: command.name, - description: command.description, - options: command.options ? command.options : [], - }; - }); - - await rest.put( - Routes.applicationGuildCommands(clientId, guildId), - { body: guildData }, - ); - - saveModules(guildId, modules); - - console.log('\x1b[32m%s\x1b[0m', `Successfully deployed ${guildCommands.length} guild application (/) commands for Guild ID: ${guildId}.`); - } else { - console.log(`No guild commands to update for Guild ID: ${guildId}.`); - } - } catch (error) { - console.error(error); - } + const foldersPath = path.join(__dirname, '..', 'commands'); + const moduleFolders = fs.readdirSync(foldersPath); + + const rest = new REST({ version: '9' }).setToken(token); + + try { + createModuleFileIfNotExists(guildId); + + const modules = loadModules(guildId); + + console.log('\x1b[31m%s\x1b[0m', `Active modules for Guild ID ${guildId}:`, Object.keys(modules).length > 0 ? Object.keys(modules).join(', ') : '---'); + + const existingCommands = await rest.get(Routes.applicationGuildCommands(clientId, guildId)); + + const commandIdsToDelete = existingCommands.map(command => command.id); + + await rest.put( + Routes.applicationGuildCommands(clientId, guildId), + { body: [] }, + ); + + console.log('\x1b[33m%s\x1b[0m', `Deleted ${commandIdsToDelete.length} guild application (/) commands for Guild ID: ${guildId}.`); + + const guildCommands = []; + + for (const moduleName in modules) { + if (moduleFolders.includes(moduleName)) { + const moduleFolderPath = path.join(foldersPath, moduleName); + const commandFiles = fs.readdirSync(moduleFolderPath).filter(file => file.endsWith('.js')); + + for (const file of commandFiles) { + const filePath = path.join(moduleFolderPath, file); + const command = require(filePath); + + if ('data' in command && 'execute' in command) { + guildCommands.push(command.data.toJSON()); + console.log('\x1b[36m%s\x1b[0m', '|Loaded|', command.data.name); + } + else { + console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); + } + } + } + } + + if (guildCommands.length > 0) { + const guildData = guildCommands.map(command => { + return { + name: command.name, + description: command.description, + options: command.options ? command.options : [], + }; + }); + + await rest.put( + Routes.applicationGuildCommands(clientId, guildId), + { body: guildData }, + ); + + saveModules(guildId, modules); + + console.log('\x1b[32m%s\x1b[0m', `Successfully deployed ${guildCommands.length} guild application (/) commands for Guild ID: ${guildId}.`); + } + else { + console.log(`No guild commands to update for Guild ID: ${guildId}.`); + } + } + catch (error) { + console.error(error); + } } module.exports = { - deployCommands, - reloadGuildCommands, + deployCommands, + reloadGuildCommands, }; \ No newline at end of file diff --git a/handlers/eventHandler.js b/handlers/eventHandler.js index a3f77ef..bab6b74 100644 --- a/handlers/eventHandler.js +++ b/handlers/eventHandler.js @@ -2,19 +2,19 @@ const fs = require('node:fs'); const path = require('node:path'); const handleEvents = (client) => { - const eventsPath = path.join(__dirname, '..', 'events'); - const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js')); + const eventsPath = path.join(__dirname, '..', 'events'); + const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js')); - for (const file of eventFiles) { - const filePath = path.join(eventsPath, file); - const event = require(filePath); - if (event.once) { - client.once(event.name, (...args) => event.execute(...args)); - } - else { - client.on(event.name, (...args) => event.execute(...args)); - } - } + for (const file of eventFiles) { + const filePath = path.join(eventsPath, file); + const event = require(filePath); + if (event.once) { + client.once(event.name, (...args) => event.execute(...args)); + } + else { + client.on(event.name, (...args) => event.execute(...args)); + } + } }; module.exports = handleEvents;