Import all commands from folder with ES modules

So my previous bot I build I used CommonJS and did the commands like this:
const commandsList = [];
const slashCommandFiles = readdirSync(path.resolve(__dirname, '../discord/commands'));

for (const fileSlash of slashCommandFiles) {
const commandSlash = require(path.resolve(__dirname, `../discord/commands/${fileSlash}`));
commandsList.push(commandSlash.commands)
}
const commandsList = [];
const slashCommandFiles = readdirSync(path.resolve(__dirname, '../discord/commands'));

for (const fileSlash of slashCommandFiles) {
const commandSlash = require(path.resolve(__dirname, `../discord/commands/${fileSlash}`));
commandsList.push(commandSlash.commands)
}
But now I really prefer to work with ES modules also because they are by default async. But now I have been thinking about a way to import all commands from the folder. But it doesn't work that easy with import. Anyone know a good way to do this. Because the export where build like
{
data: blabla,
action: (arg) => { stuff }
}
{
data: blabla,
action: (arg) => { stuff }
}
10 Replies
d.js docs
d.js docs3y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Ypsilon
YpsilonOP3y ago
for (const fileSlash of slashCommandFiles) {
import command from `../discord/commands/${fileSlash}`
commandsList.push(commandSlash.commands)
}
for (const fileSlash of slashCommandFiles) {
import command from `../discord/commands/${fileSlash}`
commandsList.push(commandSlash.commands)
}
import like this just wont work or am I just doing something wrong. But can't an import just be used at the beginning ?
Dagan
Dagan3y ago
Isnt it import commandSlash from path?
Ypsilon
YpsilonOP3y ago
yes but
Dagan
Dagan3y ago
Replacing path with your path ofc
Ypsilon
YpsilonOP3y ago
Dagan
Dagan3y ago
Ohh I see Maybe you could make your own import function In a different file
Ypsilon
YpsilonOP3y ago
If only you could just import a folder... 😦 but I just read in the nodejs docs its not possible Sadge
import { readdirSync } from 'fs'

const commands = readdirSync('../discord/commands')
console.log(commands);
import { readdirSync } from 'fs'

const commands = readdirSync('../discord/commands')
console.log(commands);
doesnt even work in ES6 😦
Syjalo
Syjalo3y ago
Ypsilon
YpsilonOP3y ago
My apologies I saw it wrong i get what you are saying now thank you. Sorry Thank you @qjuh How I have it now and working
import path from 'path';
import { readdirSync } from 'fs';

const cmdList = [];
const cmdFiles = readdirSync(path.resolve('./discord/commands'));

for (const file of cmdFiles) {
const cmd = (await import(`../discord/commands/${file}`)).default
cmdList.push(cmd)
}

console.log(cmdList);
import path from 'path';
import { readdirSync } from 'fs';

const cmdList = [];
const cmdFiles = readdirSync(path.resolve('./discord/commands'));

for (const file of cmdFiles) {
const cmd = (await import(`../discord/commands/${file}`)).default
cmdList.push(cmd)
}

console.log(cmdList);
Again my apologies.
Want results from more Discord servers?
Add your server