read filesystem overhaul. sharing functions across files
This commit is contained in:
parent
d773935367
commit
f70912e4fd
50
deploy.js
50
deploy.js
@ -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(
|
|
||||||
async (categories) =>
|
|
||||||
// For each category directory
|
|
||||||
await Promise.all(
|
|
||||||
categories.map(async (category) => {
|
|
||||||
const catPath = join(mainPath, category);
|
|
||||||
const files = (await readdirAsync(catPath)).filter((file) => file.endsWith('.js') && !file.endsWith('.example.js'));
|
|
||||||
// For each command file
|
// For each command file
|
||||||
return await Promise.all(
|
.then(async (files) =>
|
||||||
files.map(async (current) => {
|
(await Promise.all(files.map(importAndCheck)))
|
||||||
const filePath = join(catPath, current);
|
.filter((module) => module !== 0)
|
||||||
const command = await import(filePath);
|
.map((module) => module.data.toJSON())
|
||||||
// Warn incomplete commands
|
).then(putCommands);
|
||||||
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);
|
|
||||||
|
70
index.js
70
index.js
@ -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(
|
|
||||||
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
|
// For each command file
|
||||||
return await Promise.all(
|
.then(async (files) =>
|
||||||
files.map(async (current) => {
|
(await Promise.all(files.map(importAndCheck)))
|
||||||
const filePath = join(catPath, current);
|
.filter((module) => module !== 0)
|
||||||
const command = await import(filePath);
|
).then(async (commands) => {
|
||||||
// Warn incomplete commands
|
const files = await getFiles(evtPath);
|
||||||
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
38
shared.js
Normal 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;
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user