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

302
Views
how to remove a AFK from the nickname without say the prefix

so I make a simple AFK command like this

client.on('message', message => {
  if (message.content.includes('!afk')) {
      message.member.setNickname(`[AFK] ${message.member.displayName} `).catch(error => console.log(error));
  }
  
  if (message.content === '!back') {
    message.member.setNickname(message.member.displayName.replace('[AFK] ', )).catch(error => console.log(error));

  }

 
  
  
});

and I want If the user is afk and then user chat like "hi" the [AFK] is gone without say the prefix "!back"

How can I do that?

Thank You for helping me (:

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

0

If I understand correctly, you are asking to delete the AFK status when the player types something again. I made a rough solution using JS Set that should work well with a small set of users, but it may require additional tweaks for large servers.

Basically, when a player types !afk it is included in the set of AFK players and when he/she/it comes back and types something, its name is deleted from the set and its nickname updated. I am not sure if the message.member.id exists and it is the correct field to track the player (EDIT: Yes, it is), but the main idea is to keep record of something unique for each afk player.

    const afkPlayers = new Set();
    
    client.on('message', message => {
      if (message.content.includes('!afk')) {
          afkPlayers.add(message.member.id);
          message.member.setNickname(`[AFK] ${message.member.displayName} `).catch(error => console.log(error));
      }
      
      if(afkPlayers.member.has(message.member.id)){
        afkPlayers.member.delete(message.member.id);
        message.member.setNickname(message.member.displayName.replace('[AFK] ', )).catch(error => console.log(error));
      }
      
});
about 4 years ago · Juan Pablo Isaza Report

0

I don't really know how Discord works and when exactly the "message" event is triggered, but it seems to me that you could probably do it even without the set, as proposed by @TheCave3:

client.on('message', message => {
  const pref='[AFK] ';
  if (message.content.includes('!afk'))
    message.member.setNickname(pref+message.member.displayName).catch(error => console.log(error));
  else if (message.member.displayName.slice(0,pref.length)===pref)
    message.member.setNickname(message.member.displayName.slice(pref.length)).catch(error => console.log(error));
});
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!