TahaHaza00
TahaHaza00

Reputation: 53

How do I delete a message once it's been sent discord.js

The start

Hello everyone, this may sound/look like this question has been asked before on stack overflow but it hasn't. I've been looking around for a while and been reading documentation yet I still don't understand/don't know how to implement the thing i'm talking about into my code.

The problem

I have a problem where on discord I am trying to see if a user sent a message which has m!verify in it and after they send that message in 0.5 seconds (i.e) the message is deleted. I've looked around and seen similar questions like these but none of them solve my problem.

The code

module.exports = class VerifyCommand extends BaseCommand {
  constructor() {
    super('verify', 'tools', []);
  }

  async run(client, message, args) {
    if (!message.guild.me.hasPermission("MANAGE_ROLES")) return message.channel.send("I require the \`MANAGE_ROLES\` permission to execute this command!")

    const roleMember = message.guild.roles.cache.get("860636844618154034");
    const roleVerified = message.guild.roles.cache.get("860636743564918844");

    await message.member.roles.add(roleMember).catch(err => console.log(err));
    await message.member.roles.add(roleVerified).catch(err => console.log(err));
  }
}

What I want is something like this (pseudocode)

if(user sent this message m!verify) {
setTimeout({
   message.bulkDelete("m!verify")
}, 1000)
}

Note: Linking any documentation or tutorials that have the EXACT same answer I'm looking for (in the comments and not as an answer) would be really useful. (I promise I have looked at lots of documentation) :). I am also using slappey to generate my commands.

Upvotes: 0

Views: 813

Answers (2)

Çınar Civan
Çınar Civan

Reputation: 1

If you want it to be deleted within a certain time: message.delete({ timeout: 500})

Upvotes: 0

Squiddleton
Squiddleton

Reputation: 448

The method you're looking for is <message>.delete, docs found here. The benefit of this is its optional options parameter which has a built-in setTimeout for how long to wait before deleting the message.

In your specific case of deleting the message object that initiated the command after half a second, you can simply use message.delete({ timeout: 500 }).

Upvotes: 1

Related Questions