Tengo un bot que envía un mensaje incrustado después de recibir un /xyz con algo como esto
client.on("interactionCreate", async (interaction) => { if (!interaction.isCommand()) return; const { commandName } = interaction; if (commandName === "xyz") { const embed = { title: "title of embed message", description: "description of embed message", .... } await interaction.channel.send({ embeds: [embed] }); } Me gustaría saber cómo eliminar los mensajes incrustados anteriores que envió el bot, antes de enviar uno nuevo después de recibir el comando /xyz
Establezca una variable en el ámbito externo que asignará al enviar las incrustaciones. Eliminarlo cada vez que se ejecuta el comando
let embedBefore; client.on("interactionCreate", async (interaction) => { if (!interaction.isCommand()) return; const { commandName } = interaction; if (commandName === "xyz") { await embedBefore?.delete().catch(console.error) // delete the embed, catch and log error if thrown const embed = { title: "title of embed message", description: "description of embed message" } // assign embedBefore to the sent embed; Note: this is not an interaction reply embedBefore = await interaction.channel.send({ embeds: [embed] }); } })La inserción permanecerá allí cuando se reinicie el bot y se vuelva a ejecutar el comando.
Puede almacenar las incrustaciones anteriores (solo debe haber una, cierto, ya que elimina las anteriores ...) en algo así como un Set si tiene varias incrustaciones de alguna manera y luego, antes de enviar su respuesta, itere sobre el Conjunto e invoque el método delete() en los mensajes almacenados. Aquí la documentación para Sets en caso de que necesite más información.