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 (:
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 (EDIT: Yes, it is), but the main idea is to keep record of something unique for each afk player.message.member.id
exists and it is the correct field to track the 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));
}
});
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));
});