diff --git a/handlers/checkStatus.js b/handlers/checkStatus.js index f0bd5b4..d8763bb 100644 --- a/handlers/checkStatus.js +++ b/handlers/checkStatus.js @@ -5,6 +5,23 @@ const postStatus = require('./postStatus') module.exports = async function checkStatus(client) { + if (client.config.channel.startsWith('Put')) { + console.log(chalk.cyan('[PteroStats]') + chalk.red(' Err! Invalid Channel ID')) + process.exit() + } else if (client.config.panel.url.startsWith('Put')) { + console.log(chalk.cyan('[PteroStats]') + chalk.red(' Err! Invalid Panel URL')) + process.exit() + } else if (client.config.panel.key.startsWith('Put')) { + console.log(chalk.cyan('[PteroStats]') + chalk.red(' Err! 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(' 1. Make sure the panel url is starts with "https://" or "http://"')) + process.exit() + } + + if (client.config.panel.url.endsWith('/')) client.config.panel.url = client.config.panel.url.slice(0, -1) + const nodes = [] const panel = { @@ -47,7 +64,7 @@ module.exports = async function checkStatus(client) { 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 URL Panel')) + console.log(chalk.cyan('[PteroStats]') + chalk.red(' Err! 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] ') + err) } else console.log(chalk.cyan('[PteroStats] ') + err) diff --git a/handlers/postStatus.js b/handlers/postStatus.js index 38a977f..5e46fa3 100644 --- a/handlers/postStatus.js +++ b/handlers/postStatus.js @@ -36,9 +36,7 @@ module.exports = async function postStatus(client, panel, nodes) { if (nodes.length !== 0) { nodes.forEach((data, i) => { const title = data.name + ': ' + String(data.status).replace('true', client.config.status.online).replace('false', client.config.status.offline) - let description = '```' + '\n' + - 'Status : ' + String(data.status).replace('true', client.config.status.online).replace('false', client.config.status.offline) + '\n' - + let description = '```' switch (client.config.resource.unit) { case 'gb': description = description + @@ -57,7 +55,7 @@ module.exports = async function postStatus(client, panel, nodes) { } if (client.config.resource.location) description = description + 'Location: ' + data.location + '\n' - if (client.config.resource.allocations) description = description + 'Servers : ' + data.allocations.toLocaleString() + '\n' + if (client.config.resource.allocations) description = description + 'Allocations : ' + data.allocations.toLocaleString() + '\n' if (client.config.resource.servers) description = description + 'Servers : ' + data.total_servers.toLocaleString() + '\n' description = description + '```' @@ -76,9 +74,9 @@ module.exports = async function postStatus(client, panel, nodes) { resolve() } else { text = messages.embeds[0].description.replaceAll(client.config.status.online, client.config.status.offline) - if (!panel.status && String(String(messages.embeds[0].fields[1].value).split('\n')[2]).split('')[String(String(messages.embeds[0].fields[1].value).split('\n')[2]).length - 1] !== '`') { - panel.total_users = String(String(messages.embeds[0].fields[1].value).split('\n')[2]).split('')[String(String(messages.embeds[0].fields[1].value).split('\n')[2]).length - 1] - panel.total_servers = String(String(messages.embeds[0].fields[1].value).split('\n')[3]).split('')[String(String(messages.embeds[0].fields[1].value).split('\n')[3]).length - 1] + if (!panel.status && String(String(messages.embeds[0].fields[0].value).split('\n')[2]).split('')[String(String(messages.embeds[0].fields[0].value).split('\n')[2]).length - 1] !== '`') { + panel.total_users = String(String(messages.embeds[0].fields[0].value).split('\n')[2]).split('')[String(String(messages.embeds[0].fields[0].value).split('\n')[2]).length - 1] + panel.total_servers = String(String(messages.embeds[0].fields[0].value).split('\n')[3]).split('')[String(String(messages.embeds[0].fields[0].value).split('\n')[3]).length - 1] } resolve() } @@ -107,9 +105,9 @@ module.exports = async function postStatus(client, panel, nodes) { let row = [] if (client.config.button.enable) { - row = new MessageActionRow + const button = new MessageActionRow if (client.config.button.btn1.label.length !== 0 && client.config.button.btn1.url.length !== 0) { - row.addComponents( + button.addComponents( new MessageButton() .setLabel(client.config.button.btn1.label) .setStyle('LINK') @@ -117,7 +115,7 @@ module.exports = async function postStatus(client, panel, nodes) { ) } if (client.config.button.btn2.label.length !== 0 && client.config.button.btn2.url.length !== 0) { - row.addComponents( + button.addComponents( new MessageButton() .setLabel(client.config.button.btn2.label) .setStyle('LINK') @@ -125,7 +123,7 @@ module.exports = async function postStatus(client, panel, nodes) { ) } if (client.config.button.btn3.label.length !== 0 && client.config.button.btn3.url.length !== 0) { - row.addComponents( + button.addComponents( new MessageButton() .setLabel(client.config.button.btn3.label) .setStyle('LINK') @@ -133,7 +131,7 @@ module.exports = async function postStatus(client, panel, nodes) { ) } if (client.config.button.btn4.label.length !== 0 && client.config.button.btn4.url.length !== 0) { - row.addComponents( + button.addComponents( new MessageButton() .setLabel(client.config.button.btn4.label) .setStyle('LINK') @@ -141,7 +139,7 @@ module.exports = async function postStatus(client, panel, nodes) { ) } if (client.config.button.btn5.label.length !== 0 && client.config.button.btn5.url.length !== 0) { - row.addComponents( + button.addComponents( new MessageButton() .setLabel(client.config.button.btn5.label) .setStyle('LINK') @@ -149,10 +147,10 @@ module.exports = async function postStatus(client, panel, nodes) { ) } - row = [row] + row.push(button) } - if (!messages) channel.send({ embeds: [embed] }) + if (!messages) channel.send({ embeds: [embed], components: row }) else messages.edit({ embeds: [embed], components: row }) console.log(chalk.cyan('[PteroStats]') + chalk.green(' Stats posted!')) }) diff --git a/index.js b/index.js index 599a3dc..499866b 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ const fs = require('fs'); -const child = require("child_process") +const child = require('child_process'); if (Number(process.version.split('.')[0]) < 16) { console.log('Invalid NodeJS Version!, Please use NodeJS 16.x or upper') @@ -12,16 +12,20 @@ if (fs.existsSync('./node_modules')) { process.exit() } } else { - console.log('You are not installing package first, please wait until Auto Installer complete.') - child.execSync('npm i').catch((err) => { - console.log('An error detected: ', err) - console.log('Need Support? https://discord.gg/zv6maQRah3') - process.exit() - }) - console.log("Auto Installer complete! Please re run this bot!") - process.exit() + console.log('You didn\'t install the required node packages first!') + console.log('Please wait... starting to install all required node packages using child process') + try { + child.execSync('npm i') + console.log('Install complete!, please run "node index" command again!') + process.exit() + } catch (err) { + console.log('Err! ', err) + console.log('Support Server: https://discord.gg/zv6maQRah3') + process.exit() + } } +const chalk = require('chalk'); const yaml = require('js-yaml'); const { Client, Intents } = require('discord.js'); const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] }); @@ -43,9 +47,9 @@ for (const file of eventFiles) { } // Login to bot -try { - client.login(config.token); -} catch (Err) { - console.log('Invalid discord bot token') +if (client.config.token.startsWith('Put')) { + console.log(chalk.cyan('[PteroStats]') + chalk.red(' Err! Invalid Discord Bot Token')) process.exit() } + +client.login(config.token); \ No newline at end of file