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

71
Views
Mention users with specific role in bot status

Sooo, hello to everyone who sees this, it's my first time posting anything on StackOverflow. Anyway, recently I started learning discord.js and I managed to create a small bot in v12 with automatically changing status. Here is the code:

setInterval(() => {
    const statuses = [
      `user 1`,
      `user 2`,
      `user 3...`
    ]

    const status = statuses[Math.floor(Math.random() * statuses.length)]
    client.user.setActivity(status, { type: "WATCHING" })
  }, 5000)

The point is to have certain members usernames appear in bot status and this is really nice if the server has like just a few of these "special" members but now my server became larger and I would like for bot to target ONE specific role and list all the members with that "special" role :) Please help me, thank you in advance

So after Elitezen tried to help me I did this:

I added async in client on ready:

client.on("ready", async () => {
  const guild = client.guilds.cache.get("SERVER_ID");

I added your code:

const role = guild.roles.cache.get('SERVER_ID');

  await guild.members.fetch();

setInterval(() => {
    const statuses = Array
       .from(role.members.cache.values())
       .map(member => member.displayName);

    const status = statuses[Math.floor(Math.random() * statuses.length)]
    client.user.setActivity(status, { type: "WATCHING" })
  }, 5000)

And now Im stuck with this error: TypeError: Cannot read property 'values' of undefined

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

An example to target your desired role using RoleManager#get() or RoleManager#find()

const role = <Guild>.roles.cache.find(r => r.name == 'role name');
// Or
const role = <Guild>.roles.cache.get('role id');

To get all the members that hold this role fetch all members of the guild first, then construct an array from the Collection's values.

Then map the array of members to their display name (or user's username)

Ensure you have the guild member's intent enabled if you want to fetch all members.

await <Guild>.members.fetch();

setInterval(() => {
    const statuses = Array
       .from(role.members.values());
       .map(member => member.displayName);

    const status = statuses[Math.floor(Math.random() * statuses.length)]
    client.user.setActivity(status, { type: "WATCHING" })
  }, 5000)
over 4 years ago · Santiago Trujillo Report

0

So, thanks to Elitezen I managed to solve my problem.

After making client on ready an async function

client.on("ready", async () => {
const guild = client.guilds.cache.get("SERVER_ID");

This is the final script for mentioning users with a certain role in bot status:

const role = guild.roles.cache.get('ROLE_ID');

await guild.members.fetch();

setInterval(() => {
    const statuses = Array
       .from(role.members.values())
       .map(member => member.displayName);

    const status = statuses[Math.floor(Math.random() * statuses.length)]
    client.user.setActivity(status, { type: "WATCHING" })
}, 5000)
over 4 years ago · Santiago Trujillo 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!