I have this 'clear' command for my discord bot and it should work like this: -user types in !mute
- the bot deletes this many messages (up to 100)
- the bot sends a message saying that these messages were deleted
- the bot deletes that message 5 seconds later It all works except for the last part, you see if after executing part 2 the message is deleted by another source then the bot can't find a message to delete and crashes. The code is as follow:
module.exports = {
name: 'clear',
description: "clears messages",
async execute(message, args)
{
if(!args[0]) return message.reply("Please specify how many messages you want to clear!");
if(isNaN(args[0])) return message.reply("Please enter a number of messages you want to clear!");
if(args[0] > 100) return message.reply("You can't delete more than 100 messages!");
if(args[0] < 1) return message.reply("You can't delete less than 1 message!");
await message.channel.messages.fetch({limit: args[0]}).then(messages =>{
message.channel.bulkDelete(messages).then(() => {
message.channel.send("Deleted " args[0] " messages") .then(msg => {
let id = msg.id;
setTimeout(function(){
if(message.channel.messages.fetch(id))
{
try {
msg.delete()
}
catch (error) {
console.log(error)
return
}
}
}, 5000);
})
});
})
}
}
The error I'm getting is:
C:\Users\Miki\Desktop\discord boty\jajco bot\node_modules\discord.js\src\rest\RequestHandler.js:350
throw new DiscordAPIError(data, res.status, request);
^
DiscordAPIError: Unknown Message
at RequestHandler.execute (C:\Users\Miki\Desktop\discord boty\jajco bot\node_modules\discord.js\src\rest\RequestHandler.js:350:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (C:\Users\Miki\Desktop\discord boty\jajco bot\node_modules\discord.js\src\rest\RequestHandler.js:51:14)
at async MessageManager._fetchId (C:\Users\Miki\Desktop\discord boty\jajco bot\node_modules\discord.js\src\managers\MessageManager.js:219:18) {
method: 'get',
path: '/channels/943105549795471443/messages/946096412922368100',
code: 10008,
httpStatus: 404,
requestData: { json: undefined, files: [] }
}
As you can see I've tried using the try...catch() but it didn't fix the problem. Any ideas on what to do? Or maybe there is a mistake in my code like a missing import or some other thing like that?
CodePudding user response:
First,
try {
msg.delete()
} catch (error) {
return console.log(error)
}
that code here won't do what you expect. msg.delete()
is a function that returns a Promise
. Meaning if the promise fails
, a try/catch
structure won't do anything. Instead to catch the error you have to use the catch
method on the Promise, just as you used .then(
earlier, you'll have to use .catch(
.
So your new code will be
msg.delete().catch(error => console.error(error)
// Or, even better :
msg.delete().catch(console.error)
That is the exact same thing here :
if(message.channel.messages.fetch(id))
That will always be true
because the fetch
function will return a Promise
ans since it is neither a 0
, ""
(empty string),NaN
, false
, undefined
or null
See here for false values`
Instead what you're trying to do is check if the messages contain your id :
message.channel.messages.fetch(id)
.then(msg =>{
// Do what you want with the fetched msg
})
.catch(error =>{
message.reply('Failed to fetch the id')
}) // Console error why it failed to fetch the id (probably because it doesn't exist or got deleted)
Lastly,
message.channel.bulkDelete(messages).then(() => {
messages
is a Discord.Collection
, not a number, .bulkDelete
waits for a number so instead do this :
message.channel.bulkDelete(messages.size).then(() => {
I, personnally don't see why you have to fetch the message, you can simply do this :
message.channel.messages.fetch({limit: args[0]}).then(messages =>{
message.channel.bulkDelete(messages.size).then(() => {
message.channel.send("Deleted " args[0] " messages").then(msg =>{
setTimeout(() => msg.delete(), 5000);
})
});
})
CodePudding user response:
All you need to do is to await
msg.delete()
inside your try
block. A promise must be settled before it can be tested for any error.
try {
// Add 'await'
await msg.delete();
} catch (error) {
return console.log(error);
}
If you want to void the error to have the function fail silently:
msg.delete()
.catch(() => null);
You can also check Message#deletable
before having your client attempt to delete it:
if (msg.deletable) {
// I'd still recommend adding your preferred error handling
msg.delete();
}