I'm trying to get this discord bot work. The idea is simple, make the bot send a notification when someone joins in the voice chat. Im making this on Atom, the error i got is this one:
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)
the code attempt:
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');
// User Joins a voice channel
} else if(newUserChannel === 489674797261783041){
channel.send('has left a voice channel');
// User leaves a voice channel
}
})
})
The console.log is just for testing if voiceStateUpdate
is working.
When I try to run it I get this error:
"TypeError: Cannot read property 'send' of undefined"
As i understand the error is because .send
function is undefined
i got almost everything from here:
- https://www.reddit.com/r/discordapp/comments/6p85uf/discordjs_any_way_to_detect_when_someone_enter_a/
- https://discord.js.org/#/docs/main/stable/general/welcome
Everything I've tried:
Add
client.on("message", function(message) {
Edit
.send
to.message
and.sendMessage
try to define .send
change client.channels.get to lient.channelid.get
Change/delete
var channel = client.channels.get('353996293154144259');
Move
client.on('voiceStateUpdate', (oldMember, newMember) => {
out ofclient.on("message", function(message) {
When i do that i got another error
"Second error i got when i move client.on out: "(node:18196) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 message listeners added. Use emitter.setMaxListeners() to increase limit"
Maybe some useless info:
353996293154144259 = id of General chat on discord server
353996293154144260 = id of the first voice chat
489674797261783041 = id of the second voice chat
UPDATE
Okay, i solved part of the problem, i need use message.channel.send('has joined a voice channel');
instead of channel.send('has joined a voice channel');
But i can't make it work as i want.
Special thanks for YakovL. He helps me to make this post more complete and better.
client.channels.forEach((i,j)=>{console.log(j)})
, see if there really an object with id you provided. – MT-FreeHK