Home > Software engineering >  Discord.js v13 How to check in which vc someone is?
Discord.js v13 How to check in which vc someone is?

Time:02-26

I have this 'wake up' command for my bot which is supposed to move the metnioned member between 2 specific voice chats and afterwards move them back to their original vc. I managed to get the bot to move me between those 2 vc's but I have no idea how to check in which vc the user was before nmoving. I tried searching online and going through the documentation but I can't figure it out. Here's my code:

const { Permissions,MessageEmbed } = require('discord.js')

module.exports = {
    name: 'pobudka',
    description: "Budzi gracza.",
    execute(message, args)
    {
        const target = message.mentions.members.first()
        const targetVC = target.voiceState.channel;
        const usageEmbed = new MessageEmbed() //the embed sent if the usage is incorrect
            .setColor('ff0004')
            .setTitle('Poprawne użycie')
            .addField('rp pobudka @user', 'zamienić @user na wzmiankę użytkownika')

        if(message.member.permissions.has(Permissions.FLAGS.ADMINISTRATOR)) //checking if the invoking member has the administrator perms to use the command
        {
            if(target.voiceState === 'null')
            {
                message.reply('Twój cel nie jest na kanale głosowym!')
            }
            else
            {
                if(target)
                {
                    for(let i = 5; i > 0; i--) //loop for changing the voice channels 5 times 
                    { 
                        target.voice.setChannel("940573437561303080") //moving the member to a different voice channel
                        target.voice.setChannel("946790140003631165") //moving the member to a different voice channel
                    }
                    target.voice.setChannel(targetVC)
                }
                else
                {
                    message.reply({ embeds: [usageEmbed]}) //sending the usage embed
                }
            }
        }
        else
        {
            message.reply("Nie możesz tego użyć.") //replying if the invoking member doesnt have administrator
        }
    }
}

Here's the error I'm getting:

C:\Users\Miki\Desktop\dc bots\jajco bot smol\commands\pobudka.js:9
        const targetVC = target.voiceState.channel;
                                           ^

TypeError: Cannot read properties of undefined (reading 'channel')
    at Object.execute (C:\Users\Miki\Desktop\dc bots\jajco bot smol\commands\pobudka.js:9:44)
    at Client.<anonymous> (C:\Users\Miki\Desktop\dc bots\jajco bot smol\main.js:134:44)
    at Client.emit (node:events:520:28)
    at MessageCreateAction.handle (C:\Users\Miki\Desktop\dc bots\jajco bot smol\node_modules\discord.js\src\client\actions\MessageCreate.js:26:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\Miki\Desktop\dc bots\jajco bot smol\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\Miki\Desktop\dc bots\jajco bot smol\node_modules\discord.js\src\client\websocket\WebSocketManager.js:351:31)
    at WebSocketShard.onPacket (C:\Users\Miki\Desktop\dc bots\jajco bot smol\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\Miki\Desktop\dc bots\jajco bot smol\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\Miki\Desktop\dc bots\jajco bot smol\node_modules\ws\lib\event-target.js:199:18)

CodePudding user response:

The property is named voice, not voiceState

const targetVC = target.voice.channel

You can use the docs to see properties of certain classes

  • Related