NodeJS 正在更新机器人嵌入- discord.js

wn9m85ua  于 2022-12-22  发布在  Node.js
关注(0)|答案(1)|浏览(133)

我想机器人更新嵌入每4秒,这是我的代码:

if (cmd === 'fivemst') {
  function updateEmbed() {
    sys.probe('cfx.re', async (isAlive) => {
      if (isAlive) {
        embed = new MessageEmbed().setAuthor(
          'FiveM System Status -  ',
          'https://cdn.discordapp.com/attachments/1041091660316160032/1053741836725915818/82d62076a21ee0f408aa344403324efb5eb669cd.png',
        ).setDescription(`
         ✅

      **All Systems Operational**
      `);
      } else {
        embed = new MessageEmbed().setAuthor(
          'FiveM System Status - ',
          'https://cdn.discordapp.com/attachments/1041091660316160032/1053741836725915818/82d62076a21ee0f408aa344403324efb5eb669cd.png',
        ).setDescription(`
         ❎

      **Partial System Outage**
      `);
      }
      message.channel.send({ embeds: [embed] }).then((message) => {
        setTimeout(() => {
          message.edit({ embeds: [embed] });
        });
      }, 4000);
    });
  }
  setInterval(updateEmbed, 4000);
}

我尝试了这个代码,但机器人正在发送一个新的嵌入,而不是更新最后发送的嵌入。

u91tlkcl

u91tlkcl1#

下面是您应该如何继续操作:将最后发送的消息存储在一个变量中,并使用它来更新内容。

if (cmd === 'fivemst') {
    let message = null;
    function updateMessageWithNewEmbed() {
        sys.probe('cfx.re', async (isAlive) => {
            if (isAlive) {
                embed = new MessageEmbed().setAuthor(
                    'FiveM System Status -  ',
                    'https://cdn.discordapp.com/attachments/1041091660316160032/1053741836725915818/82d62076a21ee0f408aa344403324efb5eb669cd.png',
                ).setDescription(`
                    ✅

                **All Systems Operational**
                `);
            } else {
                embed = new MessageEmbed().setAuthor(
                    'FiveM System Status - ',
                    'https://cdn.discordapp.com/attachments/1041091660316160032/1053741836725915818/82d62076a21ee0f408aa344403324efb5eb669cd.png',
                ).setDescription(`
                    ❎

                **Partial System Outage**
                `);
            }
            if (message) message.edit({
                embeds: [embed]
            });
            else {
                message = await message.channel.send({
                    embeds: [embed]
                });
            }
        });
    }
    setInterval(() => updateMessageWithNewEmbed(), 4000);
}

相关问题