feat: combine setup.js and index.js into one file

This commit is contained in:
Hirzi
2024-09-17 06:15:39 +07:00
parent f740d9a3db
commit a754adb078
6 changed files with 39 additions and 65 deletions

2
.gitignore vendored
View File

@@ -1,7 +1,7 @@
node_modules/
config-dev.yml
package-lock.json
.vscode
node_modules
cache.json
.env
.setup-complete

View File

@@ -14,8 +14,6 @@ PteroStats is a Discord App/Bot designed to check Pterodactyl or Pelican Panel s
<img alt="PteroStats Console Preview" src="https://usercontent.catto.pictures/hirzi/8ce3aac6-5c46-4626-bd14-af994b602f8e.png" width="300"/>
<img alt="PteroStats GIF Preview" src="https://usercontent.catto.pictures/hirzi/ad6e36cc-b582-460b-ab4e-b5e1dacd8b02.gif" width="300"/>
## Guide
- [Starting the App/Bot](#starting-the-appbot)
- [Changing Env Configuration](https://github.com/HirziDevs/PteroStats/blob/main/guide/changing-env-configuration.md)
@@ -30,17 +28,17 @@ PteroStats is a Discord App/Bot designed to check Pterodactyl or Pelican Panel s
1. [Create your Discord App/Bot](https://discordjs.guide/preparations/adding-your-bot-to-servers.html).
2. [Invite your Discord App/Bot to your Discord server](https://discordjs.guide/preparations/adding-your-bot-to-servers.html).
3. Download this repository:
- [Download this repository](https://github.com/HirziDevs/PteroStats/archive/refs/heads/main.zip) and extract it.
- Manually: [Download this repository](https://github.com/HirziDevs/PteroStats/archive/refs/heads/main.zip) and extract it.
- Using Git: Run `git clone https://github.com/HirziDevs/PteroStats.git` in the command line.
4. Run `npm install` in the root directory of the app/bot files.
5. Run `node index` and answer the prompted questions to set up the app/bot.
5. Run `node index.js` and answer the prompted questions to set up the app/bot.
<img alt="Setup" src="https://usercontent.catto.pictures/hirzi/b8645828-591d-4d52-b6d8-51f8df60440c.png" width="300"/>
- [Getting an Panel API key](https://github.com/HirziDevs/PteroStats/blob/main/guide/getting-panel-api-key.md)
- [Getting a Channel ID](https://github.com/HirziDevs/PteroStats/blob/main/guide/getting-channel-id.md)
6. Run `node index` if you want to start the app/bot again, and you're done!
6. Run `node index.js` if you want to start the app/bot again, and you're done!
<img alt="Console Logging" src="https://usercontent.catto.pictures/hirzi/8ce3aac6-5c46-4626-bd14-af994b602f8e.png" width="300"/>

View File

@@ -2,10 +2,14 @@
> [!TIP]
> You can change other configuration at the `config.yml` file.
1. Run `node setup` in the root directory of the app/bot files.
2. Enter `2` to change configuration.
1. Delete `.setup-complete` file in the root directory of the app/bot files.
2. Run `node index.js` and answer the prompted questions to set up the app/bot.
<img alt="Change Configuration" src="https://usercontent.catto.pictures/hirzi/f61ebf43-3df8-4b86-93ac-166e2de1edcd.png" width="300"/>
<img alt="Setup" src="https://usercontent.catto.pictures/hirzi/b8645828-591d-4d52-b6d8-51f8df60440c.png" width="300"/>
3. Answer the provided question to set up the app/bot.
4. Run `node index` if you want to start the app/bot again, and you're done!
- [Getting an Panel API key](https://github.com/HirziDevs/PteroStats/blob/main/guide/getting-panel-api-key.md)
- [Getting a Channel ID](https://github.com/HirziDevs/PteroStats/blob/main/guide/getting-channel-id.md)
3. Run `node index.js` if you want to start the app/bot again, and you're done!
<img alt="Console Logging" src="https://usercontent.catto.pictures/hirzi/8ce3aac6-5c46-4626-bd14-af994b602f8e.png" width="300"/>

View File

@@ -1,7 +1,7 @@
const axios = require("axios")
const cliColor = require("cli-color")
const { Client, GatewayIntentBits } = require("discord.js")
const fs = require("fs")
const axios = require("axios");
const cliColor = require("cli-color");
const { Client, GatewayIntentBits } = require("discord.js");
const fs = require("node:fs");
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
@@ -82,8 +82,26 @@ module.exports = function Setup() {
console.log(cliColor.green("✓ Valid Discord Bot"));
client.channels.fetch(answers[Question.channelId]).then(() => {
console.log(cliColor.green("✓ Valid Discord Channel"));
fs.writeFileSync(".env", `PanelURL=${answers[Question.panelUrl]}\nPanelKEY=${answers[Question.panelApiKey]}\nDiscordBotToken=${answers[Question.botToken]}\nDiscordChannel=${answers[Question.channelId]}`, "utf8")
fs.writeFileSync("config.yml", fs.readFileSync("./config.yml", "utf8").replaceAll("Hosting Panel", answers[0]).replaceAll("https://panel.example.com", answers[1]), "utf-8")
fs.writeFileSync(".setup-complete", "If you want to re-run the setup process, you can delete this file.", "utf8");
fs.writeFileSync(
".env",
`PanelURL=${answers[Question.panelUrl]}\n` +
`PanelKEY=${answers[Question.panelApiKey]}\n` +
`DiscordBotToken=${answers[Question.botToken]}\n` +
`DiscordChannel=${answers[Question.channelId]}`,
"utf8"
);
fs.writeFileSync(
"config.yml",
fs.readFileSync("./config.yml", "utf8")
.replaceAll("Hosting Panel", answers[0])
.replaceAll("https://panel.example.com", answers[1]),
"utf-8"
);
console.log(" \n" + cliColor.green(`Configuration saved in ${cliColor.blueBright(".env")} and ${cliColor.blueBright("config.yml")}.\n `));
require("./application.js")()

View File

@@ -18,6 +18,6 @@ console.log(
` \n \n${package.description}\n `
);
if (!fs.existsSync(".env")) return require("./handlers/setup.js")();
if (!fs.existsSync(".env") || !fs.existsSync(".setup-complete")) return require("./handlers/setup.js")();
require("./handlers/application.js")();
require("./handlers/application.js")();

View File

@@ -1,46 +0,0 @@
const fs = require("node:fs");
const cliColor = require("cli-color");
const package = require("./package.json");
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
console.log(
` _${cliColor.blueBright.bold(`${cliColor.underline("Ptero")}dact${cliColor.underline("yl & P")}eli${cliColor.underline("can")}`)}___ ______ ______ \n` +
` /\\ ___\\ /\\__ _\\ /\\ __ \\ /\\__ _\\ /\\ ___\\ \n` +
` \\ \\___ \\ \\/_ \\ \\/ \\ \\ \\_\\ \\ \\/_/\\ \\/ \\ \\___ \\ \n` +
` \\/\\_____\\ \\ \\_\\ \\ \\_\\ \\_\\ \\ \\_\\ \\/\\_____\\ \n` +
` \\/_____/ \\/_/ \\/_/\\/_/ \\/_/ \\/_____/${cliColor.yellowBright.bold(`${package.version}`)}`
);
console.log(
` \nCopyright © 2022 - ${new Date().getFullYear()} HirziDevs & Contributors\n ` +
" \nDiscord: https://discord.znproject.my.id" +
" \n Source: https://github.com/HirziDevs/PteroStats" +
" \nLicense: https://github.com/Hirzidevs/PteroStats/blob/main/LICENSE" +
` \n \n${package.description}\n `
);
if (!fs.existsSync(".env")) return require("./handlers/setup.js")();
console.log(cliColor.yellowBright(
"Configuration is already set. Please select one of the following options:\n \n" +
`${cliColor.cyanBright("1")} ${cliColor.blueBright("»")} Start the App\n` +
`${cliColor.cyanBright("2")} ${cliColor.blueBright("»")} Change configuration\n `
));
readline.question('> ', async (answer) => {
readline.close();
switch (answer) {
case '2':
require("./handlers/setup.js")();
break;
case '1':
require("./handlers/application.js")();
break;
default:
console.log(cliColor.redBright('Invalid input. Please type either 1 or 2.'));
}
});