Delete command usage after Using it

Viewed 99

Im trying to delete the .ping after i just used it, while the answer, 'pong' still remains.

The code below is what i can think of, but it doesn't seem to work, can anyone correct my mistake?

client.on('message', msg => {
  if (msg.content === '.ping') {
 message.delete(1000);
    msg.reply('pong!');
  }
});
2 Answers

Like what @NullDev mentioned in the comment, you should be using msg rather than message since that's what you declared the variable.

Second, is how you set up the timeout function. In your code, you set it as:

message.delete(1000);

But if you reference the discord.js documentation, it shows you the proper way to delete a message with a timeout is like this:

message.delete({ timeout: 1000 });

This is because the timeout is a property of the options parameter, thus you have to use the curly braces.


But for good measure, I would suggest that you add a catch method in order to avoid your code from breaking down, like so:

message.delete({ timeout: 1000 })
    .catch(console.error);

Note:

From discord.js v13 and beyond, the delete method will not accept any options, therefore you have to do this instead:

setTimeout(() => message.delete(), 1000);

well this one code should do the job :

client.on('message', msg => {
  if (msg.content === '.ping') {
   message.channel.bulkDelete(1)
   msg.reply('pong!');
  }
});
Related