mirror of
https://github.com/HirziDevs/PteroStats.git
synced 2026-03-31 06:24:09 -04:00
feat: webhook notifier
This commit is contained in:
35
README.md
35
README.md
@@ -23,6 +23,7 @@ PteroStats is a Discord App/Bot that designed to check Pterodactyl or Pelican Pa
|
||||
- [Getting a Channel ID](#getting-channel-id)
|
||||
- [Using Custom Emoji](#using-custom-emoji)
|
||||
- [Blacklist Nodes](#blacklist-nodes)
|
||||
- [Notifier](#notifier)
|
||||
|
||||
### Starting the App/Bot
|
||||
1. [Create your Discord App/Bot](https://discordjs.guide/preparations/adding-your-bot-to-servers.html).
|
||||
@@ -124,6 +125,40 @@ You can add more than one node to the blacklist.
|
||||
|
||||
<img alt="Blacklist Config" src="https://usercontent.catto.pictures/hirzi/7b5d6c7f-54d9-40ea-b5a6-9192325ba2a0.png" width="400"/>
|
||||
|
||||
### Notifier
|
||||
Get a notification on Discord when your panel or specific nodes are currently down.
|
||||
|
||||
<img alt="Notifier Preview" src="https://usercontent.catto.pictures/hirzi/a2b8e36f-7448-4849-a14a-b1eb4ec8fb26.png" width="250"/>
|
||||
|
||||
### Enabling Notifier
|
||||
Open `config.yml` and set `enable` at the notifier configuration to `true`
|
||||
|
||||
<img alt="Notifier Config" src="https://usercontent.catto.pictures/hirzi/b4c3f1d0-e053-402c-8401-4de44926fce6.png" width="300"/>
|
||||
|
||||
#### Getting Discord Webhook URL
|
||||
1. Go to the channel settings of the channel you want to set for the notifier.
|
||||
|
||||
<img alt="Notifier Config" src="https://usercontent.catto.pictures/hirzi/7d7712b9-d9ac-4650-83ac-21dc3f20c3fe.png" width="300"/>
|
||||
|
||||
2. Go to integrations and select `View Webhooks` or `Create Webhook`.
|
||||
|
||||
<img alt="Notifier Config" src="https://usercontent.catto.pictures/hirzi/e251f1e9-6b46-4051-be64-1945a6eaee33.png" width="300"/>
|
||||
|
||||
3. Create a new webhook and copy the Webhook URL
|
||||
|
||||
<img alt="Notifier Config" src="https://usercontent.catto.pictures/hirzi/e0af8410-527a-42e2-b284-48d7eb81456f.png" width="300"/>
|
||||
|
||||
4. Paste the Webhook URL on the webhook notifier configuration.
|
||||
|
||||
<img alt="Notifier Config" src="https://usercontent.catto.pictures/hirzi/b4ec26ad-e426-434e-b8c8-27ddc2916f5f.png" width="300"/>
|
||||
|
||||
|
||||
> [!TIP]
|
||||
> You can change the webhook icon and username on the webhook settings.
|
||||
|
||||
<img alt="Notifier Config" src="https://usercontent.catto.pictures/hirzi/2a4f7aba-9377-4722-bf19-3b7f0cc32772.png" width="300"/>
|
||||
|
||||
|
||||
## Reporting a Bug
|
||||
Enable `log_error` in the `config.yml` file and check the console for the error message. After that, report it to our Discord server at [Support Server](https://discord.znproject.my.id).
|
||||
|
||||
|
||||
15
config.yml
15
config.yml
@@ -88,6 +88,21 @@ panel_settings:
|
||||
servers: true # Display servers count (true/false).
|
||||
users: true # Display users count (true/false).
|
||||
|
||||
# Notifier Configuration
|
||||
notifier:
|
||||
enable: false # Enable or disable notifier.
|
||||
webhook: "" # Discord Webhook URL for the notifier.
|
||||
embed:
|
||||
author:
|
||||
name: "" # Author name for the notifier embed.
|
||||
icon: "" # Icon URL for the author of the notifier embed.
|
||||
timestamp: true # Include a timestamp in the notifier embed (true/false).
|
||||
footer:
|
||||
text: "PteroStats Notifier" # Footer text for notifier stats.
|
||||
icon: "" # Footer icon URL.
|
||||
thumbnail: "" # Thumbnail URL for the notifier stats embed.
|
||||
image: "" # Image URL for the notifier stats embed.
|
||||
|
||||
# Error Logging Configuration
|
||||
# Enable logging to console if servers go offline, useful for debugging.
|
||||
log_error: false # Set to true to enable error logging.
|
||||
@@ -5,6 +5,7 @@ const cliColor = require("cli-color");
|
||||
const config = require("./config.js");
|
||||
const convertUnits = require("./convertUnits.js");
|
||||
const getStats = require("./getStats.js");
|
||||
const webhook = require("./webhook.js");
|
||||
|
||||
module.exports = function App() {
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.green("Starting app..."));
|
||||
@@ -16,6 +17,12 @@ module.exports = function App() {
|
||||
async function startGetStatus() {
|
||||
try {
|
||||
const results = await getStats();
|
||||
if (results.isPanelDown) webhook(
|
||||
new EmbedBuilder()
|
||||
.setTitle("Panel Online")
|
||||
.setColor("57F287")
|
||||
.setDescription(`Panel is back online`)
|
||||
)
|
||||
createMessage({
|
||||
panel: true,
|
||||
uptime: results.uptime,
|
||||
@@ -35,6 +42,12 @@ module.exports = function App() {
|
||||
|
||||
try {
|
||||
const results = JSON.parse(data);
|
||||
if (results.uptime) webhook(
|
||||
new EmbedBuilder()
|
||||
.setTitle("Panel Offline")
|
||||
.setColor("ED4245")
|
||||
.setDescription(`Panel is currently offline`)
|
||||
)
|
||||
results.uptime = false
|
||||
fs.writeFileSync("cache.json", JSON.stringify(results, null, 2), "utf8");
|
||||
createMessage({
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
const config = require("./config.js");
|
||||
const fs = require("node:fs");
|
||||
const cliColor = require("cli-color");
|
||||
const webhook = require("./webhook.js");
|
||||
const { EmbedBuilder } = require("discord.js");
|
||||
|
||||
module.exports = async function getStats() {
|
||||
let cache = (() => {
|
||||
@@ -29,7 +31,22 @@ module.exports = async function getStats() {
|
||||
|
||||
if (!nodeStatus) {
|
||||
nodeUptime = false
|
||||
if (cache && cache.nodes.find((n) => n.attributes.id === node.attributes.id)?.status)
|
||||
webhook(
|
||||
new EmbedBuilder()
|
||||
.setTitle("Node Offline")
|
||||
.setColor("ED4245")
|
||||
.setDescription(`Node \`${node.attributes.name}\` is currently offline`)
|
||||
)
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.redBright(`Node ${cliColor.blueBright(node.attributes.name)} is currently offline.`))
|
||||
} else {
|
||||
if (cache && !cache.nodes.find((n) => n.attributes.id === node.attributes.id)?.status)
|
||||
webhook(
|
||||
new EmbedBuilder()
|
||||
.setTitle("Node Online")
|
||||
.setColor("57F287")
|
||||
.setDescription(`Node \`${node.attributes.name}\` is back online`)
|
||||
)
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -58,6 +75,7 @@ module.exports = async function getStats() {
|
||||
servers: await require("./getServers.js")(),
|
||||
users: await require("./getUsers.js")(),
|
||||
nodes: await Promise.all(statusPromises),
|
||||
isPanelDown: !cache.uptime,
|
||||
timestamp: Date.now()
|
||||
}
|
||||
|
||||
|
||||
33
handlers/webhook.js
Normal file
33
handlers/webhook.js
Normal file
@@ -0,0 +1,33 @@
|
||||
const { WebhookClient, EmbedBuilder } = require("discord.js")
|
||||
const config = require("./config")
|
||||
const cliColor = require("cli-color")
|
||||
|
||||
module.exports = function Webhook(embed) {
|
||||
if (config.notifier.enable) {
|
||||
try {
|
||||
const webhook = new WebhookClient({
|
||||
url: config.notifier.webhook
|
||||
})
|
||||
webhook.send({
|
||||
embeds: [
|
||||
new EmbedBuilder(embed.data)
|
||||
.setAuthor({
|
||||
name: config.notifier.embed.author.name || null,
|
||||
iconURL: config.notifier.embed.author.icon || null
|
||||
})
|
||||
.setFooter({
|
||||
text: config.notifier.embed.footer.text || null,
|
||||
iconURL: config.notifier.embed.footer.icon || null
|
||||
})
|
||||
.setURL(config.notifier.embed.url || null)
|
||||
.setTimestamp(config.notifier.embed.timestamp ? new Date() : null)
|
||||
.setThumbnail(config.notifier.embed.thumbnail || null)
|
||||
.setImage(config.notifier.embed.image || null)
|
||||
]
|
||||
})
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.redBright("Invalid Webhook URL"))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user