嵌入消息不更新
Embed message doesn't update
我想用嵌入消息进行投票。
当有人添加反应时,我想添加一个喜欢并在嵌入中显示喜欢的数量。这里有一个例子:
每当有人点击赞时,我的所有代码行都会起作用,我最终会像这样更改链接到赞的字段值:
messageReaction.message.embeds[0].fields[0] = "Some much like";
但是嵌入消息没有更新。
我尝试用这个更新消息:
function doAfakeEdit(message){
message.edit(message.content);
}
它仍然保留该字段的旧值。
我该怎么办?
我想知道您的问题是否是重复使用变量名、将旧数据放回已编辑的消息或其他原因。无论如何,这对我有用:
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)在我放置// fun stuff here
的地方创建一个ReactionCollector
(你可以使用不同的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
,其值基本相似(或不相同 - 你可以随意更改),然后通过 .edit(...)
:
将新的 Embed
放回原始消息中
// 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);
对于我的代码,只有在按下 ✅ 表情符号时才会进行编辑,只是为了好玩。如果您需要帮助编辑上面的代码,请告诉我。希望对你有帮助。
很晚才回答。但以防万一有人发现这个。还有更短的方法。
如果您有大量嵌入且不想重建整个嵌入,则更有用:
message.embeds[0].fields[0] = "Some much like";
message.edit(new Discord.RichEmbed(message.embeds[0]));
我想用嵌入消息进行投票。
当有人添加反应时,我想添加一个喜欢并在嵌入中显示喜欢的数量。这里有一个例子:
每当有人点击赞时,我的所有代码行都会起作用,我最终会像这样更改链接到赞的字段值:
messageReaction.message.embeds[0].fields[0] = "Some much like";
但是嵌入消息没有更新。
我尝试用这个更新消息:
function doAfakeEdit(message){
message.edit(message.content);
}
它仍然保留该字段的旧值。
我该怎么办?
我想知道您的问题是否是重复使用变量名、将旧数据放回已编辑的消息或其他原因。无论如何,这对我有用:
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)在我放置// fun stuff here
的地方创建一个ReactionCollector
(你可以使用不同的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
,其值基本相似(或不相同 - 你可以随意更改),然后通过 .edit(...)
:
Embed
放回原始消息中
// 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);
对于我的代码,只有在按下 ✅ 表情符号时才会进行编辑,只是为了好玩。如果您需要帮助编辑上面的代码,请告诉我。希望对你有帮助。
很晚才回答。但以防万一有人发现这个。还有更短的方法。
如果您有大量嵌入且不想重建整个嵌入,则更有用:
message.embeds[0].fields[0] = "Some much like";
message.edit(new Discord.RichEmbed(message.embeds[0]));