admin管理员组文章数量:1359245
I'm trying to get an image attachment through a Discord slash mand 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 es 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 mand 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.
I'm trying to get an image attachment through a Discord slash mand 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 es 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 mand 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.
3 Answers
Reset to default 3According to https://discordjs.guide/interactions/slash-mands.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")
.
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")
};
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] });```
本文标签: javascriptHow can I recieve attachments from discordjs interactionsStack Overflow
版权声明:本文标题:javascript - How can I recieve attachments from discord.js interactions? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744082026a2587820.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论