我想用嵌入消息进行投票
当有人添加反应时,我想添加一个喜欢并在嵌入中显示喜欢的数量。这是一个例子:
每当有人点击时,我的所有代码行都会起作用,我最终会改变链接的字段值,就像那样:
messageReaction.message.embeds[0].fields[0] = "Some much like";
但嵌入消息不会更新 我试图用这个更新消息:
function doAfakeEdit(message){
message.edit(message.content);
}
它仍保留该字段的旧值 我该怎么办?
答案 0 :(得分:4)
我想知道您的问题是您是否重新使用变量名称,将旧数据放回已编辑的消息或其他内容。无论如何,这里有一些对我有用的东西:
1)创建一个Embed
发送给用户(我假设你已经这样做了,创建了你在imgr上展示的Embed
):
const embed = new Discord.RichEmbed({
title: 'Suggestion by someone',
description: 'This is a test suggestion. Can you please like it or dislike it :)',
fields: [{
name: 'Like:',
value: '<3'
}]
});
2)将Embed
发送到您的频道(我向其添加了一些Reaction
- 可能与您的方式相同):
// add reaction emojis to message
message.channel.send(embed)
.then(msg => msg.react('✅'))
.then(mReaction => mReaction.message.react('❎'))
.then(mReaction => {
// fun stuff here
})
.catch(console.log);
3)在我放置ReactionCollector
的位置内创建// fun stuff here
(您可以使用不同的reactionFilter
和时间限制):
const reactionFilter = (reaction, user) => reaction.emoji.name === '✅';
// createReactionCollector - responds on each react, AND again at the end.
const collector = mReaction.message
.createReactionCollector(reactionFilter, {
time: 15000
});
// set collector events
collector.on('collect', r => {
// see step 4
});
// you can put anything you want here
collector.on('end', collected => console.log(`Collected ${collected.size} reactions`));
4)在'collect'
事件中(我放置// see step 4
),创建一个具有大致相似值的新Embed
(或不 - 你改变你想要的任何东西),然后把它新Embed
通过.edit(...)
返回原始邮件:
// immutably copy embed's 'Like:' field to new obj
let embedLikeField = Object.assign({}, embed.fields[0]);
// update 'field' with new value - you probably want emojis here
embedLikeField.value = '<3 <3 <3';
// create new embed with old title & description, new field
const newEmbed = new Discord.RichEmbed({
title: embed.title,
description: embed.description,
fields: [embedLikeField]
});
// edit message with new embed
// NOTE: can only edit messages you author
r.message.edit(newEmbed)
.then(newMsg => console.log(`new embed added`)) // this is not necessary
.catch(console.log); // useful for catching errors
所以整件事看起来像这样:
const reactionFilter = (reaction, user) => reaction.emoji.name === '✅';
const embed = new Discord.RichEmbed({
title: 'Suggestion by someone',
description: 'This is a test suggestion. Can you please like it or dislike it :)',
fields: [{
name: 'Like:',
value: '<3'
}]
});
// add reaction emoji to message
message.channel.send(embed)
.then(msg => msg.react('✅'))
.then(mReaction => mReaction.message.react('❎'))
.then(mReaction => {
// createReactionCollector - responds on each react, AND again at the end.
const collector = mReaction.message
.createReactionCollector(reactionFilter, {
time: 15000
});
// set collector events
collector.on('collect', r => {
// immutably copy embed's Like field to new obj
let embedLikeField = Object.assign({}, embed.fields[0]);
// update 'field' with new value
embedLikeField.value = '<3 <3 <3';
// create new embed with old title & description, new field
const newEmbed = new Discord.RichEmbed({
title: embed.title,
description: embed.description,
fields: [embedLikeField]
});
// edit message with new embed
// NOTE: can only edit messages you author
r.message.edit(newEmbed)
.then(newMsg => console.log(`new embed added`))
.catch(console.log);
});
collector.on('end', collected => console.log(`Collected ${collected.size} reactions`));
})
.catch(console.log);
对于我的代码,只有在按下✅表情符号时才会进行编辑,只是为了好玩。如果您需要帮助编辑上面的代码,请告诉我。希望它有所帮助。
答案 1 :(得分:0)
答案很晚。但是以防万一有人发现了这个。还有一种更短的方法。
如果您的嵌入较大且不想重建整个嵌入,则更有用:
message.embeds[0].fields[0] = "Some much like;
message.edit(new Discord.RichEmbed(message.embeds[0]));