javascript - Discord.js v12 清除命令
问题描述
我有一个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);
}
}
});
解决方案
您的代码工作正常,它删除了正确数量的消息。您忘记的是,您当前带有评论的消息也是一条消息,并且也将计入删除的数量。所以你可以把这个数字加一。
我可能会对您的代码进行一些更改:
- 我不会用一个空格来分割你的消息,而是使用一个正则表达式来捕获多个空格。当前的不适用于
.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);
}
});
推荐阅读
- c# - 无法加载 MSBuildSdkResolver
- python-3.x - Python3 从网上下载文档
- javascript - 从用 Jsx Vue 编写的子组件调用父组件方法
- angular - TypeError:在单元测试Angular 6时无法读取未定义的属性“查询”
- c - 将小数转换为二进制后,我的输出显示的小数位数不超过 6 位。我需要小数点后 12 位
- string - Lua:返回“{foo}{bar}”的内容
- javascript - Highcharts - 实时更新柱形图
- excel - 对应单元格中所有值的总和,针对特定日历月和公司
- node.js - 在 mongo 数据库中创建动态模式
- php - 未捕获的错误:找不到类“PhpAmqpLib\Connection\AMQPStreamConnection”