admin管理员组

文章数量:1277281

I'm building a discord bot with node.js for my server and I have a bunch of mands for the bot. Each mand is in a different file so I have a lot of const cmd = require("../mands/cmd.js");

const kick = require("../mands/kick");
const info = require("../mands/info"); 
const cooldown = require("../mands/cooldown");
const help = require("../mands/help");

Is there a simpler way to do this?

I'm building a discord bot with node.js for my server and I have a bunch of mands for the bot. Each mand is in a different file so I have a lot of const cmd = require("../mands/cmd.js");

const kick = require("../mands/kick");
const info = require("../mands/info"); 
const cooldown = require("../mands/cooldown");
const help = require("../mands/help");

Is there a simpler way to do this?

Share Improve this question asked Apr 24, 2020 at 14:08 Forgotten-StormForgotten-Storm 3071 gold badge4 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

Inside folder mands put a file called index.js.

Each time you implement new mands in new file, require that file in index.js and then add it to the exports of it. For example index.js would be:

const kick = require('./kick');
const info = require('./info');

module.exports = {
  kick: kick,
  info: info
}

And then from any folder you can require multiple mands in one line like this:

const { kick, info } = require('../mands');

Export an object from one file instead?

const kick = require("../mands/kick");
const info = require("../mands/info"); 
const cooldown = require("../mands/cooldown");
const help = require("../mands/help");

const mands = {
  kick,
  info,
  ...
}

module.exports = mands;

And then:

const mands = require('mymands')

mands.kick()

Create index.js file inside the mand folder and then you can export an object like this.

const kick = require("../mands/kick");
const info = require("../mands/info"); 
const cooldown = require("../mands/cooldown");
const help = require("../mands/help");

const mand = {
  kick,
  info,
  cooldown,
  help
};

module.exports = mand;

You can import and use it like this:

const {kick, info} = require('./mands');

本文标签: javascriptNodejs require multiple files in the same folderStack Overflow