clean up: fs.promises api and reformat

This commit is contained in:
Baipyrus 2024-02-06 14:09:04 +01:00
parent fdedb50bc5
commit 46e938946c

View File

@ -1,13 +1,11 @@
import { Client, Collection, GatewayIntentBits } from 'discord.js'; import { Client, Collection, GatewayIntentBits } from 'discord.js';
import { readdir, stat } from 'fs/promises';
import { Partials } from 'discord.js';
import { join, dirname } from 'path'; import { join, dirname } from 'path';
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
import { promisify } from 'util';
import { config } from 'dotenv'; import { config } from 'dotenv';
import { readdir } from 'fs';
import { Partials } from 'discord.js';
config(); config();
const readdirAsync = promisify(readdir);
const required = ['data', 'execute']; const required = ['data', 'execute'];
const optional = ['autocomplete', 'modalSubmit']; const optional = ['autocomplete', 'modalSubmit'];
@ -39,44 +37,43 @@ const runClient = (commands, events) => {
const __dirname = dirname(fileURLToPath(import.meta.url)); const __dirname = dirname(fileURLToPath(import.meta.url));
const cmdPath = join(__dirname, 'commands'); const cmdPath = join(__dirname, 'commands');
const evtPath = join(__dirname, 'events'); const evtPath = join(__dirname, 'events');
readdirAsync(cmdPath) readdir(cmdPath)
.then( .then(async (categories) =>
async (categories) => // For each category directory
// For each category directory await Promise.all(
await Promise.all( categories.map(async (category) => {
categories.map(async (category) => { const catPath = join(cmdPath, category);
const catPath = join(cmdPath, category); const content = await readdir(catPath);
const content = await readdirAsync(catPath); const files = content.filter((file) => file.endsWith('.js') && !file.endsWith('.example.js'));
const files = content.filter((file) => file.endsWith('.js') && !file.endsWith('.example.js')); // For each command file
// For each command file return await Promise.all(
return await Promise.all( files.map(async (current) => {
files.map(async (current) => { const filePath = join(catPath, current);
const filePath = join(catPath, current); const command = await import(filePath);
const command = await import(filePath); // Warn incomplete commands
// Warn incomplete commands if (!required.every((name) => name in command)) {
if (!required.every((name) => name in command)) { console.error(
console.error( `[ERROR] The command at ${filePath} is missing a required "data" or "execute" property.`
`[ERROR] The command at ${filePath} is missing a required "data" or "execute" property.` );
); return 0;
return 0; }
} const properties = optional.filter((name) => !(name in command));
const properties = optional.filter((name) => !(name in command)); if (properties.length > 0)
if (properties.length > 0) properties.forEach((name) =>
properties.forEach((name) => console.warn(
console.warn( `[WARNING] The command at ${filePath} is missing a optional "${name}" property.`
`[WARNING] The command at ${filePath} is missing a optional "${name}" property.` )
) );
); // Add command to collection
// Add command to collection return command;
return command; })
}) );
); })
}) )
) ).then((commands) =>
) commands.reduce((a, i) => a.concat(i), []).filter((e) => e !== 0)
.then((commands) => commands.reduce((a, i) => a.concat(i), []).filter((e) => e !== 0)) ).then(async (commands) => {
.then(async (commands) => { const content = await readdir(evtPath);
const content = await readdirAsync(evtPath);
const files = content.filter((file) => file.endsWith('.js')); const files = content.filter((file) => file.endsWith('.js'));
const events = await Promise.all( const events = await Promise.all(
files.map(async (current) => { files.map(async (current) => {