I want to create a channel with different courses. Each course will be stored in an array and will have 1 message containing the course name. We add a thread to this message. And when all threads are created, I want to create a table of contents in an embed linking to each thread in 1 click (thanks to the id of the corresponding thread)
Why does the code for sending the table of contents message run before the threads are created? How can I get the table of contents to be created directly at the very end?
client.on("messageCreate", message => {
arrayCourses = [["Course 1","https://example.xyz"],["Course 2","https://example2.xyz"]]
desc = "";
if(message.content === "$a go"){
arrayCourses.forEach( async element => {
var randomColor = Math.floor(Math.random()*16777215).toString(16);
let y = await message.channel.send({embeds:[ //sends a message on which we will create a thread
{
color: parseInt("0x" + randomColor),
title: element[0]
}
]});
y.startThread({ //create a thread on the message just send
name: element[0],
autoArchiveDuration: 60,
type: 'GUILD_PUBLIC_THREAD'
}).then( result => {
desc += result.id + "\n" //add the id of the created thread to a string.
})
});
//Send a message with the table of contents
message.channel.send({embeds:[{
color: 0x1515,
title: "Table des matières・JLPT N5",
description: desc,
footer: {text: "Pour rechercher un cours : a!search <recherche>", icon_url: "https://images.emojiterra.com/twitter/512px/1f50d.png"}
}]});
}
})