Ralkey
Ralkey

Reputation: 27

Discord.js V13 dont know how to check if a user ID is in the current guild

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.

Upvotes: 0

Views: 4029

Answers (2)

Elitezen
Elitezen

Reputation: 6730

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');

Upvotes: 2

WoJo
WoJo

Reputation: 514

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
}

Upvotes: 0

Related Questions