Discord client object is undefined after being exported(Discord 客户端对象在导出后未定义)
问题描述
我最近遇到了一个问题.
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");
});
在阅读了有关导出模块的内容后,我认为这就是我所要做的,但每次我尝试运行它时,它都会指出客户端未定义.它总是给我消息无法读取指向 test.js 中的侦听器的未定义的属性'on'
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
推荐答案
如果您创建和导出客户端的文件名为 main.js
,则您的 require
陈述是错误的.改成
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
这篇关于Discord 客户端对象在导出后未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Discord 客户端对象在导出后未定义
基础教程推荐
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01