0

I have been trying to check if a certain user ID exists in the current guild,

I have been looking at countless stack overflow questions and documentations hoping i could find the answer but nothing I try actually works.

i use Discord.js V13 with the latest node.js version

const server = client.guilds.fetch( message.guild.id );
if ( !server.member(args[0]) ) return commandFailed('a user with this ID does not exist');
// args[0] contains the userID

could someone please help me with this.

Ralkey
  • 19
  • 1
  • 8

2 Answers2

2

First: fetch returns a promise, you must handle that promise. However, you don't need to fetch the server since the server is already accessible through message.guild.

Second: Guild#member() is deprecated, try fetching the member directly and checking if a member returned.

I'll be using Async/Await for this example, make sure you're inside an Async function.

// const server = await client.guilds.fetch(message.guild.id); is not needed

const server = message.guild;

const member = await server.members.fetch(args[0]);

if (!member) return commandFailed('a user with this ID does not exist');
Elitezen
  • 6,551
  • 6
  • 15
  • 29
  • thank you this worked. but now i have the problem that this piece of code `memberTarget = message.guild.members.cache.get( args[0] );` returns undefined, which is weird since this works perfectly with getting the mentioned user's id – Ralkey Aug 28 '21 at 16:47
  • `.get()` checks the cache, returns undefined if whatever you're trying to access is *not cached*, it's best to always use `fetch()` and not rely on the cache.Whenever a member is mentioned they are instantly cached which is why you are able to use `get()` on them – Elitezen Aug 28 '21 at 17:11
  • when i change get() to fetch() i get this error `TypeError: message.guild.members.cache.fetch is not a function`. is there any reason for this? – Ralkey Aug 28 '21 at 17:39
  • last question. is there a way to get the data without getting a promise? i am not very good at implementing promises as i have no idea how it works... – Ralkey Aug 28 '21 at 18:19
  • No, it's data retrieved through API calls. If you want to fully learn Discord.JS you will need to get the hang of handling promises. They may be intimidating but study, watch a tutorial or 2 and you'll get the hang of it – Elitezen Aug 28 '21 at 18:24
  • I have successfully managed to resolve the promise and get the data. But the promise is resolved AFTER the rest of the code is done. Is there a way to make the promise resolve immediately. Because I am currently using functions to call the rest of the code after the promise is resolved, and I don't like that "cheaty" method. – Ralkey Aug 28 '21 at 20:35
  • If you have a new issue consider posting a new question – Elitezen Aug 28 '21 at 20:50
0

Take a look at this thread, which explains exactly what you want:

let guild = client.guilds.get('guild ID here'),
  USER_ID = '123123123';

if (guild.member(USER_ID)) {
  // there is a GuildMember with that ID
}
WoJo
  • 500
  • 1
  • 3
  • 20
  • i have already tried that thread and it returns this error. TypeError: client.guilds.get is not a function. could be because that thread is from 2018 – Ralkey Aug 28 '21 at 15:11
  • What about this: https://stackoverflow.com/a/65853663/11943314 – WoJo Aug 28 '21 at 15:44