0
votes

I am trying to make my bot delete a specific number of messages, I think an if would be best for the case, but I don't know how to do this...

This is how it's supposed to work: There's a message from my bot in the channel, it's always the last one in the chat. My bot will check if the message is actually there, if it is, it will delete the message, along with the command, and then send a new message. If the message is not there, it will just delete the command and send the new message.

Here's my code for reference:

if(/* message is there */) const fetched = await message.channel.fetchMessages({limit: 2});
else const fetched = await message.channel.fetchMessages({limit: 1});

// Deletes stuff
message.channel.bulkDelete(fetched)
  .catch(error => message.reply(`There was an error: ${error}`));
message.channel.send("```Open```");

How can I check if that previous message is there?

1

1 Answers

0
votes

I would check if the author of the last message is your bot:

let lastTwo = await message.channel.fetchMessages({limit: 2}), // Get the last 2 messages
  last = lastTwo.last(); // The last in the collection will be the last message
if (last.author.id == client.user.id) await message.channel.bulkDelete(lastTwo);
else await last.delete();
message.channel.send("```Open```");