【问题标题】:TypeError: Cannot read property 'user' of undefinedTypeError:无法读取未定义的属性“用户”
【发布时间】:2020-02-11 17:11:16
【问题描述】:

不知道为什么

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

错误信息


/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 =

但这没有任何帮助。

我在这里错过了什么?

【问题讨论】:

  • 这样定义OnDiscordReady 方法是否有效? OnDiscordReady = () => {
  • 你的```this```引用没有占用类的this。
  • @giuseppedeponte 是的,当我使用 ()=>{ 时它可以工作,但有没有在不使用箭头函数的情况下修复代码?
  • @Raju 我不明白?
  • 我在回答中解释了检查一下兄弟

标签: node.js discord.js


【解决方案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();

【讨论】:

  • 很高兴为您提供帮助 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-21
  • 2019-07-27
  • 2016-05-08
  • 1970-01-01
  • 1970-01-01
  • 2021-10-21
相关资源
最近更新 更多