QUICKGAM3R
QUICKGAM3R

Reputation: 41

Delete request message

How do I delete a message that requested the bot? Imagine this is in discord:

Me
!quote

Bot
'quote........' - someone

Then the bot deletes its own message as well as my message.

I got this code to delete the bots own message:

if (msg.content === ping_char+'quote') {
    var randomItem = quotes[Math.floor(Math.random()*quotes.length)];
    msg.channel.send(randomItem).then(msg => msg.delete({timeout: 10000}));
  }

But I don't know how to delete the message that brought up the bot in the first place if this is even possible. My code basically clears the last two messages, the bots and the user's/mine in this case?

Upvotes: 2

Views: 108

Answers (1)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23160

channel.send() returns the sent message (the one with the random quote) and you named it msg just like your incoming message (it's also msg). Make sure these are two different variables with two distinct names. If you name the original message message and the sent message sentMessage, you want to delete message once the message is sent.

Check out the working code below:

client.on('message', (message) => {
  if (message.author.bot) return;

  if (message.content === `${ping_char}quote`) {
    const randomItem = quotes[Math.floor(Math.random() * quotes.length)];
    message.channel
      .send(randomItem)
      // you want to delete the original message, not sentMessage
      .then((sentMessage) => message.delete({ timeout: 10000 }))
      .catch(console.error);
  }
});

enter image description here

Upvotes: 1

Related Questions