问题
I've recently run into a problem.
For the past several weeks I have been working on some discord bots I have made and am having trouble with exporting the client object. I've been trying to export it to other files so I can hook event listeners in other files using the same object. Below's what I attempted doing.
main.js
const client = new Discord.Client(); //Defining the client
exports.squidly = client; //Attempting to export the client
test.js
const client = require('../squidly').squidly;
client.on('ready', () => {
console.log("Test");
});
After reading things about exporting modules I thought this was all I had to do but every time I try running this it states that client is undefined. It always gives me the message "cannot read property 'on' of undefined' pointing towards the listener in test.js
I've read many different sources online and none of them seemed to help me solve the issue. Any help would be appreciated. Thanks
回答1:
If the file in which you create and export the client is called main.js
, your require
statement is wrong. Change it to
const client = require('../main').squidly; //The require needs to point to the path and file name in which you export your client
来源:https://stackoverflow.com/questions/54486826/discord-client-object-is-undefined-after-being-exported