LoginSignup

Is there a way to check if a specific user id exists within the server? without using cache

Asked 2 months agoAnswer 2Votes 0

0

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?

Is there a way to check if a specific user id exists within the server? without using cache


2 Answers

0

No



0

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.

` async function exists(guild, arg) { try { const member = await guild.members.fetch(arg); return !!member; } catch (err) { console.error(err); return false; } }

`




Your Answer