Is there a way to check if a specific user id exists within the server? without using cache
I couldn’t find a working code, whether or not the user had never chatted
commands.add('test3', (arg) =>{
var exists = bot.users.cache.has(arg);
if (exists == true) { msg.channel.send('1'); }else { msg.channel.send('2');} //msg.sme{e
});
This is a temporary command for testing. When user send a command with @anyuser, the bot needs to make sure the user is on the server first (otherwise it could be crashed) but nothing I tried worked properly…
Is there a better way than this?
2
Answers
Try to fetch the member through the guild member manager. An error will be thrown if the member does not exist. Use a try/catch block to handle the possible error.
You can use guild.members.fetch(userID) to fetch a member from the server by their user ID. If the user exists, it will return the member otherwise, it will throw an error.