This commit is contained in:
Hirzi
2021-07-20 19:30:26 +07:00
committed by GitHub
parent 5962ad9588
commit 5ab426ce5b
+198 -239
View File
@@ -1,11 +1,13 @@
module.exports = client => {
//Code are very sensitive, please changes things on config.yml instead
const { MessageEmbed } = require('discord.js')
const axios = require('axios')
const db = require('quick.db')
const nodetable = new db.table('nodetable')
const chalk = require('chalk')
const config = client.config
const nodelist = client.nodelist
let enablecs = config.botstatus.enable
let cs = config.botstatus.text
@@ -14,7 +16,6 @@ module.exports = client => {
let time = config.refreshtime
let hosturl = config.panel.url
let apikey = config.panel.clientkey
let adminapikey = config.panel.adminkey
let statusonline = config.status.online
@@ -63,266 +64,224 @@ module.exports = client => {
console.log(chalk.green('Version: ') + chalk.cyan('Stable v1.4.0'))
console.log(chalk.green('Refresh Time: ') + chalk.cyan(time + ' Seconds'))
console.log(chalk.green('Bot Status: ') + chalk.cyan('Online'))
console.log(chalk.green('Support: ') + chalk.cyan('https://discord.gg/9Z7zpdwATZ'))
console.log(chalk.green('Support: ') + chalk.cyan('https://discord.gg/zv6maQRah3'))
console.log(chalk.red('=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+='))
setInterval(() => {
if (isNaN(time)) return console.log(chalk.cyan('[PteroStats Checker] ') + chalk.red(time + ' is not a number!'))
if (!hosturl.includes('.')) return console.log(chalk.cyan('[PteroStats Checker] ') + chalk.red(hosturl + ' is invalid url!'))
if (apikey.length < 48) return console.log(chalk.cyan('[PteroStats Checker] ') + chalk.red('Invalid Client Apikey!!'))
if (adminapikey.length < 48) return console.log(chalk.cyan('[PteroStats Checker] ') + chalk.red('Invalid Admin Apikey!!'))
let list = []
nodelist.forEach(data => {
if (data.id === 'Server ID') return console.log(chalk.cyan('[PteroStats Checker] ') + chalk.red('You need to use a valid server'))
axios(api + '/client/servers/' + data.id + '/resources', {
axios(api + '/application/nodes/', {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: 'Bearer ' + adminapikey
}
}).then(response => {
let data = response.data.data
data.forEach(nodes => {
let id = nodes.attributes.id
axios(api + '/application/nodes/' + id + '?include=servers,location,allocations', {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: 'Bearer ' + adminapikey
}
}).then(node => {
axios(api + '/application/nodes/' + id + '/configuration', {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: 'Bearer ' + adminapikey
}
}).then(data => {
axios(node.data.attributes.scheme + '://' + node.data.attributes.fqdn + ':' + node.data.attributes.daemon_listen + '/api/servers', {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: 'Bearer ' + data.data.token
}
}).then(status => {
let ram = 'temp'
let disk = 'temp'
const mode = node.data.attributes.maintenance_mode
const loc = '[Locations: ' + node.data.attributes.relationships.location.attributes.short + ']'
const port = '[Allocations: ' + node.data.attributes.relationships.allocations.data.length + ']'
const servers = '[Servers: ' + node.data.attributes.relationships.servers.data.length + ']'
const rampercent = '[Ram: ' + Math.floor(node.data.attributes.allocated_resources.memory / node.data.attributes.memory * 100) + '%/100%]'
const diskpercent = '[Disk: ' + Math.floor(node.data.attributes.allocated_resources.disk / node.data.attributes.disk * 100) + '%/100%]'
const rammega = '[Ram: ' + node.data.attributes.allocated_resources.memory + 'MB/' + node.data.attributes.memory + 'MB]'
const diskmega = '[Disk: ' + node.data.attributes.allocated_resources.disk + 'MB/' + node.data.attributes.disk + 'MB]'
const ramgiga = '[Ram: ' + Math.floor(node.data.attributes.allocated_resources.memory / 1000) + 'GB/' + Math.floor(node.data.attributes.memory / 1000) + 'GB]'
const diskgiga = '[Disk: ' + Math.floor(node.data.attributes.allocated_resources.disk / 1000) + 'GB/' + Math.floor(node.data.attributes.disk / 1000) + 'GB]'
if (unit === 'mb') {
disk = diskmega
ram = rammega
}
if (unit === 'gb') {
disk = diskgiga
ram = ramgiga
}
if (unit === 'percent') {
disk = diskpercent
ram = rampercent
}
nodetable.set('node' + id, {
ram: ram,
disk: disk,
status: true,
servers: servers,
location: loc,
port: port,
mode: mode
})
}).catch((err) => {
let servers = '[Servers: N/A]'
let loc = '[Location: N/A]'
let port = '[Allocations: N/A]'
let ram = '[Ram: N/A]'
let disk = '[Disk: N/A]'
console.log(chalk.cyan('[PteroStats Checker] ') + chalk.red(node.data.attributes.name + ' is down!'))
if (debugerror === true) console.log(chalk.magenta('[PteroStats Debug] ') + chalk.red(err) + chalk.cyan(' Need Support? https://discord.gg/zv6maQRah3'))
nodetable.set('node' + id, {
ram: ram,
disk: disk,
status: false,
servers: servers,
location: loc,
port: port,
mode: false
})
})
}).catch((err) => {
console.log(chalk.magenta('[PteroStats Debug] ') + chalk.red(err) + chalk.cyan(' Need Support? https://discord.gg/zv6maQRah3'))
})
}).catch((err) => {
console.log(chalk.magenta('[PteroStats Debug] ') + chalk.red(err) + chalk.cyan(' Need Support? https://discord.gg/zv6maQRah3'))
})
let stats = nodetable.get('node' + id)
let msgStats = ''
if (!stats) msgStats = '**' + nodes.attributes.name + '**: ' + checking
if (stats) {
let statsname = '**' + nodes.attributes.name + '**: '
if (stats.status === true) statsname = statsname + statusonline
if (stats.status === false) statsname = statsname + statusoffline
if (stats.mode === true) statsname = stats.status + " [Maintance]"
if (resource === true) statsname = statsname + '\n```\n' + stats.ram + '\n' + stats.disk
if (serverloc === true) statsname = statsname + '\n' + stats.location
if (serverport === true) statsname = statsname + '\n' + stats.port
if (serverres === true) statsname = statsname + '\n' + stats.servers
if (resource === false) statsname = statsname + '\n'
if (resource === true) msgStats = statsname + '```\n'
}
if (debug === true) console.log(chalk.magenta('[PteroStats Debug] ') + chalk.blue(nodes.attributes.name + ': ' + stats.status))
list.push(msgStats)
})
axios(api + '/application/servers', {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: 'Bearer ' + apikey
Authorization: 'Bearer ' + adminapikey
}
}).then((response) => {
axios(api + '/application/nodes/' + data.nodeid + '?include=servers,location,allocations', {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: 'Bearer ' + adminapikey
}
}).then(response => {
let ram = 'temp'
let disk = 'temp'
const mode = response.data.attributes.maintenance_mode
const loc = '[Locations: ' + response.data.attributes.relationships.location.attributes.short + ']'
const port = '[Allocations: ' + response.data.attributes.relationships.allocations.data.length + ']'
const servers = '[Servers: ' + response.data.attributes.relationships.servers.data.length + ']'
const rampercent = '[Ram: ' + Math.floor(response.data.attributes.allocated_resources.memory / response.data.attributes.memory * 100) + '%/100%]'
const diskpercent = '[Disk: ' + Math.floor(response.data.attributes.allocated_resources.disk / response.data.attributes.disk * 100) + '%/100%]'
const rammega = '[Ram: ' + response.data.attributes.allocated_resources.memory + 'MB/' + response.data.attributes.memory + 'MB]'
const diskmega = '[Disk: ' + response.data.attributes.allocated_resources.disk + 'MB/' + response.data.attributes.disk + 'MB]'
const ramgiga = '[Ram: ' + Math.floor(response.data.attributes.allocated_resources.memory / 1000) + 'GB/' + Math.floor(response.data.attributes.memory / 1000) + 'GB]'
const diskgiga = '[Disk: ' + Math.floor(response.data.attributes.allocated_resources.disk / 1000) + 'GB/' + Math.floor(response.data.attributes.disk / 1000) + 'GB]'
if (unit === 'mb') {
disk = diskmega
ram = rammega
}
if (unit === 'gb') {
disk = diskgiga
ram = ramgiga
}
if (unit === 'percent') {
disk = diskpercent
ram = rampercent
}
let status = '**' + data.name + '**: ' + statusonline
nodetable.set(data.nameid, {
ram: ram,
disk: disk,
status: status,
servers: servers,
location: loc,
port: port,
mode: mode
})
}).catch(err => {
let status = '**' + data.name + '**: ' + statusonline
let servers = '[Servers: N/A]'
let loc = '[Location: N/A]'
let port = '[Allocations: N/A]'
let ram = '[Ram: N/A]'
let disk = '[Disk: N/A]'
console.log(chalk.cyan('[PteroStats Checker] [Node Resource] ') + chalk.red(data.name + ' node resource is down, make sure you put right id or vaild node id!'))
if (debugerror === true) console.log(chalk.magenta('[PteroStats Debug] [Node Resource] ') + chalk.red(err))
nodetable.set(data.nameid, {
ram: ram,
disk: disk,
status: status,
servers: servers,
location: loc,
port: port,
mode: false
})
})
}).then(ser => {
let res = ser.data.meta.pagination.total
db.set('serverCount', res)
}).catch((err) => {
console.log(chalk.cyan('[PteroStats Checker] [Node Status]') + chalk.red(data.name + ' is down!'))
if (debugerror === true) console.log(chalk.magenta('[PteroStats Debug] [Node Status] ') + err)
axios(api + '/application/nodes/' + data.nodeid + '?include=servers,location,allocations', {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: 'Bearer ' + adminapikey
}
}).then(response => {
let ram = 'temp'
let disk = 'temp'
const mode = response.data.attributes.maintenance_mode
const loc = '[Locations: ' + response.data.attributes.relationships.location.attributes.short + ']'
const port = '[Allocations: ' + response.data.attributes.relationships.allocations.data.length + ']'
const servers = '[Servers: ' + response.data.attributes.relationships.servers.data.length + ']'
const rampercent = '[Ram: ' + Math.floor(response.data.attributes.allocated_resources.memory / response.data.attributes.memory * 100) + '%/100%]'
const diskpercent = '[Disk: ' + Math.floor(response.data.attributes.allocated_resources.disk / response.data.attributes.disk * 100) + '%/100%]'
const rammega = '[Ram: ' + response.data.attributes.allocated_resources.memory + 'MB/' + response.data.attributes.memory + 'MB]'
const diskmega = '[Disk: ' + response.data.attributes.allocated_resources.disk + 'MB/' + response.data.attributes.disk + 'MB]'
const ramgiga = '[Ram: ' + Math.floor(response.data.attributes.allocated_resources.memory / 1000) + 'GB/' + Math.floor(response.data.attributes.memory / 1000) + 'GB]'
const diskgiga = '[Disk: ' + Math.floor(response.data.attributes.allocated_resources.disk / 1000) + 'GB/' + Math.floor(response.data.attributes.disk / 1000) + 'GB]'
if (unit === 'mb') {
disk = diskmega
ram = rammega
}
if (unit === 'gb') {
disk = diskgiga
ram = ramgiga
}
if (unit === 'percent') {
disk = diskpercent
ram = rampercent
}
let status = '**' + data.name + '**: ' + statusoffline
nodetable.set(data.nameid, {
ram: ram,
disk: disk,
status: status,
servers: servers,
location: loc,
port: port,
mode: mode
})
}).catch(err => {
let status = '**' + data.name + '**: ' + statusoffline
let servers = '[Servers: N/A]'
let port = '[Allocations: N/A]'
let loc = '[Location: N/A]'
let ram = '[Ram: N/A]'
let disk = '[Disk: N/A]'
console.log(chalk.cyan('[PteroStats Checker] [Node Resource] ') + chalk.red(data.name + ' node resource is down, make sure you put right id or vaild node id!'))
if (debugerror === true) console.log(chalk.magenta('[PteroStats Debug] [Node Resource] ') + chalk.red(err))
nodetable.set(data.nameid, {
ram: ram,
disk: disk,
status: status,
servers: servers,
location: loc,
port: port,
mode: false
})
})
db.set('serverCount', 'N/A')
console.log(chalk.cyan('[PteroStats Checker] ') + chalk.red('Panel is down'))
if (debugerror === true) console.log(chalk.magenta('[PteroStats Debug] ') + err)
})
let stats = nodetable.get(data.nameid)
let msgStats = ''
if (!stats) msgStats = '**' + data.name + '**: ' + checking
if (stats) {
let statsname = stats.status
axios(api + '/application/users', {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: 'Bearer ' + adminapikey
}
}).then(usr => {
let res = usr.data.meta.pagination.total
db.set('userCount', res)
}).catch((err) => {
db.set('userCount', 'N/A')
console.log(chalk.cyan('[PteroStats Checker] ') + chalk.red('Panel is down!'))
if (debugerror === true) console.log(chalk.magenta('[PteroStats Debug] ') + err)
})
if (stats.mode === true) statsname = stats.status + " [Maintance]"
let userCount = db.get('userCount')
let serverCount = db.get('serverCount')
if (resource === true) statsname = statsname + '\n```\n' + stats.ram + '\n' + stats.disk
if (serverloc === true) statsname = statsname + '\n' + stats.location
if (serverport === true) statsname = statsname + '\n' + stats.port
if (serverres === true) statsname = statsname + '\n' + stats.servers
if (resource === false) statsname = statsname + '\n'
if (userCount === null) userCount = checking
if (serverCount === null) serverCount = checking
if (resource === true) msgStats = statsname + '```\n'
if (userCount !== 'N/A') db.set('panel', '**Panel**: ' + statusonline)
if (userCount === 'N/A') {
db.set('panel', '**Panel**: ' + statusoffline)
console.log(chalk.cyan('[PteroStats Checker] ') + chalk.red('panel is down!'))
}
if (userCount === checking) db.set('panel', '**Panel**: ' + checking)
let panel = db.get('panel') + '\n\nUsers: ' + userCount + '\nServers: ' + serverCount
if (panel === null) panel = '**Panel**: ' + checking + '\n\nUsers: ' + userCount + '\nServers: ' + serverCount
let nodes
list.forEach((d) => {
if (!nodes) return nodes = d
nodes = nodes + d
})
let nodeCount = '[Total ' + list.length + ']'
if (debug === true) console.log(chalk.magenta('[PteroStats Debug] ') + chalk.blue(nodes))
if (nodes === undefined) {
nodes = checking + ' Please wait ' + time + ' seconds'
console.log(chalk.cyan(['[PteroStats Checker] ']) + chalk.yellow(checking + ' Please wait ' + time + ' seconds'))
}
if (debug === true) console.log(chalk.magenta('[PteroStats Debug] ') + chalk.blue(msgStats))
list.push(msgStats)
})
axios(api + '/application/servers', {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: 'Bearer ' + adminapikey
let embedfooter = 'Updated every ' + time + ' seconds'
if (enablef === true) embedfooter = 'Updated every ' + time + ' seconds | ' + footer
let embed = new MessageEmbed()
.setTitle(title)
.setColor(color)
.addField('Panel Stats', panel)
.setFooter(embedfooter)
.setThumbnail(client.user.avatarURL())
if (enablets === true) {
embed.setTimestamp()
}
}).then(response => {
let res = response.data.meta.pagination.total
db.set('serverCount', res)
}).catch(err => {
db.set('serverCount', 'N/A')
console.log(chalk.cyan('[PteroStats Checker] ') + chalk.red('Panel is down'))
if (debugerror === true) console.log(chalk.magenta('[PteroStats Debug] ') + err)
})
axios(api + '/application/users', {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: 'Bearer ' + adminapikey
if (enabledesc === true) {
embed.setDescription(desc + '\n**Nodes Stats' + nodeCount + '**\n' + nodes)
} else {
embed.setDescription('\n**Nodes Stats' + nodeCount + '**\n' + nodes)
}
}).then(response => {
let res = response.data.meta.pagination.total
db.set('userCount', res)
}).catch(err => {
db.set('userCount', 'N/A')
console.log(chalk.cyan('[PteroStats Checker] ') + chalk.red('Panel is down!'))
if (debugerror === true) console.log(chalk.magenta('[PteroStats Debug] ') + err)
ch.send(embed).then(msg => { msg.delete({ timeout: time + '000' }) })
console.log(chalk.cyan('[PteroStats Checker] ') + chalk.green('Posted Stats'))
if (panel !== null) console.log(chalk.cyan('[PteroStats Checker] ') + chalk.green('Stats Updated'))
console.log(chalk.cyan('[PteroStats Checker] ') + chalk.green('Updating Stats in ' + time + ' Seconds'))
}).catch((err) => {
console.log(chalk.magenta('[PteroStats Debug] ') + chalk.red(err) + chalk.cyan(' Need Support? https://discord.gg/zv6maQRah3'))
})
let userCount = db.get('userCount')
let serverCount = db.get('serverCount')
if (userCount === null) userCount = checking
if (serverCount === null) serverCount = checking
if (userCount !== 'N/A') db.set('panel', '**Panel**: ' + statusonline)
if (userCount === 'N/A') {
db.set('panel', '**Panel**: ' + statusoffline)
console.log(chalk.cyan('[PteroStats Checker] ') + chalk.red('panel is down!'))
}
if (userCount === checking) db.set('panel', '**Panel**: ' + checking)
let panel = db.get('panel') + '\n\nUsers: ' + userCount + '\nServers: ' + serverCount
if (panel === null) panel = '**Panel**: ' + checking + '\n\nUsers: ' + userCount + '\nServers: ' + serverCount
let nodes
list.forEach((d) => {
if (!nodes) return nodes = d
nodes = nodes + d
})
let nodeCount = '[Total ' + list.length + ']'
if (nodes === undefined) nodes = checking + 'Please wait ' + time + 'seconds'
let embedfooter = 'Updated every ' + time + ' seconds'
if (enablef === true) embedfooter = 'Updated every ' + time + ' seconds | ' + footer
let embed = new MessageEmbed()
.setTitle(title)
.setColor(color)
.addField('Panel Stats', panel)
.setFooter(embedfooter)
.setThumbnail(client.user.avatarURL())
if (enablets === true) {
embed.setTimestamp()
}
if (enabledesc === true) {
embed.setDescription(desc + '\n**Nodes Stats' + nodeCount + '**\n' + nodes)
} else {
embed.setDescription('\n**Nodes Stats' + nodeCount + '**\n' + nodes)
}
ch.send(embed).then(msg => { msg.delete({ timeout: time + '000' }) })
console.log(chalk.cyan('[PteroStats Checker] ') + chalk.green('Posted Stats'))
if (panel !== null) console.log(chalk.cyan('[PteroStats Checker] ') + chalk.green('Stats Updated'))
console.log(chalk.cyan('[PteroStats Checker] ') + chalk.green('Updating Stats in ' + time + ' Seconds'))
}, time + '000')
}