clean up: fs.promises api and reformat
This commit is contained in:
parent
fdedb50bc5
commit
46e938946c
81
index.js
81
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) => {
|
||||
|
Loading…
Reference in New Issue
Block a user