2
votes

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:

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 of client.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.

3
As I understand from the code, client.channels.get('353996293154144259'); returns unidentified.Gurkan İlleez
If i try without that line i keep getting the same error, Even if i replace itfranco BlackWP
Maybe 353996293154144259 does not presents in channelGurkan İlleez
yes its present, the 353996293154144259 its the general chat id of my discord test serverfranco BlackWP
Do me a flavor, add a line client.channels.forEach((i,j)=>{console.log(j)}), see if there really an object with id you provided.MT-FreeHK

3 Answers

1
votes
var channel = client.channels.get('353996293154144259');

this didn't work for me so I used this instead:

const channel = client.channels.cache.filter((channel) => channel.name === 'name').first();

and it would be better to use this but searching for the ID:

const channel = client.channels.cache.filter((channel) => channel.id === '353996293154144259').first();
0
votes

Replace

var channel = client.channelID.get('353996293154144259');

with

var channel = client.channels.get('353996293154144259');

Edit: As of v12, it is

var channel = client.channels.cache.get('353996293154144259');
-1
votes

I am Brazilian, my language is Brazilian Portuguese!

const Discord = require('discord.js'),
  client = new Discord.Client(),
  colors = require('colors/safe');

client.on('voiceStateUpdate', (oldMember, newMember) => {
  
  let newUserChannel = newMember.channelID;
  let oldUserChannel = oldMember.channelID;
  let userid = newMember.id;

// ----------- verificação --------------

  let selfmute = newMember.mute;
  let mute = newMember.selfMute;
  let selfmute1 = oldMember.mute;
  let mute1 = oldMember.selfMute;
  let selfdeaf = newMember.selfDeaf;
  let selfdeaf1 = oldMember.selfDeaf;  
  let selfvid = newMember.selfVideo;
  let selfvid1 = oldMember.selfVideo;
  let deaf = newMember.deaf;
  let deaf1 = oldMember.deaf;
  let servmute = newMember.serverMute;
  let servmute1 = oldMember.serverMute;

  if ( selfmute == true ) return;
  if ( mute == true ) return;
  if ( selfmute1 == true ) return;
  if ( mute1 == true ) return;
  if ( selfdeaf == true ) return;
  if ( selfdeaf1 == true ) return;
  if ( selfvid == true ) return;
  if ( selfvid1 == true ) return;
  if ( deaf == true ) return;
  if ( deaf1 == true ) return;
  if ( servmute == true ) return;
  if ( servmute1 == true ) return;

// -------- definindo cores ----------

var a = colors.red('[-] ');
var b = colors.green('[+] ');
var c = colors.blue('[!] ');

// -------- menssagem / log ----------

       if ( newUserChannel === "827668874542579797") { //id voice channel

          console.log(c+"- id: "+userid)
          console.log(b+"- Entrou em: "+newUserChannel);   

       } else if ( oldUserChannel === "827668874542579797") { //id voice channel
           
           console.log(c+"- id: "+userid)
           console.log(a+"- Saiu de: "+oldUserChannel);

       } else {

           console.log(c+"- Ratos estão dialogando...");

       }
    
})
//----------------Comando pra ficar on-------------

client.login("TOKEN");