jackosaur
jackosaur

Reputation: 335

Discord.js delete function

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.

Upvotes: 0

Views: 4677

Answers (9)

Seblor
Seblor

Reputation: 7136

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

  }
});

Upvotes: 6

Max
Max

Reputation: 107

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

Upvotes: 2

DWGaryYT
DWGaryYT

Reputation: 16

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

msg.delete({ timeout: 1000});

Or delete instantly via this:

msg.delete();

Upvotes: 0

Radnerus
Radnerus

Reputation: 592

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)

Upvotes: 1

Disky
Disky

Reputation: 1

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.

Upvotes: 0

user14674305
user14674305

Reputation:

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..

Upvotes: 1

Mahir Jobanputra
Mahir Jobanputra

Reputation: 21

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/#/

Upvotes: 2

RareHyperIon
RareHyperIon

Reputation: 11

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.

Upvotes: 0

Octagonal T
Octagonal T

Reputation: 404

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()

Upvotes: 0

Related Questions