Skip to content
Advertisement

Discord.js: “Unknown Message” when trying to delete a message

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?

Advertisement

Answer

I fixed it myself. The issue was not in the delete() method, but rather on the edit() method that was trying to edit the message when it had already been deleted.

Here are the changes to the function:

async function onData (data) {
    if (data.indexOf("Done") != -1) {
        message.channel.send("Server open");

        // Unrelated to this issue but I was removing the message and error listeners the wrong way before
        mcserver.process.stdout.removeAllListeners();
        mcserver.process.stderr.removeAllListeners();

        // Wait for the message, then delete it
        await statusMessage;
        statusMessage.delete();
    } else {
        // Check if the message still exists in that channel, and return if it doesn't
        if (statusMessage.channel.messages.fetch(statusMessage.id) == null) {
            console.log("nope");
            return;
        }

        // Edit the message
        statusMessage.edit(statusBase + "`" + data.toString() + "`").catch(() => {});
    }
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement