Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

277
Views
DiscordJS Cannot read properties of null (reading 'id')

I am attempting to log when someone joins vc and moves vc, the moving vc logging works well but when joining a vc it gives the error TypeError: Cannot read properties of null (reading 'id')

module.exports = async (client, oldState, newState) => {
        if (oldState.channel && newState.channel && oldState.channel.id !== newState.channel.id) {
            const embed = new Discord.MessageEmbed()
                .setAuthor({ name: newState.member.user.tag, iconURL: newState.member.user.displayAvatarURL({ dynamic: true }) })
                .setTimestamp()
                .setColor(config.colour)
                .setFooter({ text: newState.guild.name, iconURL: newState.guild.iconURL({ dynamic: true }) })
                .setDescription(`**${newState.member} moved from \`${oldState.channel.name}\` to \`${newState.channel.name}\`**`)
            return logChannel.send({ embeds: [embed] })
        }
        if (!oldState.channel.id && newState.channel.id) {
            const embed = new Discord.MessageEmbed()
                .setAuthor({ name: newState.member.user.tag, iconURL: newState.member.user.displayAvatarURL({ dynamic: true }) })
                .setTimestamp()
                .setColor(config.colour)
                .setFooter({ text: newState.guild.name, iconURL: newState.guild.iconURL({ dynamic: true }) })
                .setDescription(`**๐Ÿ”Š ${newState.member} has joined \`${newState.channel.name}\` channel.**`)
            return logChannel.send({ embeds: [embed] })
        }
    }
}
about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

When the first time joining the channel, there's no oldState.channel so bot can't fetch id of a channel which is not exist. You can edit your statements like;

if (oldState.channel && newState.channel && oldState.channel.id !== newState.channel.id)

to

if(oldState.channel && newState.channel){
if(oldState.channel.id != newState.channel.id){
 // rest of code
}
}

maybe you can make more clean but this is how it works.

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!