admin管理员组文章数量:1326333
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.
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.
Share Improve this question edited Aug 23, 2021 at 9:42 Skulaurun Mrusal 2,8471 gold badge17 silver badges30 bronze badges asked Apr 23, 2020 at 20:19 jackosaurjackosaur 3454 silver badges6 bronze badges 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 ;) – Temani Afif Commented May 26, 2020 at 20:17
9 Answers
Reset to default 6As 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
}
});
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 pare the docs - https://www.npmjs./package/discord.js-v11
You can pare the docs - https://discord.js/#/
You could do it by two methods
SetTimeout method
setTimeout(() => { msg.delete() }, 1000 // <- change this according to your requirement in miliseconds)
You could add the timeout inside the args of the mands
just instead of
msg.delete(3000);
put
msg.delete({ timeout: 1000});
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 pare the docs here - Discord.js v11 and Discord.js v12
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 mand 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..
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)
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()
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 mand 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.
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.
Use this to delete msg in a timeout (calculated in ms.):
msg.delete({ timeout: 1000});
Or delete instantly via this:
msg.delete();
本文标签: javascriptDiscordjs delete functionStack Overflow
版权声明:本文标题:javascript - Discord.js delete function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742201808a2432100.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论