简体   繁体   English

更新机器人嵌入 - discord.js

[英]updating bot embed - discord.js

I want the bot to update the embed every 4 seconds and this is my code:我希望机器人每 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);
}

I tried this code but the bot is sending a new embed and not updating the last embed sent.我试过这段代码,但机器人正在发送一个新的嵌入,而不是更新最后发送的嵌入。

Here is how you should proceed.以下是您应该如何进行。 Storing the last sent message in a variable and use it to update the content.将最后发送的消息存储在变量中并使用它来更新内容。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM