javascript - 清除命令说“未定义”而不是“已清除消息的数量”
问题描述
我试图让它显示在嵌入中删除了多少消息,但是当它发送嵌入时它说bot deleted undefined
messages。这是一张图片:
我需要它说机器人删除的ex: 15
消息。它可以用作普通消息,但不能用作嵌入。
这是我的代码:
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;
const amountEmbed = new Discord.MessageEmbed()
amountEmbed.setColor('#9947d6')
amountEmbed.setDescription('**Please enter the amount of messages that you would like to clear**')
const posiEmbed = new Discord.MessageEmbed()
posiEmbed.setColor('#9947d6')
posiEmbed.setDescription('**Please enter a positive number**')
const clearedEmbed = new Discord.MessageEmbed()
clearedEmbed.setColor('#9947d6')
clearedEmbed.setDescription(`**Bot cleared** \`${message.size}\` **messages :broom:**`)
if (command === 'clear' || command === 'c') {
if (!message.member.hasPermission('MANAGE_MESSAGES')) {
return;
}
if (isNaN(input)) {
return message.channel
.send(amountEmbed)
.then((sent) => {
setTimeout(() => {
sent.delete();
}, 2500);
});
}
if (Number(input) < 0) {
return message.channel
.send(posiEmbed)
.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(clearedEmbed)
.then((sent) => {
setTimeout(() => {
sent.delete();
}, 2500);
});
});
}
});
解决方案
bulkDelete()
返回已删除的消息,因此您应该使用该size
集合的 ,因为它可能与用户输入不同。例如,如果频道中只有 45 条消息,并且您键入!clear 100
,机器人应该说它删除了 46 条消息(它应该包括带有命令的消息),而不是 100 条。
如果您只发送一个,则不应创建所有这些不必要的嵌入。您可以在发送它们之前创建单个嵌入并在 if 语句中更新其描述。
我让你的功能更干净了;删除了所有不必要的绒毛,并添加了一个sendAndDelete()
您似乎一直在使用的功能。:)
检查下面的代码片段,它应该可以按预期工作并且代码更少:
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;
const embed = new Discord.MessageEmbed().setColor('#9947d6');
function sendAndDelete(msg, delay = 2500) {
message.channel.send(msg).then((sent) => {
setTimeout(() => {
sent.delete();
}, delay);
});
}
if (isNaN(input)) {
embed.setDescription('**Please enter the amount of messages that you would like to clear**');
return sendAndDelete(embed);
}
// you should not accept 0 either
if (Number(input) <= 0) {
embed.setDescription('**Please enter a positive number**');
return sendAndDelete(embed);
}
const amount = Number(input) > 100 ? 101 : Number(input) + 1;
message.channel.bulkDelete(amount, true).then((_message) => {
embed.setDescription(`**Bot cleared** \`${_message.size}\` **messages :broom:**`);
return sendAndDelete(embed);
});
}
});
推荐阅读
- sql - Oracle WITH 子句限制
- ios - 如何显示打开自定义 iOS 相机编辑应用程序以从任何 Web 浏览器或第三方应用程序捕获图片的选项?
- python - 对于没有标题行的 csv,如何从 pandas.Series.from_csv 切换到 pandas.read_csv?
- c# - MVVM WPF C#中DataGrid中的LoadingRow
- c# - 简化 JSON 序列化对象
- java - 如何使用 readerForUpdating().readValue 方法强制 Jackson 对象映射器忽略不完整的字段
- java - 无法传递参数apache shiro jsp sevlet
- junit - 为什么gitlab CI找不到我的junit报告神器?
- javascript - Highcharts:动态渲染颜色到系列
- c# - 在图片框上显示文本框