Is it possible to format a value that we received from an array to make it something else

Viewed 77

My title doesn't really explain itself that much, so here is an explanation.

Currently, I have a command on my Discord bot that shows me Mojang's servers and their status.

Here is the code:

const Discord = require("discord.js");
const { get } = require("https");

module.exports.run = async(bot, message, args) => {
    //const member = message.mentions.members.first() || message.member

    get("https://status.mojang.com/check", (res) => {
        const { statusCode } = res;
        if (statusCode != 200) {
        res.resume;
        }
        res.setEncoding("utf8");
        let rawData = '';
        res.on("data", (chunk) => {
            rawData += chunk;
        });
        res.on("end", () => {
            try {
                const parsedData = JSON.parse(rawData);

                console.log(parsedData[0]);
                console.log(parsedData[1]);

                message.channel.send({
                    embed: {
                        color: 0xe61616,
                        title: `Mojang API Status`,
                        fields: [
                          {
                            name: "minecraft.net",
                            value: parsedData[0]['minecraft.net']
                          },
                          {
                            name: "session.minecraft.net",
                            value: parsedData[1]['session.minecraft.net']
                          },
                          {
                            name: "account.mojang.com",
                            value: parsedData[2]['account.mojang.com']
                          },
                          {
                            name: "authserver.mojang.com",
                            value: parsedData[3]['authserver.mojang.com']
                          },
                          {
                            name: "sessionserver.mojang.com",
                            value: parsedData[4]['sessionserver.mojang.com']
                          },
                          {
                            name: "api.mojang.com",
                            value: parsedData[5]['api.mojang.com']
                          },
                          {
                            name: "textures.minecraft.net",
                            value: parsedData[6]['textures.minecraft.net']
                          },
                          {
                            name: "mojang.com",
                            value: parsedData[7]['mojang.com']
                          }
                        ],
                        footer: {
                            text: `${bot.user.username} - Copyright 2021 - 2025`
                        }
                    }
                });

                console.log(parsedData);
            } catch (e) {
                console.error(e.message);
            }
        });
            }).on("error", (err) => {
                console.error(err.message);
            });
}

The result is what the array says, so minecraft.net green

[
  { "minecraft.net": "red" },
  { "session.minecraft.net": "green" }
]

But what I want is that when it shows, it's "minecraft.net: ". Is it possible to do that?

1 Answers

Of course you can format a value. As the response you receive from the API is an array of objects, where the key is the server name and the value is the colour/status, you could just check the value and if it's the string green, you replace it with a green emoji, and if it's the string red, replace it with a red one.

You could also use node-fetch instead of struggling with the built-in http module.

To get the key and value for each object, you can use the Object.entries() method. It returns an array of key-value pairs, so you'll need to grab the first one. To get the fields, you can iterate over the response and for each object, return a new object, where the name is the server's name, and the value is the emoji (based on the green/red value):

const fetch = require('node-fetch');
// ...
module.exports.run = async (bot, message, args) => {
  try {
    const res = await fetch('https://status.mojang.com/check');
    const json = await res.json();
    const statusEmojies = {
      green: '',
      red: '',
    };
    const fields = json.map((server) => {
      const [name, status] = Object.entries(server)[0];
      return {
        name: name,
        value: statusEmojies[status],
      };
    });
    const embed = new MessageEmbed()
      .setColor(0xe61616)
      .setTitle('Mojang API status')
      .addFields(fields);

    message.channel.send(embed);
  } catch (error) {
    console.log(error);
    message.channel.send('Oops, there was an error. Try again later!');
  }
};

enter image description here

You could also just create an array of strings instead and use it in the description. At least, I think it looks a bit better this way:

const fetch = require('node-fetch');
// ...
module.exports.run = async (bot, message, args) => {
  try {
    const res = await fetch('https://status.mojang.com/check');
    const json = await res.json();
    const statusEmojies = {
      green: '',
      red: '',
    };
    const fields = json.map((server) => {
      const [name, status] = Object.entries(server)[0];
      return `${statusEmojies[status]} ${name}`;
    });
    const embed = new MessageEmbed()
      .setColor(0xe61616)
      .setTitle('Mojang API status')
      .setDescription(fields.join('\n\n'));

    message.channel.send(embed);
  } catch (error) {
    console.log(error);
    message.channel.send('Oops, there was an error. Try again later!');
  }
};

enter image description here

Related