作者:EMBRACE-老王 | 来源:互联网 | 2022-12-15 11:11
我想用嵌入的消息进行表决。
当有人添加反应时,我想添加一个喜欢并显示嵌入中的喜欢数量。这里是一个例子:
每当有人点击like时,我所有的代码行都会工作,而我最终将所链接的Field值更改为:
messageReaction.message.embeds[0].fields[0] = "Some much like";
但是嵌入消息不会更新。
我尝试使用以下方法更新消息:
function doAfakeEdit(message){
message.edit(message.content);
}
它仍然保留该字段的旧值。
我该怎么办?
1> Blundering P..:
我想知道您的问题是否在于您是否正在重新使用变量名,将旧数据放回已编辑的消息中或其他原因。无论如何,这对我有用:
1)创建一个Embed
发送给用户(我假设您已经做过,创建Embed
您在imgr上显示的):
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);
对于我的代码,仅在?时进行编辑。按下表情符号只是为了好玩。如果您需要编辑以上代码的帮助,请告诉我。希望能帮助到你。
嗯,这只是“编辑新嵌入的消息”,这是一条很长的消息,但是它起作用了:)谢谢;