From 46e938946c70151f484e91444dd9fe4452ff7c57 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Tue, 6 Feb 2024 14:09:04 +0100 Subject: [PATCH] clean up: fs.promises api and reformat --- index.js | 81 +++++++++++++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 42 deletions(-) diff --git a/index.js b/index.js index a08a3bf..eb777a6 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,11 @@ import { Client, Collection, GatewayIntentBits } from 'discord.js'; +import { readdir, stat } from 'fs/promises'; +import { Partials } from 'discord.js'; import { join, dirname } from 'path'; import { fileURLToPath } from 'url'; -import { promisify } from 'util'; import { config } from 'dotenv'; -import { readdir } from 'fs'; -import { Partials } from 'discord.js'; config(); -const readdirAsync = promisify(readdir); const required = ['data', 'execute']; const optional = ['autocomplete', 'modalSubmit']; @@ -39,44 +37,43 @@ const runClient = (commands, events) => { const __dirname = dirname(fileURLToPath(import.meta.url)); const cmdPath = join(__dirname, 'commands'); const evtPath = join(__dirname, 'events'); -readdirAsync(cmdPath) - .then( - async (categories) => - // For each category directory - await Promise.all( - categories.map(async (category) => { - const catPath = join(cmdPath, category); - const content = await readdirAsync(catPath); - const files = content.filter((file) => file.endsWith('.js') && !file.endsWith('.example.js')); - // For each command file - return await Promise.all( - files.map(async (current) => { - const filePath = join(catPath, current); - const command = await import(filePath); - // Warn incomplete commands - if (!required.every((name) => name in command)) { - console.error( - `[ERROR] The command at ${filePath} is missing a required "data" or "execute" property.` - ); - return 0; - } - const properties = optional.filter((name) => !(name in command)); - if (properties.length > 0) - properties.forEach((name) => - console.warn( - `[WARNING] The command at ${filePath} is missing a optional "${name}" property.` - ) - ); - // Add command to collection - return command; - }) - ); - }) - ) - ) - .then((commands) => commands.reduce((a, i) => a.concat(i), []).filter((e) => e !== 0)) - .then(async (commands) => { - const content = await readdirAsync(evtPath); +readdir(cmdPath) + .then(async (categories) => + // For each category directory + await Promise.all( + categories.map(async (category) => { + const catPath = join(cmdPath, category); + const content = await readdir(catPath); + const files = content.filter((file) => file.endsWith('.js') && !file.endsWith('.example.js')); + // For each command file + return await Promise.all( + files.map(async (current) => { + const filePath = join(catPath, current); + const command = await import(filePath); + // Warn incomplete commands + if (!required.every((name) => name in command)) { + console.error( + `[ERROR] The command at ${filePath} is missing a required "data" or "execute" property.` + ); + return 0; + } + const properties = optional.filter((name) => !(name in command)); + if (properties.length > 0) + properties.forEach((name) => + console.warn( + `[WARNING] The command at ${filePath} is missing a optional "${name}" property.` + ) + ); + // Add command to collection + return command; + }) + ); + }) + ) + ).then((commands) => + commands.reduce((a, i) => a.concat(i), []).filter((e) => e !== 0) + ).then(async (commands) => { + const content = await readdir(evtPath); const files = content.filter((file) => file.endsWith('.js')); const events = await Promise.all( files.map(async (current) => {