Than instead of printing it send it to the channel or do whatever you want it to show up
Ok soooo if i want it in the status ill do that and “yourArray” ill just call players then ill do the other things you send or?
OK so ill start off from the start. Im parsing the json object. What should i parse? I have written this:
var obj = JSON.parse('{"what to write here?"}');
1 player online = length 1 = 1/64 online
2 player online = length 2 = 2/64 online // and so on
bot.user.setActivity(`(yourArray.length)`, {
type: 'WATCHING'
});
});
Is this all wrong? I think the (yourArray,length) is not correct but i don’t seem to understand it.
I assume you don’t have a programming background, this is not valid syntax
I’ll explain first why and give you an example after
This is not actual syntax, it was an example from me, what you want to do is just get the length of the “array” and that would be the playercount
An array can be seen as a group with elements and you can count them
This was also an example and shouldn’t be used as that because
- It’s used as string while it’s code
- yourArray doesn’t exists, this should be obj for example
A working example below, this looks pretty long but that’s because it uses built-in http libs it can be shorter if you for example use a library like express (feel free to ask what anything does)
const http = require("http")
http.get("http://yourserverip:30120/players.json", function(response){
let json = ""
// receive everything from the server
response.on("data", function(chunk){
json += chunk
})
// if received everything
response.on("end", function(){
if(response.statusCode == 200){
try {
// we parse the response to an "array"
const players = JSON.parse(json)
// get the length (so count) and set it as status
bot.user.setActivity(`${players.length}/64 players`, {
type: 'WATCHING'
})
}catch(e){
console.log("Something got wrong")
}
}
})
})
1 Like
When i use this code it logs to the console “Something got wrong” i have changed the ip and the text nothing else. Any idea?
I wrote it like this:
const http = require(“http”);
http.get(“http://144.76.127.20:30160/players.json”, function(response){
let json = “”
// receive everything from the server
response.on("data", function(chunk){
json += chunk
})
// if received everything
response.on("end", function(){
if(response.statusCode == 200){
try {
// we parse the response to an "array"
const players = JSON.parse(json)
// get the length (so count) and set it as status
bot.user.setActivity(`${players.length}/64 online`, {
type: 'WATCHING'
})
}catch(e){
console.log("Noget gik galt med Status")
}
}
})
})
Where is this code and does it know about the bot variable?
Yep, i can send the whole start in a pastebin.
Pastebin: https://pastebin.com/RZX0K7Gy
Make sure you put it inside the bot.on(“ready” callback so it doesn’t try to while it’s still connecting
It works thank you so beep much
You are the best!
How to make some kind of loop to do an actualisation of the player list for exemple every 10 seconds ?
setInterval(function(){
// code here to update
}, 10 * 1000)
// 10 = seconds, i suggest you maybe put that little higher like 30-45 otherwise you might flood the server endpoint
If am right, I should set inside the setInterval the const players = JSON.parse(json)?
system
Closed
36
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.