0

My issue is that I receive an error when trying to delete a message in Discord.

client.on('message', msg => {

    if(msg.content.startsWith(".del ")) {

      msg.delete(1000); //Supposed to delete message

    }
});

I receive this error:

C:\Users\---\Desktop\Test\node_modules\discord.js\src\structures\Message.js:501
    if (typeof options !== 'object') throw new TypeError('INVALID_TYPE', 'options', 'object', true);
                                     ^

TypeError [INVALID_TYPE]: Supplied options is not an object.
    at Message.delete (C:\Users\---\Desktop\Test\node_modules\discord.js\src\structures\Message.js:501:44)
    at Client.<anonymous> (C:\Users\---\Desktop\Test\index.js:51:17)
    at Client.emit (events.js:310:20)
    at MessageCreateAction.handle (C:\Users\---\Desktop\Test\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\---\Desktop\Test\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\---\Desktop\Test\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (C:\Users\---\Desktop\Test\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (C:\Users\---\Desktop\Test\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
    at WebSocket.onMessage (C:\Users\---\Desktop\Test\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:310:20) {
  [Symbol(code)]: 'INVALID_TYPE'
}

I have also tried message.delete(1000), but I receive an error telling me that message is undefined.

The program works when I remove the code attempting to delete the message.

2
  • Can you edit your question to add the exact error message ? Also are you sure the message exists when you try to delete it ? Maybe there is something else deleting the message before, like an other instance of your bot.
    – Seblor
    Commented Apr 23, 2020 at 20:29
  • no need to delete your question. I gave one answer, you can still wait for more. I am not the only one here ;) Commented May 26, 2020 at 20:17

9 Answers 9

6

As the error message says, you need to pass either nothing or an object to the delete method.


New version vor Discord.js V13

For Discord.js@13 and above, the message.delete() method does not accept options any longer. You now need to use a setTimeout like this:

The Message.delete() method no longer accepts any options, requiring a timed-delete to be performed manually.

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

For Discord.js V12 and older

You can look it up in the documentation. What you are trying to do would be something like this :

client.on('message', msg => {
  if(msg.content.startsWith(".del ")) {

    msg.delete({timeout: 1000}); //Supposed to delete message

  }
});
4
  • how to handle the error, in case the message is already deleted before the code run? let's say with 5 sec timeout, it's a plausible scenario Commented Dec 23, 2021 at 18:54
  • @FederickJonathan I guess you just need to catch the promise with a simple .catch(console.log), or just a no-op callback if you want to avoid any exception.
    – Seblor
    Commented Dec 23, 2021 at 20:56
  • the catch solution somehow doesn't work. my code looks like this message.delete().catch(reason => {console.log('reason:', reason)}), the error is catched and printed out in console but the bot still crash. about no-op callback, what is that? can you explain more? Commented Dec 24, 2021 at 5:40
  • @FederickJonathan If the deletion is catched, it should not crash. You should create a question of your own with the code and the error message. As for the no-op function, it's just a function that has no operation, so usually an empty function () => {}.
    – Seblor
    Commented Dec 24, 2021 at 10:54
2

change your code by removing msg.delete(3000) to msg.delete({ timeout: 3000 }) and that should fix the error because it was changed in discord.js v12.

You can compare the docs - https://www.npmjs.com/package/discord.js-v11
You can compare the docs - https://discord.js.org/#/

2

You could do it by two methods

  1. SetTimeout method

    setTimeout(() => {
        msg.delete()
    }, 1000 // <- change this according to your requirement in miliseconds)
    
  2. You could add the timeout inside the args of the commands

    just instead of

    msg.delete(3000);

    put

    msg.delete({ timeout: 1000});

  3. To do bulk deletes you can do

    setTimeout(() => {
        message.channel.bulkDelete(var)
    }, 1000 // <- change this according to your requirement in miliseconds)
    

    this can help you delete up to 100 messages.

you can compare the docs here - Discord.js v11 and Discord.js v12

1

If you want to remove a message after delay:

setTimeout(function() {
  msg.delete();
}, 1000) // Time in miliseconds, 1s = 1000ms

If you want to delete it use:

msg.delete();

Simple code (removes command message after 1 sec.):

client.on('message', msg => {
    if(msg.content.startsWith(".del ")) {
      setTimeout(function() {
          msg.delete();
      }, 1000)
    }
});

Error message is not defined shows you because you defined msg at client.on('message', msg..

1

Note: The timeout parameter of delete() method/function is going to get deprecated in the v13 djs (which is not mentioned in the documents).So you can use setTimeout() function for a delay.

setTimeout(function(){
  msg.delete()
},3000)
0

If you want a delay in deleting the message:

setTimeout(function(){
  msg.delete()
}, 1000) // this is the number of miliseconds for the delay

If you want to delete it right away,

msg.delete()
0

your almost correct, I can see you use v12 so that function has changed so first thing you can do to correct your mistake is change msg.delete(3000) to msg.delete({ timeout: 3000 }) and that should fix the error you can see v11 does not use require you to do that but v12 does anyway i think i've solved the issue here please mark this as answered. I have a suggestion for you if your making the Discord.JS bot for public then add configuration into the bot and make command files so you dont have to make one big messy file and using prefix statements like if(!message.content.includes("**prefixhere**") return; that would ignore all message that does not have a prefix and then you need to split up the message into ["prefix", "cmd", "args"] and that should work perfectly fine. Thanks for letting me help you.

0

If you are attempting to add a delay, the previous responses offer numerous examples. For the chance that you are attempting to delete a certain amount of messages, you need to use message.channel.bulkDelete(varName). The method to delete numerous messages is bulk delete, which I will also mention, will only allow you to delete 1 - 100 messages at a time.

0

Use this to delete msg in a timeout (calculated in ms.):

msg.delete({ timeout: 1000});

Or delete instantly via this:

msg.delete();

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.