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] })
}
}
}
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.