[英]Embed message doesn't update
我想用嵌入的消息進行投票。
當有人添加反應時,我想添加一個贊並顯示嵌入中的贊數。 這里有一個例子:
每當有人點擊喜歡時,我所有的代碼行都可以工作,我最終會像這樣更改鏈接到的字段值:
messageReaction.message.embeds[0].fields[0] = "Some much like";
但嵌入消息不會更新。
我試圖用這個更新消息:
function doAfakeEdit(message){
message.edit(message.content);
}
它仍然保留字段的舊值。
我該怎么辦?
很晚的答案。 但以防萬一有人發現這個。 有一個更短的方法。
如果您有大型嵌入並且不想重建整個嵌入,則更有用:
message.embeds[0].fields[0] = "Some much like";
message.edit(new Discord.RichEmbed(message.embeds[0]));
我想知道您的問題是您重新使用變量名稱,將舊數據放回編輯過的消息中,還是其他原因。 無論如何,這里有一些對我有用的東西:
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
s - 可能與您的方式相同):
// 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);
對於我的代碼,只有在按下 ✅ 表情符號時才會進行編輯,只是為了好玩。 如果您需要幫助編輯上面的代碼,請告訴我。 希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.