Reputation: 79
I am making a large config.js file for all my messages (Discord.js V12). It works fine and everything, until the message I am trying to send contains variables (I am just stepping over from the situation with all variables spread, to one file). Somehow, the command file imports the variables from config.js, but not the other way around. This leaves me in a very tricky spot, where the config.js file gets rendered pretty much useless (for my situation). I switched from an .ENV file to a .JS file, specifically so that (I thought) I can include variables in my external file. Here is an sample for a problem:
Old situation:
const randomnumber = 5;
message.channel.send(`Hello world! ${randomnumber}`);
New situation, which doesn't work at the moment:
const config = require("./../../Other/config.js");
const randomnumber = 5;
message.channel.send(config.messages.testmessage);
Here is a piece of config.js (the randomnumber
variable doesn't work, which is my problem):
exports.messages = {
testmessage: `Hello world! ${randomnumber}`),
}
I think that it doesn't work if also place const randomnumber = 5
in the config.js file, since somethimes the value after const
, also contains variables of previous constructed variables. Or maybe, it is possible, but I am not sure. So that's my question.
Upvotes: 0
Views: 833
Reputation: 473
The randomnumber
variable isn't shared between files, unless it is explicitly imported in the config.js. In order to use that variable, it either needs to be declared in the config.js:
const randomnumber = 100;
exports.messages = {
testmessage: `hello world: ${randomnumber}`
}
or, it can be shared using node imports and exports:
exports.randomnumber = 100
config.js:
const { randomnumber } = require('./otherfile.js');
exports.messages = {
testmessage: `hello world: ${randomnumber}`
}
Upvotes: 1