cloud-list
Version:
official module of the website cloudlist.xyzSimple post and get request method
109 lines (99 loc) • 3.36 kB
JavaScript
var https = require('https');
const { EventEmitter } = require("events");
var fetch = require('node-fetch')
module.exports = class Cloud_client extends EventEmitter {
constructor(id, token) {
super();
if (!id) throw new Error("Missing client instance on contructor");
if (!token) throw new Error("Missing token on constructor");
this.id = id;
this.token = token;
this.io = null;
this.io = require("socket.io-client")(`https://www.cloudlist.xyz/vote/${id}`, {
reconnection:true,
forceNew:true,
reconnectionDelay: 1000,
reconnectionDelayMax : 5000,
reconnectionAttempts: Infinity
});
this.io.on("connect", () => {
this.emit("connected")
});
this.io.on("reconnecting", (...args) => { this.emit("reconnecting") });
this.io.on("disconnect", (...args) => { this.emit("disconnected") });
this.io.on("Conectado", (...args) => {this.emit("connected",...args)});
this.io.on("voted", (...args) => this.emit("voted", ...args))};
autoPost(client) {
const id = this.id
if(!this.id) return console.error(`[cloud-list] No Client id provided`)
const token = this.token
setInterval(function() {
const body = { count: client.guilds.size };
fetch(`https://www.cloudlist.xyz/api/stats/${id}`, {
method: "POST",
headers: {
Authorization: `${token}`,
"Content-Type": "application/json"
},
body:JSON.stringify(body),
}).then(res => res.json())
.then(async response => {
console.log(`[cloud-list] ${response}`)
return this;
}).catch(async function(err) {
if(err) return console.error(`[cloud-list] ${err}`);
})
},120 * 1000)
//Single Post
}
post(client) {
const id = this.id
if(!client) return console.error(`[cloud-list] No Client provided`)
const body = { count: client.guilds.size };
fetch(`https://www.cloudlist.xyz/api/stats/${id}`, {
method: "POST",
headers: {
Authorization: `${this.token}`,
"Content-Type": "application/json"
},
body:JSON.stringify(body),
}).then(res => res.json())
.then(async response => {
console.log(`[cloud-list] ${response}`)
return this;
}).catch(async function(err) {
if(err) return console.error(`[cloud-list] ${err}`);
})
}
// Get stats from bot
getStats(id) {
if(!id) return console.error(`[cloud-list] No Id provided`)
return new Promise(function(resolve, reject) {
fetch(`https://www.cloudlist.xyz/api/bot/${id}`, {
method: "GET",
headers: {
"Content-Type": "application/json"
},
}).then(res => res.json())
.then(async response => {
resolve(response);
})
})
}
// Check if someone Voted
hasVoted(user_id) {
if(!user_id) return console.error(`[cloud-list] No User_Id Provided`)
var client_id = this.id;
return new Promise(function(resolve, reject) {
fetch(`https://www.cloudlist.xyz/api/bot/${client_id}/hasvoted/${user_id}`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
}).then(res => res.json())
.then(async response => {
resolve(response);
})
})
}
}