Get attachment from command after modal submit

Hi there :waveBoye: I'm currently working on a small /bug report command for a friend of mine. For now, it works like this: - 1 Call the command and attach an Image (via .addAttachmentOption) - 2 Hit enter. Modal shows up > More info to add! Submit and done. So, here's my question: What is the easiest way of getting an attachment url for an embed that'll be send to a private channel? -# I thought I could make it easy for myself, but unfortunately I can't haha. Obviously, no more command data is transferred with a modal submit... :nerdSplain_siera: Here'y my small test code:
import { getChannel } from "./xyz";
import { EmbedBuilder } from 'discord.js';

export const data = { customIdPrefix: 'bugReport' };

export async function execute(interaction) {
// Get all the input values from the modal - test for now...
const module = interaction.fields.getTextInputValue('bugReport1') || '/';
const bugDescription = interaction.fields.getTextInputValue('bugReport2') || '/';
const reproductionSteps = interaction.fields.getTextInputValue('bugReport3') || '/';
const optionalLinks = interaction.fields.getTextInputValue('bugReport4') || '/';

// Error: Cannot read property 'getAttachment' of undefined
const optionalScreenshot = interaction.options.getAttachment('screenshot');

const guild = interaction.guild;
const forumChannelId = '1353799943244222465'; // Static ID for now...
const forumChannel = await getChannel(guild, forumChannelId);

const postTitle = `${interaction.user.username}: ${module}`.slice(0, 100); // Limit to 100 characters
const postContent = `🦺`;

const post = await forumChannel.threads.create({
name: postTitle,
autoArchiveDuration: 1440, // 1440 Minutes = 24 hours
reason: 'New bug report',
message: {
content: postContent,
}
});

const postEmbed = new EmbedBuilder()
.setTitle('Bug Report Details')
.addFields(
{ name: 'Module', value: module },
{ name: 'Description', value: bugDescription },
{ name: 'Reproduction Steps', value: reproductionSteps },
{ name: 'Optional Links', value: optionalLinks }
)
.setImage(optionalScreenshot?.url)
.setFooter({
iconURL: interaction.user.displayAvatarURL(),
text: `Reported by ${interaction.user.username}`
})
.setTimestamp()
.setColor('DarkGrey');

await post.send({ embeds: [postEmbed] });

const successEmbed = new EmbedBuilder()
.setDescription('test 123')
.setColor('Green');
await interaction.reply({ embeds: [successEmbed], ephemeral: true });
}
import { getChannel } from "./xyz";
import { EmbedBuilder } from 'discord.js';

export const data = { customIdPrefix: 'bugReport' };

export async function execute(interaction) {
// Get all the input values from the modal - test for now...
const module = interaction.fields.getTextInputValue('bugReport1') || '/';
const bugDescription = interaction.fields.getTextInputValue('bugReport2') || '/';
const reproductionSteps = interaction.fields.getTextInputValue('bugReport3') || '/';
const optionalLinks = interaction.fields.getTextInputValue('bugReport4') || '/';

// Error: Cannot read property 'getAttachment' of undefined
const optionalScreenshot = interaction.options.getAttachment('screenshot');

const guild = interaction.guild;
const forumChannelId = '1353799943244222465'; // Static ID for now...
const forumChannel = await getChannel(guild, forumChannelId);

const postTitle = `${interaction.user.username}: ${module}`.slice(0, 100); // Limit to 100 characters
const postContent = `🦺`;

const post = await forumChannel.threads.create({
name: postTitle,
autoArchiveDuration: 1440, // 1440 Minutes = 24 hours
reason: 'New bug report',
message: {
content: postContent,
}
});

const postEmbed = new EmbedBuilder()
.setTitle('Bug Report Details')
.addFields(
{ name: 'Module', value: module },
{ name: 'Description', value: bugDescription },
{ name: 'Reproduction Steps', value: reproductionSteps },
{ name: 'Optional Links', value: optionalLinks }
)
.setImage(optionalScreenshot?.url)
.setFooter({
iconURL: interaction.user.displayAvatarURL(),
text: `Reported by ${interaction.user.username}`
})
.setTimestamp()
.setColor('DarkGrey');

await post.send({ embeds: [postEmbed] });

const successEmbed = new EmbedBuilder()
.setDescription('test 123')
.setColor('Green');
await interaction.reply({ embeds: [successEmbed], ephemeral: true });
}
3 Replies
d.js toolkit
d.js toolkit•2w 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
d.js docs
d.js docs•2w ago
:method: ChatInputCommandInteraction#awaitModalSubmit() [email protected] Collects a single modal submit interaction that passes the filter. The Promise will reject if the time expires.
// Collect a modal submit interaction
const filter = (interaction) => interaction.customId === 'modal';
interaction.awaitModalSubmit({ filter, time: 15_000 })
.then(interaction => console.log(`${interaction.customId} was submitted!`))
.catch(console.error);
// Collect a modal submit interaction
const filter = (interaction) => interaction.customId === 'modal';
interaction.awaitModalSubmit({ filter, time: 15_000 })
.then(interaction => console.log(`${interaction.customId} was submitted!`))
.catch(console.error);
Maverick
MaverickOP•2w ago
Yeah true, I guess that's the easiest of doing so. Thank you! :)

Did you find this page helpful?