The reason user
is always undefined because you are calling it out of scope.
if(args.length < 1) {
var user = message.author; // the user variable is only valid in this "if" block
} else {
var user = client.users.cache.get(args[0]); // the user variable is only valid in this "else" block
}
if(!user || user == undefined) return message.channel.send("**Error:** An error occurred!");
Instead, you should include your code in the main if-else
block or declare the user
variable global by not using a variable declaration keyword.
if(args.length < 1) {
user = message.author; // the user variable is only valid in this "if" block
} else {
user = client.users.cache.get(args[0]); // the user variable is only valid in this "else" block
}
if(!user) return message.channel.send("**Error:** An error occurred!");
console.log(user.tag)
//further code here...
I've also cleaned up some redundancies for you: if(!user)
will cover undefined
as well, so no need to include || user == undefined
args[0]
. is it a pinged object or id of a user? – Barış Çiçekmessage.mentions.user.first
. – Rak Laptudirm