我有一个 clear
命令可以删除您想要删除的消息数量。但是每当我说 clear 3
时,它只会清除 2 条消息。
client.on('message', async (message) => {
if (
message.content.toLowerCase().startsWith(prefix + 'clear') ||
message.content.toLowerCase().startsWith(prefix + '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(message.content.split(' ')[1])) {
let amount = 0;
if (message.content.split(' ')[1] === '1' || message.content.split(' ')[1] === '0') {
amount = 1;
} else {
amount = message.content.split(' ')[1];
if (amount > 100) {
amount = 100;
}
}
await message.channel.bulkDelete(amount, true).then((_message) => {
message.channel.send(`Bot cleared \`${_message.size}\` messages :broom:`).then((sent) => {
setTimeout(function () {
sent.delete();
}, 2500);
});
});
} else {
message.channel.send('enter the amount of messages that you would like to clear').then((sent) => {
setTimeout(function () {
sent.delete();
}, 2500);
});
}
} else {
if (message.content.toLowerCase() === prefix + 'help clear') {
const newEmbed = new Discord.MessageEmbed().setColor('#00B2B2').setTitle('**Clear Help**');
newEmbed
.setDescription('This command clears messages for example `.clear 5` or `.c 5`.')
.setFooter(`Requested by ${message.author.tag}`, message.author.displayAvatarURL())
.setTimestamp();
message.channel.send(newEmbed);
}
}
});
答案 0 :(得分:4)
您的代码运行良好,它删除了正确数量的消息。您忘记的是,您当前带有评论的消息也是一条消息,也会计入删除的数量。所以你可以把这个数字加一。
我可能会对您的代码进行一些更改:
.clear 5
。如果选中,则 isNaN(' ')
为 false
。prefix
es。await
。prefix
添加提前检查。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);
});
}
// add an extra to delete the current message too
const amount = Number(input) > 100
? 101
: Number(input) + 1;
message.channel.bulkDelete(amount, true)
.then((_message) => {
message.channel
// do you want to include the current message here?
// if not it should be ${_message.size - 1}
.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);
}
});
答案 1 :(得分:1)
那是因为它也在删除触发它的消息。简单的修复,只需取给定的数字,然后加一个。
await message.channel.bulkDelete(parseInt(amount) + 1, true).then((_message) => {
答案 2 :(得分:0)
触发消息(命令消息)也被获取。
有多种解决方案:
我在以下代码中标记了所有 3 个解决方案
client.on('message', async (message) => {
if (
message.content.toLowerCase().startsWith(prefix + 'clear') ||
message.content.toLowerCase().startsWith(prefix + '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(message.content.split(' ')[1])) {
let amount = 0;
if (message.content.split(' ')[1] === '1' || message.content.split(' ')[1] === '0') {
amount = 1;
} else {
amount = message.content.split(' ')[1];
if (amount > 100) {
amount = 100;
}
}
/* 1. Solution */
await message.delete().catch(e => { amount++; });
await message.channel.bulkDelete(amount, true).then((_message) => {
message.channel.send(`Bot cleared \`${_message.size}\` messages :broom:`).then((sent) => {
setTimeout(function () {
sent.delete();
}, 2500);
});
});
/* 2. Solution */
const messagesToDelete = await message.channel.messages.fetch({ before: message.id, limit: amount });
await message.channel.bulkDelete(messagesToDelete, true).then((_message) => {
message.channel.send(`Bot cleared \`${_message.size}\` messages :broom:`).then((sent) => {
setTimeout(function () {
sent.delete();
}, 2500);
});
});
/* 3. Solution */
amount >= 100 ? await message.delete() /* You can only bulk delete 100 messages */ : amount++;
await message.channel.bulkDelete(amount, true).then((_message) => {
message.channel.send(`Bot cleared \`${_message.size}\` messages :broom:`).then((sent) => {
setTimeout(function () {
sent.delete();
}, 2500);
});
});
/* The following code is your old code */
} else {
message.channel.send('enter the amount of messages that you would like to clear').then((sent) => {
setTimeout(function () {
sent.delete();
}, 2500);
});
}
} else {
if (message.content.toLowerCase() === prefix + 'help clear') {
const newEmbed = new Discord.MessageEmbed().setColor('#00B2B2').setTitle('**Clear Help**');
newEmbed
.setDescription('This command clears messages for example `.clear 5` or `.c 5`.')
.setFooter(`Requested by ${message.author.tag}`, message.author.displayAvatarURL())
.setTimestamp();
message.channel.send(newEmbed);
}
}
});