Home > Back-end >  DiscordAPIError: Unknown Message when trying to automatically delete messages
DiscordAPIError: Unknown Message when trying to automatically delete messages

Time:03-06

I am trying to create a system that automatically deletes messages in a specific channel if the message does not start with the command prefix AND the user does not have a specific role.

Everything works, but as soon as it deletes one message, it throws the error DiscordAPIError: Unknown Message

The following is the code snippet where this error is coming from:

client.on("messageCreate", (message) => {
    function deleteTalk(delay) {
        setInterval(() => {
            if (message.channel.id === "786356926216536102") {
                if (
                    !message.content.startsWith(prefix) &&
                    !message.member.roles.cache.has("791024545091420190")
                ) {
                    if (!message) return;
                    const user = message.author;
                    message.delete();
                    user.send("Please don't talk in report channels.");
                    return;
                }
            }
        }, delay);
    }

    deleteTalk(100);

//Other Code...
});

I've looked through questions where people have gotten the same error, but the solutions to those instances haven't worked for me. I've tried to add in code snippets that should prevent the function from firing again if there is no message to delete, but I assume what I had written was incorrect as I am still getting the error.

CodePudding user response:

You put a setInterval so for the first execution your message is deleted but then you try again to delete it 100ms later but the message no longer exists. For your situation I don't see what the setInterval is for

client.on('messageCreate', (message) => {
    if (
        message.channel.id === '786356926216536102' &&
        !message.content.startsWith(prefix) &&
        !message.member.roles.cache.has('791024545091420190')
    ) {
        const user = message.author;
        message.delete();
        user.send("Please don't talk in report channels.");
        return;
    }
});

  • Related