While merging and using the main bot class in which commands and events are handled, I got a TypeError while trying to load the function that includes all the commands and tries to operate on them.
Here's the full error:
/Users/user/Sviluppo/Discord-Bots/bot/index.js:58
const command = new (require(`./commands/${catDir}/${cmd}`))(this);
^
TypeError: require(...) is not a constructor
at /Users/user/Sviluppo/Discord-Bots/bot/index.js:58:35
at Array.forEach (<anonymous>)
at /Users/user/Sviluppo/Discord-Bots/bot/index.js:56:59
at FSReqCallback.oncomplete (fs.js:163:23)
class Bot extends Discord.Client {
constructor(options) {
super(options, { autoReconnect: true })
// ? Configuration File
this.config = require("./config.json")
// ? Commands and aliases as collections
this.commands = new Discord.Collection();
this.aliases = new Discord.Collection();
// ? Saves collections to disk thanks to Enchanted Maps
this.settings = new Enmap({ name: "settings", cloneLevel: "deep", fetchAll: false, autoFetch: true });
// ? Initializes a new Database
this.db = require("./resources/DBInit");
// ? Initializes languages
this.language = new (require(`./resources/lang/${[this.config.Language] || "English"}`))
// ? Gets the functions from the bot files
// TODO Implement a logger item
// Here we define some utils useful to speed up development
this.sleep = require("util").promisify(setTimeout);
//
// ? Let's define a new login way
// ! This will be of substitute for "const bot = new Bot()" and "bot.login" with "new Bot"
super.login(this.config.token)
// * ββββββ Command handler ββββββ
readdir("./commands/", (err, cmdDir) => {
// If there is error, throw an error in the console
if (err) { throw err; }
// Only include directory
cmdDir.filter((subDir) => !subDir.includes(".")).forEach((catDir) => {
// Browse categories
readdir(`./commands/${catDir}/`, (err, cmds) => {
// If there is error, throw an error in the console
if (err) { throw err; }
// Includes only .js files.
cmds.filter((file) => file.endsWith(".js")).forEach((cmd) => {;
// Include the file to be able to operate on it
const command = new (require(`./commands/${catDir}/${cmd}`))(this);
// Parse the file to retrieve the assigned name.
this.commands.set(command.help.name, command);
command.conf.aliases.forEach((a) => this.aliases.set(a, command.help.name));
});
});
});
});
// * ββββββ Event handler ββββββ
readdir("./events/", (err, events) => {
// If there is error, throw an error in the console
if (err) { throw err; }
// includes only .js files
events.filter((event) => event.endsWith(".js")).forEach((file) => {;
// Include the file to be able to operate on it
const event = new (require(`./events/${file}`))(this);
// Executes the file corresponding to the transmitted event.
super.on(file.split(".")[0], (...args) => event.run(...args, this.ops));
});
});
Also, using this opportunity, can I may ask you how can I make the Bot handlers and loaders more easy to implement commands? That will be awesome!
~ Thanks β€οΈ