Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda