首页 > 解决方案 > 清除命令说“未定义”而不是“已清除消息的数量”

问题描述

我试图让它显示在嵌入中删除了多少消息,但是当它发送嵌入时它说bot deleted undefinedmessages。这是一张图片:

在此处输入图像描述

我需要它说机器人删除的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);
        });
    });
  }
});

标签: javascriptnode.jsdiscorddiscord.js

解决方案


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);
    });
  }
});

推荐阅读