gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:13:11 27 4
gpt4 key购买 nike

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

发送附件:

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);
}

最佳答案

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

.send([content], [options])

content (StringResolvable): Text for the message.
options (MessageOptions or Attachment or RichEmbed): Options for the message, can also be just a RichEmbed or Attachment

现在,让我们看看您的用法如何适用。


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);

关于javascript - 如何在同一条消息中发送附件和嵌入内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57050277/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com