Accessing handle functions globally.

I've been working on creating a Discord boilerplate for myself. One of my goals was to develop a handler function capable of producing a captcha SVG file. This function would not only return the file's location but also the content of the captcha. This could be used for verification and other similar tasks. However, I've discovered a limitation: the handler function can only be used within the index.js file. It seems that this restriction is due to how I've set up the handler registration process in the index.js file. You can find the progress of my boilerplate in this GitHub repository: Github Repo Here's the code snippet where I register the handlers:
handlerFiles.forEach((file) => {
if (file.endsWith(".js")) {
const handlerName = path.basename(file, ".js");
const handlerPath = path.join(handlersDirectory, file);
handlers[handlerName] = require(handlerPath);
}
});
handlerFiles.forEach((file) => {
if (file.endsWith(".js")) {
const handlerName = path.basename(file, ".js");
const handlerPath = path.join(handlersDirectory, file);
handlers[handlerName] = require(handlerPath);
}
});
This registration method limits the usage of handler functions. For instance, I'm able to use the GenerateCaptcha function from generateCaptchaHandler.js in the main.js file. However, I'm unable to employ it in a command.js file due to the registration constraints. I'm a bit uncertain about the approach to enable the handler files for usage throughout the entire program structure. If there are any suggestions or ideas from anyone, I would greatly appreciate the assistance.
GitHub
GitHub - KiraKenjiro/DiscordBotBoilerplate
Contribute to KiraKenjiro/DiscordBotBoilerplate development by creating an account on GitHub.
5 Replies
d.js toolkit
d.js toolkit15mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Kiɾʌ Kenjiɾø
Kiɾʌ KenjiɾøOP15mo ago
Node Version: v18.17.0 Discord.js Version: 14.13.0
monbrey
monbrey15mo ago
You should basically have these functions in a separate file, handlers.js or whatever, and export them Import it into anywhere else you need it Im not sure I understand why you've had to register them to an array though You could create a handlers/index.js that exports them all as a single item
module.exports = {
logHandler: require("./logHander.js"),
other: etc
}
module.exports = {
logHandler: require("./logHander.js"),
other: etc
}
Kiɾʌ Kenjiɾø
Kiɾʌ KenjiɾøOP15mo ago
It's just be being specific. I suppose it's my way of being organised, It's just so i can know. I need this to do this, Oh i have a handler for that let me just call it from response = handlers.CaptchaGenerator.GenerateCaptcha(); It's just me being organised basically pff that is true! I do kinda want to load them dynamically. but that is a fair point, if i have a file that loads them and just load that into any file i want the handlers to be in yeah that would work I think i understand how it all works now, thanks man <3
monbrey
monbrey15mo ago
yeah, just modularise your array too so it sits outside index Load it however youd like
Want results from more Discord servers?
Add your server