Removing last embed.

const handlers = require("../handlerLoader.js");
const {
EmbedBuilder,
AttachmentBuilder,
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
} = require("discord.js");
const fs = require("fs");

let lastEphemeralMessageID = null;

module.exports = {
name: "agree",

async execute(interaction) {
if (lastEphemeralMessageID) {
await interaction.deleteReply(lastEphemeralMessageID);
lastEphemeralMessageID = null;
}

const captcha = await handlers.captchaHandler.generateCaptcha(
interaction.user.id
);

if (captcha.exists) {
fs.unlinkSync(captcha.filePath);
} else {
const file = new AttachmentBuilder(captcha.filePath, {
name: "captcha.png",
});
const captchaEmbed = new EmbedBuilder()
.setColor(0x2b2d31)
.setAuthor({
name: "Captcha Code",
})
.setImage(`attachment://${file.name}`);

const submitButton = new ButtonBuilder()
.setCustomId("submitCaptcha")
.setLabel("Submit")
.setStyle(ButtonStyle.Success)
.setEmoji("1144203623849537607");

const row = new ActionRowBuilder().addComponents(submitButton);

const reply = await interaction.reply({
embeds: [captchaEmbed],
files: [file],
components: [row],
ephemeral: true,
});

lastEphemeralMessageID = reply.id;
}
}
};
const handlers = require("../handlerLoader.js");
const {
EmbedBuilder,
AttachmentBuilder,
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
} = require("discord.js");
const fs = require("fs");

let lastEphemeralMessageID = null;

module.exports = {
name: "agree",

async execute(interaction) {
if (lastEphemeralMessageID) {
await interaction.deleteReply(lastEphemeralMessageID);
lastEphemeralMessageID = null;
}

const captcha = await handlers.captchaHandler.generateCaptcha(
interaction.user.id
);

if (captcha.exists) {
fs.unlinkSync(captcha.filePath);
} else {
const file = new AttachmentBuilder(captcha.filePath, {
name: "captcha.png",
});
const captchaEmbed = new EmbedBuilder()
.setColor(0x2b2d31)
.setAuthor({
name: "Captcha Code",
})
.setImage(`attachment://${file.name}`);

const submitButton = new ButtonBuilder()
.setCustomId("submitCaptcha")
.setLabel("Submit")
.setStyle(ButtonStyle.Success)
.setEmoji("1144203623849537607");

const row = new ActionRowBuilder().addComponents(submitButton);

const reply = await interaction.reply({
embeds: [captchaEmbed],
files: [file],
components: [row],
ephemeral: true,
});

lastEphemeralMessageID = reply.id;
}
}
};
So i've got this lil snippet from my bot. What it does is it requests a png image of a captcha code to be made then the function will reply with the image location. It sends an embed empheral and so on. I wanted it so that there can only be one active captcha image per user. And hey ho! It works, Untill i realised that the bot will just.. carry on making embed messages without removing the old ones... I have a lastEphemeralMessageID var, But. I'm not too sure if my code is right as, It's not removing the old ones, And it's just returning an error message so, Just came for some help! xD
2 Replies
d.js toolkit
d.js toolkit•12mo 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
Kiɾʌ Kenjiɾø
oh yeah wait oh shit yeah XD ya know what.... its not that big of an issue just like... click dismiss empheral welp that can get ticked off kek thanks qj <3
Want results from more Discord servers?
Add your server