1

So I have this Discord Bot command that displays the user's servers from an API: The "GetAllServers" returns A JSON that for me has two objects in but this could go up or down. The "GetServerStatus" Returns Either "on", "off or "starting".

I want to be able to display a nicely formatted version of the "on", "off" or starting into a discord embed along with some other values retrieved from GetserverStatus.

To display the information in an embed I have written a loop to go through each individual server and retrieve the status for the server and retrieve the information and post it as a separate field in an embed.

Here is my code:

const servers = new Discord.MessageEmbed()
  .setColor(2718207)
  .setTitle(`${user.username}'s Servers`)
  .setTimestamp()
  .setFooter(
    `Test`,
    `https://test.com`
  );

Client.getAllServers().then((response) => {
  response.forEach(function (element) {
    Client.getServerStatus(element.attributes.identifier).then(
      (Server_Status) => {
        console.log(Server_Status);
        if (Server_Status === "off") {
          servers.addField(
            `__${element.attributes.name}__`,
            `**Identifier:** ${element.attributes.identifier}\n**Server Status:** Offline\n**Players Online:** 0/30\n`,
            true
          );
        } else if (Server_Status === "on") {
          servers.addField(
            `__${element.attributes.name}__`,
            `**Identifier:** ${element.attributes.identifier}\n**Server Status:** Online\n**Players Online:** 0/30\n`,
            true
          );
        } else if (Server_Status === "starting") {
          servers.addField(
            `__${element.attributes.name}__`,
            `**Identifier:** ${element.attributes.identifier}\n**Server Status:** Starting\n**Players Online:** 0/30\n`,
            true
          );
          Console.log("Starting");
        }
      }
    );
  });
  message.channel.send(server);
});

the issue I am having is that the if statement isn't running although the correct values are returned of the GetServerStatus. I have tested many times by just console.log(Server_Status) and it always returns either "on" "off" or starting". However when trying them in an if statement it doesn't work.

1
  • Not sure if this will help, but from your question I gather that the variable is defined from a for loop? You can try defining the variable before the loop, and set it to null. Then the for loop is only reassigning it’s value. Don’t know if this really answers your question though? Commented Jun 12, 2020 at 8:07

1 Answer 1

1

Client.getServerStatus() is an async function, you'll need to wait for it to complete before trying to send the message.

Try the following

response.forEach(async function (element) {
  let Server_Status = await Client.getServerStatus(element.attributes.identifier);
  console.log(Server_Status);
  if (Server_Status === 'off') {
    servers.addField(
      `__${element.attributes.name}__`,
      `**Identifier:** ${element.attributes.identifier}\n**Server Status:** Offline\n**Players Online:** 0/30\n`,
      true
    );
  } else if (Server_Status === 'on') {
    servers.addField(
      `__${element.attributes.name}__`,
      `**Identifier:** ${element.attributes.identifier}\n**Server Status:** Online\n**Players Online:** 0/30\n`,
      true
    );
  } else if (Server_Status === 'starting') {
    servers.addField(
      `__${element.attributes.name}__`,
      `**Identifier:** ${element.attributes.identifier}\n**Server Status:** Starting\n**Players Online:** 0/30\n`,
      true
    );
    Console.log('Starting');
  }
});
message.channel.send(server);
Sign up to request clarification or add additional context in comments.

6 Comments

They are not Async Functions apparently when i replace that line, i get a error "await is only valid in async function".
That means you need to make the inner most function async, function test => async function test
What do you mean the most inner function, the command function i have tried it with and it says "invalid variable type"
check my edit, hopefully that made it clear
this still didnt add the elements to the embed. : (
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.