ERROR: Cannot message this thread until after the post author has sent an initial message

I'm trying to send a message right after a thread creation. But sometimes i receive this error: Error sending message in thread: DiscordAPIError[40058]: Cannot message this thread until after the post author has sent an initial message. I believe that discord has some small delay to publish the author message, due that i guess i have to wait for something but i dont know what do i have to listen for. Code:
client.on('threadCreate', async (thread) => {
try {
if (thread.parentId !== process.env.CHANNEL_ID) return;
if (!thread.joinable) return;
await thread.join();

await thread.send({
content: `<@${thread.ownerId}>, message content`,
});

} catch (error) {
console.error(`Error sending message in thread: ${error}`);
}
});
client.on('threadCreate', async (thread) => {
try {
if (thread.parentId !== process.env.CHANNEL_ID) return;
if (!thread.joinable) return;
await thread.join();

await thread.send({
content: `<@${thread.ownerId}>, message content`,
});

} catch (error) {
console.error(`Error sending message in thread: ${error}`);
}
});
1 Reply
d.js toolkit
d.js toolkit2mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP

Did you find this page helpful?