【问题标题】:How to send attachments and embeds in the same message?如何在同一条消息中发送附件和嵌入?
【发布时间】:2019-11-24 18:40:21
【问题描述】:

如何在同一条消息中发送附件和嵌入?

发送附件:

if (message.content === ';file') {
  const attachment = new Attachment('https://i.imgur.com/FpPLFbT.png');
  message.channel.send(`text`, attachment);
}

发送嵌入:

if (msg.content === ';name') {
  const embed = new Discord.RichEmbed()
    .setTitle(`About`)
    .setDescription(`My name is <@${msg.author.id}>`)
    .setColor('RANDOM');
  msg.channel.send(embed);
}

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    要了解如何完成任务,您首先需要了解TextBasedChannel.send() 方法的工作原理。让我们看看文档中的TextChannel.send()

    .send([content], [options])

    content (StringResolvable):消息文本。
    options (MessageOptions or Attachment or RichEmbed):消息的选项,也可以是 RichEmbed 或附件

    现在,让我们看看你的用法是如何应用的。


    message.channel.send(`text`, attachment);
    

    在这种情况下,`text` 作为方法的 content 参数,attachment 作为 options 参数传递。

    msg.channel.send(embed);
    

    这里省略了content参数,embed作为options参数传递。


    在保持相同代码风格的同时,您可以利用一个对象来保存 options 参数的嵌入和附件。

    // const { Attachment, RichEmbed } = require('discord.js');
    
    const attachment = new Attachment('https://puu.sh/DTwNj/a3f2281a71.gif', 'test.gif');
    
    const embed = new RichEmbed()
      .setTitle('**Test**')
      .setImage('attachment://test.gif') // Remove this line to show the attachment separately.
    
    message.channel.send({ embed, files: [attachment] })
      .catch(console.error);
    

    【讨论】:

    • 非常感谢您提供的信息... :) 现在正在运行 :))) 感谢您的辛勤工作...
    【解决方案2】:

    TextChannel.send 函数可以采用不同的options

    所以你可以这样做:

    const Discord = require('discord.js');
    const client = new Discord.Client();
    
    client.on('message', (msg) => {
      msg.channel.send({
        embed:  new Discord.RichEmbed()
          .setTitle('A slick little embed')
          .setColor(0xFF0000)
          .setDescription('Hello, this is a slick embed!'),
        files: [{
          attachment: './README.md',
          name: 'readme'
        }]
      })
        .then(console.log)
        .catch(console.error);
    });
    

    但是,如果您想在嵌入中添加图像,只需按照 guide 中的示例或 api 文档中给出的示例:

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

    【讨论】:

    • 这也有效..谢谢你的努力:)
    • @DhirajSinghKarki 毫不犹豫地接受懒惰的答案,因为它比我的更详细。这样,寻找答案的人就会知道该解决方案正在发挥作用
    猜你喜欢
    • 2019-10-23
    • 2021-06-24
    • 1970-01-01
    • 2020-04-21
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 2022-08-04
    • 1970-01-01
    相关资源
    最近更新 更多