Discord 客户端对象在导出后未定义

Discord client object is undefined after being exported(Discord 客户端对象在导出后未定义)

本文介绍了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 客户端对象在导出后未定义

基础教程推荐