我试图在保持所有其他字段的值不变的情况下触发对嵌入(已发送)消息的编辑
我发现这个答案是一个灵感(它与示例一起工作):Update embed fields with DiscordJS,但这似乎没有得到所有的字段,只有第一个。关于这个主题没有更多的东西可以找到(或者我不擅长谷歌搜索:))。
因此,新的嵌入只是第一个字段,而不是所有(未更改的)字段。
activityMsg = new Discord.RichEmbed({
title: 'Some text',
description: 'Description',
color: 3447003,
footer: {
icon_url: image,
text: image
},
thumbnail: {
url: image
},
fields: [
{
name: 'Text',
value: 'Text2',
},
{
name: 'Date and time',
value: '2pm',
},
{
name: 'Participants',
value: '@User',
},
{
name: 'Waiting list',
value: '@user2',
},
{
name: 'Max players',
value: '22',
}
]
});
const reactionFilterPlus = (reaction, user) => reaction.emoji.name === emoji_plus;
if(typeof title != undefined && title != null && data.length == 4 && error == ''){
var title = title[0].replace('[','').replace(']','');
// add reaction emoji to message
msg.channel.send(activityMsg)
.then(msg => msg.react(constants.emoji_plus))
.then(mReaction => {
// createReactionCollector - responds on each react, AND again at the end.
const collector = mReaction.message
.createReactionCollector(reactionFilterPlus, {
time: 15000
});
// set collector events
collector.on('collect', r => {
// immutably copy embed's Like field to new obj
let embedLikeField = Object.assign({}, activityMsg.fields[0]);
// update 'field' with new value
embedLikeField.value = `${user} <3`;
// create new embed with old title & description, new field
const newEmbed = new Discord.RichEmbed({
title: activityMsg.title,
description: activityMsg.description,
fields: [embedLikeField]
});
// edit message with new embed
// NOTE: can only edit messages you author
r.message.edit(newEmbed)
.catch(console.log);
});
})
.catch(console.log);
}
我希望这行能得到所有字段,但事实并非如此。
// immutably copy embed's Like field to new obj
let embedLikeField = Object.assign({}, activityMsg.fields[0]);
我已经尝试了let embedLikeField = Object.assign({}, activityMsg.fields[0] === 'Participants')
,但我得到了以下关于字段名不存在的错误。
{ DiscordAPIError: Invalid Form Body
embed.fields[0].name: This field is required
at item.request.gen.end (/usr/src/app/node_modules/discord.js/src/client/rest/RequestHandlers/Sequential.js:79:15)
at then (/usr/src/app/node_modules/snekfetch/src/index.js:215:21)
at process._tickCallback (internal/process/next_tick.js:68:7)
英语不是我的母语,我还在学习nodejs。
1条答案
按热度按时间pepwfjgg1#
assign()在源代码上执行浅克隆,您是要克隆整个嵌入字段还是仅克隆其第一个字段?
activityMsg.fields[0]
引用activityMsg对象中名为fields的列表中的第一个元素。尝试使用activityMsg作为源调用assign()。