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)
})
You have quite some problems with your code. I will list them below:
await to get the actual response.JSON.parse() on a response, but rather response.json() to get the JSON body.== than ===setInterval().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);
});