【问题标题】:Continuously Editing a Message连续编辑消息
【发布时间】:2020-12-16 21:08:48
【问题描述】:

我正在编写一个机器人,我想从某个时间开始实时倒计时。我得到了正确的时间,但我有一个问题。我无法编辑消息以显示剩余时间。这是我目前的代码:

module.exports.run = async (client, message, args) => {
let time = client.db.get(`time`)

const exampleEmbed = new Discord.MessageEmbed()
    .setColor('#ff74fc')
    .setTitle('V2 Release Time')
    .setDescription('This command will be showing the countdown until the V2 cafe releases! ')
    .setThumbnail('https://media.discordapp.net/attachments/708130362122829876/746736868124524584/image0.jpg')
    .addFields(
        { name: 'Time', value: `${time} Seconds Left!` },
    )
    .setTimestamp()
    .setFooter('Flamgo Bot');

 return message.channel.send(exampleEmbed)

  const Edit = new Discord.MessageEmbed()
    .addFields(
        { name: 'Time', value: `${time} Seconds Left!` },
    )

 message.edit(Edit)


}
module.exports.help = {
 name: "v2time"
};

如果有人可以提供帮助,那将很有帮助。谢谢。

【问题讨论】:

    标签: node.js discord.js embed


    【解决方案1】:

    编辑消息时,您需要提供要发送的完整消息。目前,由于Edit 是一个仅包含 1 个“时间”字段的嵌入,因此消息将仅使用该字段进行更新,并且所有先前的字段和其他嵌入内容都将被删除。

    试试这个:

    const createEmbed = time => new Discord.MessageEmbed()
      .setColor('#ff74fc')
      .setTitle('V2 Release Time')
      .setDescription('This command will be showing the countdown until the V2 cafe releases! ')
      .setThumbnail('https://media.discordapp.net/attachments/708130362122829876/746736868124524584/image0.jpg')
      // addField can be used for adding a single field
      .addField('Time', `${time} Seconds Left!`)
      .setTimestamp()
      .setFooter('Flamgo Bot');
    
    // Usage:
    message.channel.send({embeds: [createdEmbed(client.db.get('time'))]});
    message.edit({embeds: [createEmbed(client.db.get('time'))]});
    // For Discord.js v12 use .send(embed)/.edit(embed)
    

    例如,如果你想不断更新这个嵌入,你可以这样做:

    /**
     * The timestamp of the date that you're counting down to
     * @type {number}
     */
    const releaseDate = // ...
    
    setInterval(() =>
      message.edit({embeds: [createEmbed(Math.round((releaseDate - Date.now()) / 1000))]})
        .catch(console.error),
      1000
    )
    

    【讨论】:

      猜你喜欢
      • 2018-10-23
      • 2021-03-31
      • 2023-04-06
      • 1970-01-01
      • 2021-05-08
      • 1970-01-01
      • 1970-01-01
      • 2019-11-17
      • 2021-04-26
      相关资源
      最近更新 更多