I’m encountering a frustrating issue with my interactionCreate event – it’s failing to retrieve the target message, which is the message I aim to decrypt after the modal form is submitted. Oddly, despite the message’s existence, it stubbornly returns an ‘undefined’ error, as if the message simply isn’t there. This event is activated through a context command, which opens a modal form for key entry. After submission, the subsequent interaction should yield a decrypted message.
Here is my sanbox code:
if (interaction.customId === 'key_modal') {
try {
// Extract the code provided in the modal form
const submittedCode = interaction.fields.getTextInputValue('code-input');
// Compare the submitted code with the expected code (e.g., '123')
if (submittedCode !== '123') {
await interaction.reply({ content: 'Incorrect code. Please try again.', ephemeral: true });
return;
}
// Fetch the original message
const message = await interaction.channel.messages.fetch(interaction.targetMessage).id;
if (!message || message.embeds.length === 0) {
await interaction.reply({ content: 'No message or embed found.', ephemeral: true });
return;
}
const embed = message.embeds[0];
if (!embed.fields || embed.fields.length === 0) {
await interaction.reply({ content: 'No fields found in the embed.', ephemeral: true });
return;
}
// Use the same secret key and variable key that were used to encrypt the message
const secretKey = generateSecretKey(); // Replace with your actual secret key generation
const variableKey = submittedCode; // Use the submitted code for decryption
const regex = /||```([^]+?)```(?:|||$)|||([^]+?)||/g;
const encryptedContent1 = embed.fields[0].value.replace(regex, '$1');
let decryptedMessage = decryptMessage(encryptedContent1, secretKey, variableKey);
if (embed.fields[1]) {
const encryptedContent2 = embed.fields[1].value.replace(regex, '$1');
decryptedMessage += ` ${decryptMessage(encryptedContent2, secretKey, variableKey)}`;
}
if (decryptedMessage.length > 2000) {
decryptedMessage = decryptedMessage.slice(0, 2000);
}
await interaction.reply({ content: `${decryptedMessage}`, ephemeral: true });
const embed2 = new Discord.EmbedBuilder();
embed2
.setTitle(":unlock: Log Entry Decrypted")
.setDescription(`${interaction.user.displayName} decrypted log entry: ${message.url}`)
.setColor("Green")
.setTimestamp();
await interaction.channel.send({ embeds: [embed2] });
} catch (error) {
console.error(error);
await interaction.reply({ content: 'An error occurred while decrypting the message.', ephemeral: true });
}
}
I have tried different approaches such as
interaction.targetMessage
interaction.targetMessage.id
const message = await interaction.channel.messages.fetch(interaction.messageId);
const message = await interaction.channel.messages.fetch(interaction.targetMessage).id;