read filesystem overhaul. sharing functions across files

This commit is contained in:
Baipyrus 2024-02-06 15:25:58 +01:00
parent d773935367
commit f70912e4fd
3 changed files with 66 additions and 96 deletions

View File

@ -1,15 +1,10 @@
import { getFiles, importAndCheck } from './shared.js';
import { REST, Routes } from 'discord.js'; import { REST, Routes } 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';
config(); config();
const readdirAsync = promisify(readdir);
const required = ['data', 'execute'];
const optional = ['autocomplete', 'modalSubmit'];
// Construct and prepare an instance of the REST module // Construct and prepare an instance of the REST module
const rest = new REST().setToken(process.env.TOKEN); const rest = new REST().setToken(process.env.TOKEN);
@ -32,40 +27,11 @@ const putCommands = async (commands) => {
// Register commands from sub-directories // Register commands from sub-directories
const __dirname = dirname(fileURLToPath(import.meta.url)); const __dirname = dirname(fileURLToPath(import.meta.url));
const mainPath = join(__dirname, 'commands'); const cmdPath = join(__dirname, 'commands');
readdirAsync(mainPath) getFiles(cmdPath)
.then( // For each command file
async (categories) => .then(async (files) =>
// For each category directory (await Promise.all(files.map(importAndCheck)))
await Promise.all( .filter((module) => module !== 0)
categories.map(async (category) => { .map((module) => module.data.toJSON())
const catPath = join(mainPath, category); ).then(putCommands);
const files = (await readdirAsync(catPath)).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.data.toJSON();
})
);
})
)
)
.then((commands) => commands.reduce((a, i) => a.concat(i), []).filter((e) => e !== 0))
.then(putCommands);

View File

@ -1,19 +1,21 @@
import { Client, Collection, GatewayIntentBits } from 'discord.js'; import { Client, Collection, GatewayIntentBits } from 'discord.js';
import { getFiles, importAndCheck } from './shared.js';
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';
config(); config();
const readdirAsync = promisify(readdir);
const required = ['data', 'execute'];
const optional = ['autocomplete', 'modalSubmit'];
const runClient = (commands, events) => { const runClient = (commands, events) => {
// Create a new client instance // Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] }); const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
],
partials: [Partials.Message, Partials.Reaction]
});
client.commands = new Collection(); client.commands = new Collection();
commands.forEach((c) => client.commands.set(c.data.name, c)); commands.forEach((c) => client.commands.set(c.data.name, c));
events.forEach((e) => events.forEach((e) =>
@ -30,52 +32,16 @@ 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) getFiles(cmdPath)
.then( // For each command file
async (categories) => .then(async (files) =>
// For each category directory (await Promise.all(files.map(importAndCheck)))
await Promise.all( .filter((module) => module !== 0)
categories.map(async (category) => { ).then(async (commands) => {
const catPath = join(cmdPath, category); const files = await getFiles(evtPath);
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);
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 (filePath) =>
const filePath = join(evtPath, current); await import(filePath)
const command = await import(filePath); ));
// Add event to collection
return command;
})
);
runClient(commands, events); runClient(commands, events);
}); });

38
shared.js Normal file
View File

@ -0,0 +1,38 @@
import { join } from 'path';
import { readdir } from 'fs/promises';
const required = ['data', 'execute'];
const optional = ['autocomplete', 'modalSubmit'];
export const getFiles = async (dir) => {
const dirents = await readdir(dir, { withFileTypes: true });
const files = await Promise.all(dirents.map((dirent) => {
const res = join(dir, dirent.name);
return dirent.isDirectory() ? getFiles(res) : res;
}));
return Array.prototype.concat(...files);
};
export const importAndCheck = async (filePath) => {
if (!filePath.endsWith('.js') || filePath.endsWith('.example.js')) {
// Skip this file
return 0;
}
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 an optional "${name}" property.`
)
);
// Add command to collection
return command;
};