Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

140
Vistas
Trying to get value from object using external url but

When i start bot console say:

Online with undefined/5

and when 10 secconds are passed give this error:

undefined:1 [object Promise] ^ SyntaxError: Unexpected token o in JSON at position 1

My Code:

let jogadores
client.on("ready", async () => {

async function players_online() {
    const response = fetch(`http://ip:port/dynamic.json`);
    const data = JSON.parse(response);
    jogadores = data.clients;


}

async function autoconnect() {

    if (jogadores === -1) {
        console.log('Offline');

    } else {
        console.log('Online with ' + jogadores + '/5')

    }
}
autoconnect()

setInterval(() => {
    client.user.setStatus('dnd');
    client.user.setActivity(`Online with ${jogadores} players.`, { type: 'PLAYING' })
    players_online()
}, 10000)

})
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You have quite some problems with your code. I will list them below:

Required fixes

  • As @about14sheep said, you must use await to get the actual response.
  • You are not supposed to use JSON.parse() on a response, but rather response.json() to get the JSON body.
  • Use of an incorrect operator, for numbers I'd suggest you use == than ===
  • Before 10 seconds have passed, it will show undefined. To fix this, call the function before the setInterval().

Optional things I added

  • Formatted JS code
  • Added semicolons when necessary
  • Used ES6 template literals feature
  • Removed async when un-needed

I have fixed all of these problems in the code below.

let jogadores;

client.on('ready', async () => {

    async function players_online() {
        const response = await fetch(`http://ip:port/dynamic.json`);
        const data = await response.json();
        jogadores = data.clients;
    }

    function autoconnect() {
        if (jogadores == -1) {
            console.log('Offline');
        } else {
            console.log(`Online with ${jogadores}/5`);
        }
    }

    autoconnect();
    players_online();

    setInterval(() => {
        client.user.setStatus('dnd');
        client.user.setActivity(`Online with ${jogadores} players.`, {
            type: 'PLAYING'
        });
    }, 10000);

});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda