Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

45
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 (:

7 months 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));
      }
      
});
7 months 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));
});
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs