discord.js-如何编辑message.embed()语句?

2022-02-22 00:00:00 discord embed javascript discord.js
我正在执行ping命令- 它的代码非常简单,但我一点也不知道如何编辑我正在使用的嵌入。以下是我的代码-我使用一个命令处理程序来解释exports.run语句。

const Discord = require('discord.js')

exports.run = (bot, message, args) => {  
const pingUpdate = new Discord.MessageEmbed()
.setColor('#0099ff')
.setDescription('pinging...')
message.channel.send(pingUpdate);
}

exports.help = {
  name: 'ping'
}

我需要编辑嵌入的ping更新以编辑.description以执行此操作(简单的ping计算)

message.channel.send('pinging...').then((m) => m.edit(`${m.createdTimestamp - message.createdTimestamp}ms`))

这将使描述从"ping."更改为"ping."到"示例ms"

提前感谢您


解决方案

实际上不必创建新嵌入。您可以编辑原件:

更新:根据the docs,建议创建新嵌入,但您可以使用原始嵌入预先填充新嵌入。然后,只需更新您需要的内容并使用新嵌入编辑邮件:

// the original embed posted during collected.on('end')
const embed = new MessageEmbed()
                    .setColor('0xff4400')
                    .setTitle(`My Awesome Embed`)
                    .setDescription('u200b')
                    .setAuthor(collected.first().user.username, collected.first().user.displayAvatarURL())
                    .setImage(`https://via.placeholder.com/400x300.png/808080/000000?text=Placeholder`)
                    .setTimestamp(new Date())
                    .setThumbnail('https://via.placeholder.com/200x200.png/808080/000000?text=Placeholder');

// In the original embed, I have a placeholder image that the user
// can replace by posting a new message with the image they want
client.on('messageCreate', async message => {
    // adding image to original embed
    if (message.attachments.size > 0 && !message.author.bot) {
        // get all messages with attachments
        const messages = await client.channels.cache.get('<CHANNEL>').messages.fetch();
            
        // get the newest message by the user
        const d = messages.filter(msg => msg.embeds.length > 0).filter(m => message.author.username === m.embeds[0].author.name).first();
        
        // create new embed using original as starter
        const tempEmbed = new MessageEmbed(d.embeds[0])

        // update desired values
        tempEmbed.setImage(message.attachments.first().url);
    
        // edit/update the message
        d.edit({ embeds: [tempEmbed] });
        
        // delete the posted image
        message.delete();
    }
});

相关文章