I'm currently doing a code with [AFK] when they type ${prefix}afk
, I wanted to do is after they chat again in discord the [AFK] in their nickname would be removed. So far this is only I know adding [AFK] in their nickname when they call the command
Here's my current code:
var nickname = `[AFK]`;
message.member.setNickname(nickname ' ' message.author.username);
const embed = new MessageEmbed()
.setAuthor({iconURL: `${message.author.avatarURL()}`,name: `${message.member.user.tag}`})
.setDescription(`${message.author.toString()}` " is now AFK")
.setTimestamp()
.setColor('RANDOM')
return message.channel.send({embeds: [embed]})
//I wanted to do the next line when the person chat again [AFK] will be removed
${prefix}afk
= [AFK] Username
after chatting again [AFK] Username
= Username
Any tips on how to do it? I'm using discord js v13 and slappey
This is the full code I'm doing right now:
module.exports = class TestCommand extends BaseCommand {
constructor() {
super('test', 'testing', []);
}
async run(client, message, args) {
// if(!message.member.permissions.has("ADMINISTRATOR")) return message.channel.send("You can't use this command.");
// if(!message.guild.me.permissions.has("ADMINISTRATOR")) return message.channel.send("My role does not have the manage channels permission");
const { MessageEmbed, Collection } = require('discord.js');
var nickname = `[AFK]`;
message.member.setNickname(nickname ' ' message.author.username);
const embed = new MessageEmbed()
.setAuthor({iconURL: `${message.author.avatarURL()}`,name: `${message.member.user.tag}`})
.setDescription(`${message.author.toString()}` " is now AFK")
.setTimestamp()
.setColor('RANDOM')
return message.channel.send({embeds: [embed]})
}
}
Take note that I'm using slappey package to do my bot.
CodePudding user response:
In order to remove a member's nickname, you can simply set their nickname as null
. Example:
message.member.setNickname(null);
This is a much simpler and efficient method of removing a guild member's nickname.
CodePudding user response:
If the username includes "[AFK]" you can remove the AFK tag from the user.
Here's an example for you:
const nickname = `[AFK]`
if (message.author.username.includes(nickname)) {
const newNickname = message.author.username.split(nickname)[1]
message.member.setNickname(newNickname)
} else {
message.member.setNickname(nickname, message.author.username)
const embed = new MessageEmbed()
.setAuthor({iconURL: `${message.author.avatarURL()}`, name: `${message.member.user.tag}`})
.setDescription(`${message.author.toString()}` " is now AFK")
.setTimestamp()
.setColor('RANDOM')
return message.channel.send({embeds: [embed]})
}
NOTE: As you want user has to write ${prefix}afk
command again to remove his AFK tag.
EDIT:
You need to add this code to your index.js file
client.on("messageCreate", async (message) => {
if (message.author.username.includes("[AFK]")) {
const newNickname = message.author.username.split("[AFK]")[1]
message.member.setNickname(newNickname)
}
});