1

I'm trying to make my bot mention my server staff in a specific text channel when someone enters in the voice support waiting room.

Here's the script I use:

const { DiscordAPIError } = require('discord.js');
const BaseEvent = require('../../utils/structures/BaseEvent');
const Discord = require("discord.js");
const client = new Discord.Client();

module.exports = class ReadyEvent extends BaseEvent {
  constructor() {
    super('ready');
  }
  async run (client) {
  console.log(`Bot prêt, connecté en tant qu'${client.user.username}!`);
  }
}

client.on('voiceStateUpdate', (newMember) => {
  const newUserChannel = newMember.voice.channelID
  const textChannel = client.channels.cache.get('815316823275995139')

  if(newUserChannel === '815316796067414016') {
    textChannel.send(`worked`)
  }
})

There's no error in the console and when I'm joining the voice support channel, nothing happens.

This:

textChannel.send(`worked`)

is for test purposes, the line I use is

textChannel.send(`Hey ! Le <@&${753367852299321404}>, ${newMember.user.username} (${newMember.id}) est en Attente de Support !`)

The first part of the script that logs me the script is working ... is working, so I'm sure the script is correctly loaded, the bot is on my server and have all the permissions he needs.

Console and script screen

My discord.js version is 12.5.3

EDIT:

Yes, now I can see the log, so I put back my script to detect and send a message:

const { DiscordAPIError } = require('discord.js');
const BaseEvent = require('../../utils/structures/BaseEvent');
const Discord = require("discord.js");
const client = new Discord.Client();

module.exports = class ReadyEvent extends BaseEvent {
  constructor() {
    super('ready');
  }
  async run (client) {
  console.log(`Bot prêt, connecté en tant qu'${client.user.username}!`);
  client.on('voiceStateUpdate', (newState) => {
    const newUserChannel = newState.voice.channelID
    const textChannel = client.channels.cache.get('815316823275995139')
  
    if(newUserChannel === '815316796067414016') {
      textChannel.send(`working`)
    }
  }); 
  }
}

but I have this error:


D:\DiscordBot\Ariabot\src\events\ready\ReadyEvent.js:13
    const newUserChannel = newState.voice.channelID
                                          ^

TypeError: Cannot read property 'channelID' of undefined
    at Client.<anonymous> (D:\DiscordBot\Ariabot\src\events\ready\ReadyEvent.js:13:43)
    at Client.emit (events.js:376:20)
    at VoiceStateUpdate.handle (D:\DiscordBot\Ariabot\node_modules\discord.js\src\client\actions\VoiceStateUpdate.js:40:14)
    at Object.module.exports [as VOICE_STATE_UPDATE] (D:\DiscordBot\Ariabot\node_modules\discord.js\src\client\websocket\handlers\VOICE_STATE_UPDATE.js:4:35)
    at WebSocketManager.handlePacket (D:\DiscordBot\Ariabot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (D:\DiscordBot\Ariabot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (D:\DiscordBot\Ariabot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (D:\DiscordBot\Ariabot\node_modules\ws\lib\event-target.js:132:16)
    at WebSocket.emit (events.js:376:20)
    at Receiver.receiverOnMessage (D:\DiscordBot\Ariabot\node_modules\ws\lib\websocket.js:834:20)
[nodemon] app crashed - waiting for file changes before starting...
5
  • Have you tried logging to the console when the voiceStateUpdate event is triggered? Does that work, and if yes, can you log user data when they join?
    – LaytonGB
    Jun 10, 2021 at 12:51
  • 1
    Thank's for the answer @LaytonGB I just tried with this: ``` client.on('voiceStateUpdate', voice => { console.log(voice); }); ``` and nothing happens in the console Jun 10, 2021 at 13:10
  • Definitely need to get that working before anything else, though your code is correct from what I can tell. Try moving voiceStateUpdate event setup inside the async run (client) section? That way it should run when the ready event fires, not before. May fix everything.
    – LaytonGB
    Jun 10, 2021 at 13:20
  • 1
    Thank's again @LaytonGB, I'm progressing, now I see the logs and put my script back, but now, I have an error, I edited my post to add all the info. Jun 10, 2021 at 13:42
  • As the error says, Cannot read property 'channelID' of undefined, meaning newState does not have a voice property. After looking online I found this which shows your function should have two inputs, (oldMember, newMember) => {} (see line 384) and you are probably trying to refer to newMember (whereas your current code uses newState to refer to oldMember). From what I can find, using client.on('voiceStateUpdate', (oldMember, newMember) => { const newUserChannel = newMember.voice.channelID; ... } should work.
    – LaytonGB
    Jun 10, 2021 at 15:38

2 Answers 2

0

The voiceStateUpdate event calls the callback with two VoiceStates. (oldState and newState)

You should use the newState property for this purpose.

A VoiceState does not contain a voice property but does contain a channelID property.

Thus, your code should look something like this:

const { DiscordAPIError } = require('discord.js');
const BaseEvent = require('../../utils/structures/BaseEvent');
const Discord = require("discord.js");
const client = new Discord.Client();

module.exports = class ReadyEvent extends BaseEvent {
  constructor() {
    super('ready');
  }
  async run (client) {
  console.log(`Bot prêt, connecté en tant qu'${client.user.username}!`);

  // use 2 parameters
  client.on('voiceStateUpdate', (oldState, newState) => {
    // use the .channelID property (.voice doesn't exist)
    const newUserChannel = newState.channelID;
    const textChannel = client.channels.cache.get('815316823275995139');
  
    if(newUserChannel === '815316796067414016') {
      textChannel.send("working");
    }
  }); 
  }
}

When calling functions, the name of the parameter does not matter, the position determines what is assigned to those variables.

For example, in the code above, I could have named the variables foo and bar (instead of oldState and newState), and it would have still worked.

0

Thank you so much to you two it's now working fine.

If the script can be usefull to someone passing-by:

const { DiscordAPIError } = require('discord.js');
const BaseEvent = require('../../utils/structures/BaseEvent');
const Discord = require("discord.js");
const client = new Discord.Client();

module.exports = class ReadyEvent extends BaseEvent {
  constructor() {
    super('ready');
  }
  async run (client) {
  console.log(`Bot ready, connected as ${client.user.username}!`);

  client.on('voiceStateUpdate', (oldState, newState) => {
    const newUserChannel = newState.channelID;
    const textChannel = client.channels.cache.get('852643030477307995');
// 852643030477307995 = ID of the text channel, I use, where the message will be posted
  
    if(newUserChannel === '759339336663040020') {
// 759339336663040020 = ID of the voice channel I want to lookup
      textChannel.send(`Hey <@&752168551103856700> ! ${newState.member} is waiting for help !`);
// <@&752168551103856700> = ID of the role I want to tag
    }
  }); 
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.