clear command discord.js code example
Example 1: clear command discordJS
client.on('message', (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content
.toLowerCase()
.slice(prefix.length)
.trim()
.split(/\s+/);
const [command, input] = args;
if (command === 'clear' || command === 'c') {
if (!message.member.hasPermission('MANAGE_MESSAGES')) {
return message.channel
.send(
"You cant use this command since you're missing `manage_messages` perm",
);
}
if (isNaN(input)) {
return message.channel
.send('enter the amount of messages that you would like to clear')
.then((sent) => {
setTimeout(() => {
sent.delete();
}, 2500);
});
}
if (Number(input) < 0) {
return message.channel
.send('enter a positive number')
.then((sent) => {
setTimeout(() => {
sent.delete();
}, 2500);
});
}
const amount = Number(input) > 100
? 101
: Number(input) + 1;
message.channel.bulkDelete(amount, true)
.then((_message) => {
message.channel
.send(`Bot cleared \`${_message.size}\` messages :broom:`)
.then((sent) => {
setTimeout(() => {
sent.delete();
}, 2500);
});
});
}
if (command === 'help' && input === 'clear') {
const newEmbed = new MessageEmbed()
.setColor('#00B2B2')
.setTitle('**Clear Help**')
.setDescription(
`This command clears messages for example \`${prefix}clear 5\` or \`${prefix}c 5\`.`,
)
.setFooter(
`Requested by ${message.author.tag}`,
message.author.displayAvatarURL(),
)
.setTimestamp();
message.channel.send(newEmbed);
}
});
Example 2: discord.js clean content
let txt1 = args.join(' ')
if (collected.first(txt1)) {
let text1 = collected.first().cleanContent
return message.channel.send(text1);
}