2

I'm trying to get an image attachment through a Discord slash command interaction, so I can send a manipulated version back to the user, but I just can't seem to be able to do it.

The interaction itself comes through alright, but the "image" option's object is just {name: 'image', type: undefined, value: '972518871573602374'}. I think it's strange that the type is undefined despite me clearly using the .addAttachmentOption() method.

Here's my command builder:

new SlashCommandBuilder()
  .setName("dither")
  .setDescription("Apply a dithering effect to an image")

  .addAttachmentOption((option)=> option
    .setRequired(true)
    .setName("image")
    .setDescription("The image to dither"))

  .addNumberOption((option)=> option
    .setRequired(false)
    .setName("intensity")
    .setDescription(`% of dithering to apply (${intensityDefault}% by default)`))
  .toJSON()

I thought the URL or something might be elsewhere in the interaction object but I couldn't find anything related to attachments. I also couldn't find anything about interaction attachments in the documentation so I thought I'd try here. Is it just an unimplemented feature? But why would there be a method for it then?

I'm also unsure of what the value property represents. I thought it could be the attachment ID, but even if I wanted to recreate the attachment URL myself I'd still need to know the filename.

hosma
  • 21
  • 2
  • 3

3 Answers3

4

According to https://discordjs.guide/interactions/slash-commands.html#parsing-options :

const attachment = interaction.options.getAttachment("image")

const name = attachment.name
const url = attachment.url
const proxyURL = attachment.proxyURL

Please note that, in the example above, I have used image because that was the name you attributed to the attachment -- .setName("image").

  • Thanks for the reply! I've read through the docs page you've linked to but while it does mention `getAttachment()` I get an error saying that such method doesn't exist in my interaction options? I'm looking at the `interaction.options` constructor right now and there are getter methods for every single option type OTHER than Attachment, any idea why this might be happening? – hosma May 25 '22 at 01:38
  • Sorry, meant to say prototype instead of constructor. And just to clarify the error message is `Uncaught TypeError TypeError: interaction.options.getAttachment is not a function` – hosma May 25 '22 at 01:54
  • When I tried to register your command for a test, I got an error message saying that there is no ".toJSON()" option in SlashCommandBuilder. Perhaps that's the issue. – Luiz Cláudio May 26 '22 at 13:12
  • That can't be, `.toJSON()` works just fine for me. Notice that I call it last and directly on the `new SlashCommandBuilder()`. It successfully converts it to an object, the command shows up in Discord and I can use it on servers. The problem is that there's no `.getAttachment()` on the interaction sent out by the command, while there are similar methods for other option types such as `.getBoolean()`, `.getNumber()`, `.getString()`, `.getSubcommand()` etc. I am using the discord.js 13.6.0 package. – hosma May 27 '22 at 11:29
  • When you test the slash command on Discord, does it prompt you to attach a file? – Luiz Cláudio May 28 '22 at 12:10
  • Indeed, everything works as intended other than actually retrieving the attached file from the server. See the following imgur album for some screenshots of what's happening: https://imgur.com/a/klufMCF – hosma May 28 '22 at 19:59
  • What is the code you used to register the command? Were there any changes after it was last registered? – Luiz Cláudio May 29 '22 at 21:10
  • Sorry for the late response, I register my commands by passing an array of all of their `SlashCommandBuilder`s as JSON to `client.application.commands.set()`. Whenever I make changes to a command I restart the bot and switch servers or channels in order to refresh them client-side. I know there's also the `@discordjs/rest` module but it seems to me that it does the same thing but in a more confusing manner. – hosma Jun 01 '22 at 21:02
2

Above they already explained how to receive images, now if you want to receive texts:

To receive text data you can write this way, the example is a code that I use in my application:

new SlashCommandBuilder()
    .setName('add')
    .setDescription('Add a product to stock!')
    .addAttachmentOption(option =>
        option.setName("accounts")
            .setDescription("Data to be uploaded to database")
            .setRequired(true)
    ),
async execute(interaction) {
    const data = await handleUpload(interaction.options.getAttachment('accounts'))

handleUpload function:

async function handleUpload(attachment){
    const response = await fetch(attachment.attachment)
    const data = await response.text()

    return data
        .trim()
        .split("\n")
};
Yuri
  • 53
  • 5
0

This is my code and it works

new SlashCommandBuilder()
        .setName('test')
        .setDescription('Test Command!')
        .addAttachmentOption(option => option
            .setName('attach')
            .setDescription('Attachment File')
            .setRequired(true)),
    async execute(interaction, client) {
        const message = await interaction.deferReply({
            fetchReply: true
        });
        const user = message.author
        const file = interaction.options.getAttachment('attach')
        const emb = new MessageEmbed()
            .setAuthor(user.username, user.displayAvatarURL(true))
            .setTitle('Embed Message /w attachment')
            .setDescription('Uploading attachment...')
            .setThumbnail(message.guild.iconURL(true))
            .setTimestamp()
            .setImage(file.url)
            .setFooter('Successfully', user.displayAvatarURL(true))
        console.log(emb)
        await interaction.editReply({ embeds: [emb] });```
Julian-V
  • 13
  • 1
  • 6
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 24 '22 at 06:37
  • Thanks, seems like finally implemented attachments in v14 – hosma Jul 25 '22 at 09:44