This commit is contained in:
Hirzi
2022-11-28 07:59:49 +00:00
parent 76bd98d692
commit 750118e287
5 changed files with 60 additions and 72 deletions

View File

@@ -8,22 +8,20 @@ module.exports = {
console.log(chalk.cyan('[PteroStats] ') + chalk.green('Bot is up!'))
console.log(chalk.cyan('[PteroStats] ') + chalk.green('If you need support you can join our discord server https://discord.gg/zv6maQRah3'))
if (client.guilds.cache.size < 1) return console.log(chalk.cyan('[PteroStats] ') + chalk.red('Err! This bot is not on any discord servers'))
if (client.guilds.cache.size < 1) return console.log(chalk.cyan('[PteroStats] ') + chalk.red('Error! This bot is not on any discord servers'))
if (client.config.refresh < 10) console.log(chalk.cyan('[PteroStats] ') + chalk.red('Refresh lower than 10 seconds is not recommended!'))
if (client.config.bot_status.enable && client.config.bot_status.text.length > 0) {
if (!['PLAYING', 'WATCHING', 'LISTENING', 'COMPETING'].includes(client.config.bot_status.type.toUpperCase() || client.config.bot_status.type.length < 1)) {
console.log(chalk.cyan('[PteroStats] ') + chalk.red('Err! Invalid Status Type!, Can be "WATCHING", "PLAYING", "LISTENING", or "COMPETING"'))
console.log(chalk.cyan('[PteroStats] ') + chalk.red('Error! Invalid Status Type!, Can be "WATCHING", "PLAYING", "LISTENING", or "COMPETING"'))
} else {
client.user.setActivity(client.config.bot_status.text, { type: client.config.bot_status.type.toUpperCase() })
}
}
if (client.config.refresh < 10) console.log('Refresh lower than 10 seconds is not recommended!')
checkStatus(client)
checkStatus({ client: client })
setInterval(() => {
checkStatus(client)
checkStatus({ client: client })
}, client.config.refresh * 1000)
}
}

View File

@@ -3,19 +3,19 @@ const chalk = require('chalk')
const postStatus = require('./postStatus')
module.exports = function checkStatus(client) {
module.exports = function checkStatus({ client }) {
if (client.config.channel.startsWith('Put')) {
console.log(chalk.cyan('[PteroStats] ') + chalk.red('Err! Invalid Channel ID'))
console.log(chalk.cyan('[PteroStats] ') + chalk.red('Error! Invalid Channel ID'))
process.exit()
} else if (client.config.panel.url.startsWith('Put')) {
console.log(chalk.cyan('[PteroStats] ') + chalk.red('Err! Invalid Panel URL'))
console.log(chalk.cyan('[PteroStats] ') + chalk.red('Error! Invalid Panel URL'))
process.exit()
} else if (client.config.panel.key.startsWith('Put')) {
console.log(chalk.cyan('[PteroStats] ') + chalk.red('Err! Invalid Apikey'))
console.log(chalk.cyan('[PteroStats] ') + chalk.red('Error! Invalid Apikey'))
process.exit()
} else if (!client.config.panel.url.startsWith('http')) {
console.log(chalk.cyan('[PteroStats] ') + chalk.red('Err! Invalid Panel URL'))
console.log(chalk.cyan('[PteroStats] ') + chalk.red('Error! Invalid Panel URL'))
console.log(chalk.cyan('[PteroStats] ') + chalk.red('1. Make sure the panel url is starts with "https://" or "http://"'))
process.exit()
}
@@ -40,7 +40,7 @@ module.exports = function checkStatus(client) {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + client.config.panel.key
}
}).then((usr) => {
}).then((users) => {
axios(client.config.panel.url + '/api/application/servers', {
method: 'GET',
headers: {
@@ -48,25 +48,25 @@ module.exports = function checkStatus(client) {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + client.config.panel.key
}
}).then((ser) => {
panel.total_users = usr.data.meta.pagination.total
panel.total_servers = ser.data.meta.pagination.total
}).then((servers) => {
panel.total_users = users.data.meta.pagination.total
panel.total_servers = servers.data.meta.pagination.total
panel.status = true
resolve()
})
}).catch((err) => {
if (err.response) {
if (err.response.status === 403) {
console.log(chalk.cyan('[PteroStats] ') + chalk.red('Err! Invalid apikey'))
}).catch((error) => {
if (error.response) {
if (error.response.status === 403) {
console.log(chalk.cyan('[PteroStats] ') + chalk.red('Error! Invalid apikey'))
console.log(chalk.cyan('[PteroStats] ') + chalk.red('1. Make sure the apikey is from admin page not account page'))
console.log(chalk.cyan('[PteroStats] ') + chalk.red('2. Make sure the apikey has read permission on all options'))
console.log(chalk.cyan('[PteroStats] ') + chalk.red('3. Make sure the apikey is exist'))
} else if (err.response.status === 404) {
console.log(chalk.cyan('[PteroStats] ') + chalk.red('Err! Invalid Panel URL'))
} else if (error.response.status === 404) {
console.log(chalk.cyan('[PteroStats] ') + chalk.red('Error! Invalid Panel URL'))
console.log(chalk.cyan('[PteroStats] ') + chalk.red('1. Make sure the panel url is like "https://panel.example.com"'))
} else console.log(chalk.cyan('[PteroStats] ') + chalk.red('Err! ' + err))
} else console.log(chalk.cyan('[PteroStats] ') + chalk.red('Err! ' + err))
} else console.log(chalk.cyan('[PteroStats] ') + chalk.red('Error! ' + error))
} else console.log(chalk.cyan('[PteroStats] ') + chalk.red('Error! ' + error))
resolve()
})
})
@@ -111,9 +111,9 @@ module.exports = function checkStatus(client) {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + data.data.token
}
}).then((status) => {
}).then(() => {
return statsResolve()
}).catch((err) => {
}).catch(() => {
body.status = false
return statsResolve()
})
@@ -126,19 +126,15 @@ module.exports = function checkStatus(client) {
nodes.push(body)
if (nodes.length === res.data.data.length) resolve()
})
}).catch((err) => {
resolve()
})
}).catch(() => resolve())
})
}).catch((err) => {
resolve()
})
}).catch(() => resolve())
})
panelStats.then(() => {
nodeStats.then(() => {
nodes.sort(function (a, b) { return a.id - b.id })
postStatus(client, panel, nodes)
postStatus({ client: client, panel: panel, nodes: nodes })
})
})
}

View File

@@ -1,38 +1,29 @@
const { EmbedBuilder, time, ActionRowBuilder, ButtonBuilder, ButtonStyle, AttachmentBuilder } = require('discord.js')
const chalk = require('chalk')
module.exports = async function postStatus(client, panel, nodes) {
if (!client.config.nodes_resource) client.config.nodes_resource = client.config.resource
if (!client.config.nodes_resource.blacklist) client.config.nodes_resource.blacklist = []
if (!Array.isArray(client.config.nodes_resource.blacklist) && Number.isInteger(client.config.nodes_resource.blacklist)) client.config.nodes_resource.blacklist = [client.config.nodes_resource.blacklist]
if (!client.config.message.attachment) client.config.message.attachment = client.config.message.image
if (client.guilds.cache.size < 1) return console.log(chalk.cyan('[PteroStats] ') + chalk.red('Err! This bot is not on any discord servers'))
module.exports = async function postStatus({ client, panel, nodes }) {
const channel = await client.channels.cache.get(client.config.channel)
if (!channel) return console.log(chalk.cyan('[PteroStats] ') + chalk.red('Error! Invalid Channel ID'))
if (!channel) return console.log(chalk.cyan('[PteroStats] ') + chalk.red('Err! Invalid Channel ID'))
const files = []
const embed = new EmbedBuilder()
let messages = await channel.messages.fetch({ limit: 10 }).then(msg => msg.filter(m => m.author.id === client.user.id).last())
let text = ''
let desc = ''
let blacklist = 0
let content = null
let messages = await channel.messages.fetch({ limit: 10 })
messages = messages.filter(m => m.author.id === client.user.id).last();
if (!client.config.nodes_resource.blacklist) client.config.nodes_resource.blacklist = []
if (!Array.isArray(client.config.nodes_resource.blacklist) && Number.isInteger(client.config.nodes_resource.blacklist)) client.config.nodes_resource.blacklist = [client.config.nodes_resource.blacklist]
if (client.guilds.cache.size < 1) return console.log(chalk.cyan('[PteroStats] ') + chalk.red('Error! This bot is not on any discord servers'))
if (messages && messages.embeds.length < 1) {
messages.delete()
messages = null
}
const embed = new EmbedBuilder()
let text = ''
let desc = ''
let blacklist = 0
let content = null
const files = []
if (client.config.message.content) content = client.config.message.content
if (client.config.message.attachment) files.push(new AttachmentBuilder(client.config.message.attachment))
if (client.config.embed.title) embed.setTitle(client.config.embed.title)
if (client.config.embed.description) desc = client.config.embed.description + '\n'
if (client.config.embed.color) embed.setColor(client.config.embed.color)

View File

@@ -1,5 +1,7 @@
const fs = require('fs');
const child = require('child_process');
const chalk = require('chalk');
const yaml = require('js-yaml');
if (Number(process.version.split('.')[0]) < 16) {
console.log('Invalid NodeJS Version!, Please use NodeJS 16.x or upper')
@@ -19,34 +21,35 @@ if (fs.existsSync('./node_modules')) {
console.log('Install complete!, please run "node index" command again!')
process.exit()
} catch (err) {
console.log('Err! ', err)
console.log('Error! ', err)
console.log('Support Server: https://discord.gg/zv6maQRah3')
process.exit()
}
}
const chalk = require('chalk');
const yaml = require('js-yaml');
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const config = yaml.load(fs.readFileSync('./config.yml', 'utf8'));
client.config = config
client.config = yaml.load(fs.readFileSync('./config.yml', 'utf8'));
if (client.config.panel.adminkey || client.config.resource || client.config.message.image) {
console.log(chalk.cyan('[PteroStats] ') + chalk.red('You are using old config file, please update your config file at https://github.com/HirziDevs/PteroStats/blob/main/config.yml !'))
process.exit()
}
if (client.config.token.startsWith('Put') || !client.config.token.length) {
console.log(chalk.cyan('[PteroStats] ') + chalk.red('Error! Invalid Discord Bot Token'))
process.exit()
}
const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
const event = require(`./events/${file}`);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
const event = require(`./events/${file}`);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
if (client.config.token.startsWith('Put') || client.config.token.length < 1) {
console.log(chalk.cyan('[PteroStats]') + chalk.red(' Err! Invalid Discord Bot Token'))
process.exit()
}
client.login(config.token);
client.login(client.config.token);

View File

@@ -7,7 +7,7 @@
"homepage": "https://pterostats.hirzidevs.xyz",
"bugs": {
"email": "hirzidevs@gmail.com",
"url": "https://github.com/HirziDevs/PteroStats"
"url": "https://github.com/HirziDevs/PteroStats/issues"
},
"scripts": {
"start": "node index.js"