Akzus
Akzus

Reputation: 29

Clear Command saying 'Undefined' instead of 'Number of cleared messages'

I am trying to get it to show me how many messages were deleted on an embed, but when it sends the embed it says bot deleted undefined messages. Here's a picture:

enter image description here

I need it to say bot deleted ex: 15 messages. It works as a normal message but not as an embed.

Here is my code:

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

Upvotes: 1

Views: 56

Answers (2)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23161

bulkDelete() returns the deleted messages, so you should use the size of that collection as it can be different from the user input. For example, if there are only 45 messages in the channel, and you type !clear 100, the bot should say that it deleted 46 messages (it should include the one with the command), not 100.

You should not create all these unnecessary embeds if you only send one. You can create a single embed and update its description inside the if statement right before you send them.

I made your function a bit cleaner; removed all that unnecessary fluff, and added a sendAndDelete() function as you seem to use it all the time. :)

Check the snippet below, it should work as expected and with a lot less code:

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

Upvotes: 1

Brayden
Brayden

Reputation: 60

All you had to do is make the input a variable then call that variable in the embed. Here is the code:

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

Upvotes: 0

Related Questions