繁体   English   中英

如何将图像附加到斜杠命令响应 discord.js v13

[英]How to attach image to slash command response discord.js v13

我正在制作一个具有斜杠命令的机器人,我想附加一个文件,没有消息,只有一个图像文件。 我试过这样做,但它最终给了我一个空消息错误。

const attachment = new MessageAttachment("image.bmp");
client.api.interactions(interaction.id, interaction.token).callback.post({
    data: {
        type: 4,
        data: {
            files: [attachment]
        }
    }
})

所以我的问题是,如何使用此 JSON 格式 discord 交互附加图像?

更新:我目前有这个,它仍然不起作用,但给了我这个.

const file = new MessageAttachment (
                "image.bmp"
            );

client.api.interactions(interaction.id, interaction.token).callback.post({
                data: {
                    type: 4,
                    data: {
                        content: "hello",
                        "embeds": [
                            {
                            "title": `This is a cool embed`,
                            image: {
                                url: 'attachment://image.bmp',
                            },
                            "type": "rich",
                            "description": "",
                            "color": 0x00FFFF
                            }
                        ]
                    },
                }
            })

与 Discord.js V13 结合使用await interaction.deferReply(); 要让命令有更多时间工作,那么您可以使用await interaction.editRepy({ files: [attatchment] }); 更新deferReply(); 与你的依恋。 一个示例斜杠命令在斜杠命令处理程序中看起来像这样:

const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageAttachment } = require('discord.js')

module.exports = {
   data: new SlashCommandBuilder()
       .setName('attachment')
       .setDescription('Upload a file')
       .addAttachmentOption(option => option.setName('attachment').setDescription('Upload an attachment')),

   async execute(interaction) {
       let attachment = await interaction.options.getAttachment('attachment');

       await interaction.deferReply(); // Deffer Reply so we have more time

       console.log(attachment.url) // You can see URL, which is what we will use later
       console.log(attachment.contentType) // See the file type

       attachment = await new MessageAttachment(attachment.url, 'image.png'); // Converting URL to image as well as naming the file/assigning the file type

       await interaction.editReply( // Sending the image
           {
               files: [attachment]
           }
       )
   },
};

希望这会有所帮助::D

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM