TypeError:无法读取未定义的属性“用户”

时间:2019-10-15 05:08:06

标签: node.js discord.js

我不知道为什么

当我尝试从OnDiscordReady()函数访问它时,this.client返回undefined。 机器人

错误消息


/home/xcomegax/NCerberus/services/discordS.js:21
        Logger.log(`Discord client logged in as ${this.client.user.tag}`);
                                                              ^

TypeError: Cannot read property 'user' of undefined
    at Client.OnDiscordReady (/home/xcomegax/NCerberus/services/discordS.js:21:63)
    at Client.emit (events.js:203:15)
    at WebSocketConnection.triggerReady (/home/xcomegax/NCerberus/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:125:17)
    at WebSocketConnection.checkIfReady (/home/xcomegax/NCerberus/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:141:61)
    at GuildCreateHandler.handle (/home/xcomegax/NCerberus/node_modules/discord.js/src/client/websocket/packets/handlers/GuildCreate.js:13:31)
    at WebSocketPacketManager.handle (/home/xcomegax/NCerberus/node_modules/discord.js/src/client/websocket/packets/WebSocketPacketManager.js:105:65)
    at WebSocketConnection.onPacket (/home/xcomegax/NCerberus/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (/home/xcomegax/NCerberus/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:296:17)
    at WebSocket.onMessage (/home/xcomegax/NCerberus/node_modules/ws/lib/event-target.js:120:16)
    at WebSocket.emit (events.js:198:13)
    at Receiver.receiverOnMessage (/home/xcomegax/NCerberus/node_modules/ws/lib/websocket.js:789:20)
    at Receiver.emit (events.js:198:13)
    at Receiver.dataMessage (/home/xcomegax/NCerberus/node_modules/ws/lib/receiver.js:413:14)
    at Receiver.getData (/home/xcomegax/NCerberus/node_modules/ws/lib/receiver.js:352:17)
    at Receiver.startLoop (/home/xcomegax/NCerberus/node_modules/ws/lib/receiver.js:138:22)
    at Receiver._write (/home/xcomegax/NCerberus/node_modules/ws/lib/receiver.js:74:10)

我正在尝试调试的代码>:

const EventEmitter = require('events');
const Logger = require('./loggerS');
const Discord = require('discord.js');
require('dotenv').config();

class discordS extends EventEmitter{
 
    constructor(){
        super();
        this.client = new Discord.Client();
        this.client.on('ready',this.OnDiscordReady);
        this.client.on('message',this.OnDiscordMessage);
        this.client.login(process.env.DiscordAuthToken);
    }

    OnDiscordMessage(msg){
        if(msg.author == this.client.author) return;
    }

    OnDiscordReady(){
        Logger.log(`Discord client logged in as ${this.client.user.tag}`);
    }

}

module.exports = new discordS();

除了我能够访问this.client对象的能力之外,其他所有事情似乎都可以正常工作。 僵尸程序登录后发出“就绪”的消息,然后一切都不起作用

我尝试在构造函数中使用Object.assign(this,client:new Discord.Client()),而不仅仅是this.client =

但这并没有任何帮助。

我在这里想念什么?

1 个答案:

答案 0 :(得分:1)

您的类方法有自己的this范围。这就是未定义this.client.user的原因,因为此this不是类的this。要更正它,您可以在构造函数中.bind(this)内使用的各个函数,也可以简单地使用箭头函数获取类本身的词汇this

const EventEmitter = require('events');
const Logger = require('./loggerS');
const Discord = require('discord.js');
require('dotenv').config();

class discordS extends EventEmitter{

    constructor(){
        super();
        this.client = new Discord.Client();
        this.client.on('ready',this.OnDiscordReady);
        this.client.on('message',this.OnDiscordMessage);
        this.client.login(process.env.DiscordAuthToken);
    }

    OnDiscordMessage = (msg) => { //arrow function
        if(msg.author == this.client.author) return;
    }

    OnDiscordReady =() => { //arrow function
        Logger.log(`Discord client logged in as ${this.client.user.tag}`);
    }

}

module.exports = new discordS();