我试图让它显示在嵌入中删除了多少消息,但是当它发送嵌入时,它显示机器人删除了 undefined
条消息。这是一张图片:
我需要它说机器人删除了 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);
});
});
}
});
答案 0 :(得分:1)
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);
});
}
});
答案 1 :(得分:0)
您所要做的就是使输入成为一个变量,然后在嵌入中调用该变量。代码如下:
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 size = input;
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** \`${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);
});
});
}
});