【问题标题】:Discord.js local image in embedDiscord.js 本地图像嵌入
【发布时间】:2021-09-06 12:54:28
【问题描述】:

我正在尝试将此嵌入嵌入,有多种文件类型,png、jpg、gif 等。

我环顾四周,找不到解决办法

const client = new Discord.Client();
const fs = require("fs")

module.exports = {
    name: 'ari',
    description: "Sends a user his queen",
    
    async execute(message, args) {
        if(message.channel.id === "768497955417096272"){
            fs.readdir('./ariana', (err, files) => {
                if(err) console.error(err);
                const randImage = files[Math.floor(Math.random() * files.length)];
                message.channel.send({
                    files: ["./ariana/"+randImage]
                })
            })
        }else{
            message.channel.send("Go to Pics/Gifs channel!")
        }
    } 
} 

【问题讨论】:

  • 您是遇到错误还是无法正常工作?
  • 我的代码正在运行,但我不知道如何嵌入它。我已经阅读了文档,并浏览了这个网站,但似乎找不到答案。

标签: javascript node.js discord discord.js


【解决方案1】:

正如您在docs 中看到的那样,要从本地发送图像并使用它嵌入,您需要声明附件并使用附件名称并使用'attachment://<attachment-name>' 更改嵌入选项缩略图网址

// Send an embed with a local image inside
channel.send(embed, {
  embed: {
    thumbnail: {
         url: 'attachment://file.jpg'
      }
   },
   files: [{
      attachment: 'entire/path/to/file.jpg',
      name: 'file.jpg'
   }]
})
  .then(console.log)
  .catch(console.error);

或者如果你想在 setImage 中使用

      const embed = new discord.MessageEmbed()
        .attachFiles([{ name: 'file.jpg', attachment: 'entire/path/to/file.jpg' }])
        .setImage('attachment://file.jpg');
      channel.send(embed);

【讨论】:

    猜你喜欢
    • 2018-12-14
    • 2020-08-10
    • 2020-03-15
    • 2020-10-18
    • 2022-01-21
    • 2018-10-15
    • 2018-07-09
    • 2018-07-08
    • 2011-04-18
    相关资源
    最近更新 更多