Discord client object is undefined after being exported

六眼飞鱼酱① 提交于 2021-01-29 13:44:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!