I am currently working on setting up a discord bot to send notifications when someone joins a voice chat. While coding in Atom, I encountered the following error:
TypeError: Cannot read property 'send' of undefined
at Client.client.on (C:\Users\Franco\Desktop\bot\bot.js:15:20)
at emitTwo (events.js:126:13)
at Client.emit (events.js:214:7)
at VoiceStateUpdateHandler.handle (C:\Users\Franco\Desktop\bot\node_modules\discord.js\src\client\websocket\packets\handlers\VoiceStateUpdate.js:39:16)
at WebSocketPacketManager.handle (C:\Users\Franco\Desktop\bot\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
at WebSocketConnection.onPacket (C:\Users\Franco\Desktop\bot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
at WebSocketConnection.onMessage (C:\Users\Franco\Desktop\bot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
at WebSocket.onMessage (C:\Users\Franco\Desktop\bot\node_modules\ws\lib\event-target.js:120:16)
at emitOne (events.js:116:13)
at WebSocket.emit (events.js:211:7)
This is the code snippet I attempted:
client.on("message", function(message) {
client.on('voiceStateUpdate', (oldMember, newMember) => {
console.log('lol');
let newUserChannel = newMember.voiceChannel
let oldUserChannel = oldMember.voiceChannel
var channel = client.channels.get('353996293154144259');
if(oldUserChannel === 353996293154144260 && newUserChannel !== 489674797261783041) {
channel.send('has joined a voice channel');
} else if(newUserChannel === 489674797261783041){
channel.send('has left a voice channel');
}
})
})
The console.log statement was just for testing purposes.
When trying to run this code, I faced the error:
"TypeError: Cannot read property 'send' of undefined"
Upon my analysis, the issue lies with the undefined nature of the .send function.
I gathered most of my information from the following sources:
Here are the troubleshooting steps I have attempted:
Added
client.on("message", function(message) {
Changed
.send
to.message
and.sendMessage
Tried to define .send again
Modified client.channels.get to lient.channelid.get
Changed or removed
var channel = client.channels.get('353996293154144259');
Moved
outside ofclient.on('voiceStateUpdate', (oldMember, newMember) => {
. However, this resulted in another error.client.on("message", function(message) {
"Second error I encountered after moving client.on out: "(node:18196) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 message listeners added. Use emitter.setMaxListeners() to increase limit"
Additional notes:
353996293154144259 = id of General chat on discord server
353996293154144260 = id of the first voice chat
489674797261783041 = id of the second voice chat
UPDATE
Progress has been made by replacing
channel.send('has joined a voice channel');
with message.channel.send('has joined a voice channel');
. However, the desired outcome is yet to be achieved.
Special thanks to YakovL for assisting in enhancing the quality of this post.