Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

474
Views
Discord.js how to check if user has a role on a specific server

I'm trying to check if the user has a role on a specific server regardless on which server they use the bot.

For example, if I have 2 servers, server A and server B, I want to check if the user has the role "Beginner" on server A, even if I use the command in server B.

I couldn't find a way to do this on the internet, message.member.roles seems to only return the roles of the server the command was written in.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can do it like this:

function hasRole(guildId, roleId) {
    // Get second guild
    const guild = client.guilds.cache.get(guildId);

    // Get member in that guild, by ID
    const member = guild.members.cache.get(message.author.id);

    // If member is in that guild,
    if (member) {
        // return whether they have this role
        return member.roles.cache.has(roleId);
    }
}

Of course the bot has to be in both servers.

about 4 years ago · Juan Pablo Isaza Report

0

A solution is to find the server then find the member from inside the server.
To find the guild you will use the guild.fetch(...) method and from there you then use the guildMember.fetch(...) function to find the member.

Example:

client.on("messageCreate", function(message) {
    // Find the guild
    let guild = client.guilds.fetch("guild-id");
    // Find the member in the guild
    let member = guild.members.fetch(message.author.id);

    if (member)
        message.reply("You are in the server " + guild.name);
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!