Reputation: 1
I'm coding a discord bot and I have commands in a command folder and events in an event folder, however the command handler and event handler are in a functions folder. However, on my main script I need to call upon the functions folder so that it can pass "client" into the files in the functions folder while also running the handler scripts from the main script so that events and commands can be accessed at any time from within their respective folders. Here is the code I have currently:
const functionFolders = fs.readdirSync(`./functions`);
for (const folder of functionFolders) {
const functionFiles = fs.readdirSync(`./functions/${folder}`).filter((file) => file.endsWith(".js"));
for (const file of functionFiles) {
require(`./functions/${folder}/${file}`)(client);
}
}
the handler files have the same setup, calling upon the respective folder the handler is for, however the problem is that on the final line (where the require(...) is) there needs to be a function prior to the require. My code works without this section of code, other than me not being able to call upon the functions folder which in turn allows me to access the command and event folders.
Is there another way of setting it up so my main script calls upon all files within their subfolders in the functions folder and passes 'client' into them?
I had a more basic command and event handler, however I recently moved the code calling upon the command and event folders from the main script into their own scripts in a functions folder as it helps streamline as I add more features, except when I run the code the error is: "require(...) is not a function" which I can see that I am missing a function but I don't know what to put in its place to allow the rest of the code to work. Removing this portion of calling the functions folder allows the bot to start without error, but it prevents me from being able to access the command and event folders as the handlers (in the functions folder) cannot be accessed
Upvotes: 0
Views: 340