Home > Software engineering >  Discord js v13 setChannel is undefined
Discord js v13 setChannel is undefined

Time:11-13

I'm started to learn discord.js library and trying to make event when user joins special voice channel and bot creates a new one and moves user. Now bot can create channel, but when it tries to move user it have an error "Cannot read properties of undefined (reading 'setChannel')"

Here is my code:

const {Collection} = require('discord.js')
let privateVoice = new Collection()
config = require('../config.json');

module.exports = async (bot, oldState, newState)=>{
  const user = await bot.users.fetch(newState.id)
  const member = newState.guild.members.fetch(user)
  if(!oldState.channel && newState.channel.id === (config.createChannel)){
    const channel = await newState.guild.channels.create(user.tag,{
      type: "GUILD_VOICE",
      parent: newState.channel.parent
    })
      member.voice.setChannel(channel);
      privateVoice.set(user.id, channel.id)
  }
    };

CodePudding user response:

You are trying to fetch a member by their user object and you aren't even awaiting it. .fetch is a Promise and you use their ID, not their user object.

Instead of using this to get their member object:

const member = newState.guild.members.fetch(user)

Use VoiceState.member

const { member } = newState //object destructuring for cleaner syntax. 'const member = newState.member' is also fine

But this can be null since it gets them from the member cache (see here). If you really want to fetch them, make sure to await it and use their ID

const member = await newState.guild.members.fetch(user.id)
  • Related