簡體   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