Непрерывное редактирование сообщения

#node.js #discord.js #embed

#node.js #discord.js #Внедрить

Вопрос:

Я кодировал бота и хотел вести обратный отсчет с определенного времени. Я правильно рассчитал определенное время, но у меня была одна проблема. Я не смог отредактировать сообщение так, чтобы в нем отображалось оставшееся время. Вот мой код на данный момент:

 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"
};
  

Если бы кто-нибудь мог помочь, это было бы полезно. Спасибо.

Ответ №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
)