왜 그런지 모르겠어
this.client는 OnDiscordReady () 함수에서 액세스하려고 할 때 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 객체에 액세스하는 능력을 제외하고는 모든 것이 작동하는 것 같습니다. 봇은 로그인 한 다음 '준비'를 표시 한 다음 모든 것이 작동하지 않습니다.
this.client = 대신 생성자에서 Object.assign (this, client : new Discord.Client ())를 사용해 보았습니다.
하지만 그것은 도움이되지 않았습니다.
내가 여기서 무엇을 놓치고 있습니까?
클래스 메서드에는 자체 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();
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다