首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何接收来自discord.js交互的附件?

如何接收来自discord.js交互的附件?
EN

Stack Overflow用户
提问于 2022-05-07 16:03:02
回答 2查看 1.9K关注 0票数 1

我试图通过一个不和谐的斜杠命令交互获得一个图像附件,所以我可以将一个被操纵的版本发送回用户,但我似乎无法做到这一点。

交互本身是可以通过的,但是"image"选项的对象只是{name: 'image', type: undefined, value: '972518871573602374'}。我觉得奇怪的是,尽管我清楚地使用了.addAttachmentOption()方法,但类型还是没有定义。

这是我的命令生成器:

代码语言:javascript
复制
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()

我认为URL或其他东西可能在交互对象的其他地方,但我找不到任何与附件相关的内容。我在文档中也找不到任何关于交互附件的信息,所以我想在这里尝试一下。它只是一个未实现的特性吗?但为什么会有一个方法呢?

我也不确定value属性代表什么。我以为它可以是附件ID,但是即使我想自己重新创建附件URL,我仍然需要知道文件名。

EN

回答 2

Stack Overflow用户

发布于 2022-05-21 15:29:44

根据https://discordjs.guide/interactions/slash-commands.html#parsing-options的说法:

代码语言:javascript
复制
const attachment = interaction.options.getAttachment("image")

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

请注意,在上面的示例中,我使用了image,因为这是附件的名称- .setName("image")

票数 2
EN

Stack Overflow用户

发布于 2022-07-24 03:08:35

这是我的代码,它能工作

代码语言:javascript
复制
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] });```
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72154232

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档