Home > Enterprise >  Discord.js: "Unknown Message" when trying to delete a message
Discord.js: "Unknown Message" when trying to delete a message

Time:09-27

This script is meant to send a message that'll keep track of the process log of a Minecraft server, and as soon as it finishes loading, it should delete the message:

let statusBase = "Opening server...\n";
let statusMessage = await message.channel.send(statusBase   "` `"); 

// This function executes on the process's stdout and stderr's "data" event
async function onData (data) {
    if (data.indexOf("Done") != -1) {
        mcserver.process.removeAllListeners();
        message.channel.send("Server open");
        console.log(statusMessage); // Used this for debugging on this issue. Yes, it prints stuff on the prompt.
        statusMessage.delete();
    } else {
        statusMessage.edit(statusBase   "`"   data.toString()   "`");
    }
}

but for some reason it just throws me an "Unknown message" error as soon as it gets to the line where it's supposed to delete the message. I don't see anything wrong. Can someone help me?

CodePudding user response:

You can use the following code to delete a message 5 seconds after sending:

message.channel.send("Server open").then(message => message.delete({ timeout: 5000 }));

CodePudding user response:

You are trying to delete when the message still on process to send, expect that if 3 or 4 code lines in the same if else statement, all of them going to execute at once.

So you can do like this to delay the deleting after the sent message.

 mcserver.process.removeAllListeners();
 message.channel.send("Server open");
 console.log(statusMessage);
 setTimeout(() => {
   statusMessage.delete();
 }, 1500) the timer is a ms form.

Sample SetTimeout Function

  • Related