mirror of
https://github.com/HirziDevs/PteroStats
synced 2026-01-04 12:14:20 +00:00
refactror
This commit is contained in:
@@ -1,71 +1,16 @@
|
||||
require("dotenv").config();
|
||||
const { Client, GatewayIntentBits, EmbedBuilder, time, ActivityType, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require("discord.js");
|
||||
const fs = require("node:fs");
|
||||
const cliColor = require("cli-color");
|
||||
const path = require("node:path");
|
||||
const { Client, GatewayIntentBits, ActivityType } = require("discord.js");
|
||||
const config = require("./configuration.js");
|
||||
const convertUnits = require("./convertUnits.js");
|
||||
const getStats = require("./getStats.js");
|
||||
const webhook = require("./webhook.js");
|
||||
const uptimeFormatter = require("./uptimeFormatter.js");
|
||||
const cliColor = require("cli-color");
|
||||
|
||||
module.exports = function App() {
|
||||
module.exports = function Application() {
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.green("Starting app..."));
|
||||
|
||||
const client = new Client({
|
||||
intents: [GatewayIntentBits.Guilds]
|
||||
});
|
||||
|
||||
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,
|
||||
nodes: results.nodes,
|
||||
servers: results.servers,
|
||||
users: results.users,
|
||||
});
|
||||
} catch (error) {
|
||||
if (config.log_error) console.error(error)
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.redBright("Panel is currently offline."));
|
||||
|
||||
fs.readFile(path.join(__dirname, "../cache.json"), (err, data) => {
|
||||
if (err) {
|
||||
createMessage({ cache: false, panel: false });
|
||||
return console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.redBright("Last cache was not found!"));
|
||||
}
|
||||
|
||||
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({
|
||||
cache: true,
|
||||
panel: false,
|
||||
nodes: results.nodes,
|
||||
servers: results.servers,
|
||||
users: results.users,
|
||||
});
|
||||
} catch {
|
||||
createMessage({ cache: false, panel: false });
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.redBright("Something went wrong with cache data..."));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
client.getStats = require("./getStats.js");
|
||||
|
||||
client.once("ready", () => {
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.green(`${cliColor.blueBright(client.user.tag)} is online!`));
|
||||
@@ -101,161 +46,9 @@ module.exports = function App() {
|
||||
}
|
||||
}
|
||||
|
||||
startGetStatus();
|
||||
client.getStats(client);
|
||||
});
|
||||
|
||||
async function createMessage({ cache, panel, uptime, nodes, servers, users }) {
|
||||
let embed = new EmbedBuilder()
|
||||
.setAuthor({
|
||||
name: config.embed.nodes.author.name || null,
|
||||
iconURL: config.embed.nodes.author.icon || null
|
||||
})
|
||||
.setDescription(config.embed.nodes.description || null)
|
||||
.setTitle(config.embed.nodes.title || null)
|
||||
.setColor(config.embed.nodes.color || null)
|
||||
.setURL(config.embed.nodes.url || null)
|
||||
.setThumbnail(config.embed.nodes.thumbnail || null)
|
||||
|
||||
let embeds = [embed];
|
||||
|
||||
if (config.nodes_settings.details) {
|
||||
nodes.forEach((node, index) => {
|
||||
if (index % 25 === 0 && index !== 0) {
|
||||
embed = new EmbedBuilder().setColor(config.embed.nodes.color);
|
||||
if (embeds.length < 9) embeds.push(embed);
|
||||
}
|
||||
|
||||
embed.addFields({
|
||||
name: `${node.attributes.name} - ${node.status ? config.status.online : config.status.offline}`,
|
||||
value:
|
||||
"```\n" +
|
||||
(config.nodes_settings.host ? `Host : ${node.attributes.fqdn}\n` : "") +
|
||||
`Memory : ${convertUnits(node.attributes.allocated_resources.memory, node.attributes.memory, config.nodes_settings.unit)}\n` +
|
||||
`Disk : ${convertUnits(node.attributes.allocated_resources.disk, node.attributes.disk, config.nodes_settings.unit)}` +
|
||||
(node.attributes?.allocated_resources?.cpu ? `\nCPU : ${node.attributes?.allocated_resources?.cpu || 0}%` : "") +
|
||||
(config.nodes_settings.servers ? `\nServers: ${node.attributes.relationships.servers}${config.nodes_settings.allocations_as_max_servers ? ` / ${node.attributes.relationships.allocations}` : ""}` : "") +
|
||||
(config.nodes_settings.uptime ? `\nUptime : ${node.uptime ? uptimeFormatter(Date.now() - node.uptime) : "N/A"}` : "") +
|
||||
"```"
|
||||
});
|
||||
});
|
||||
} else {
|
||||
embeds[0].setDescription((embed.data.description ? (embed.data.description + "\n\n") : "") + nodes.map(node => `**${node.attributes.name}** - ${node.status ? config.status.online : config.status.offline}`).join("\n"));
|
||||
}
|
||||
|
||||
let panelEmbed = new EmbedBuilder()
|
||||
.setAuthor({
|
||||
name: config.embed.panel.author.name || null,
|
||||
iconURL: config.embed.panel.author.icon || null
|
||||
})
|
||||
.setColor(config.embed.panel.color || null)
|
||||
.setTitle(config.embed.panel.title || null)
|
||||
.setURL(config.embed.panel.url || null)
|
||||
.setTimestamp(config.embed.panel.timestamp ? new Date() : null)
|
||||
.setFooter({
|
||||
text: config.embed.panel.footer.text || null,
|
||||
iconURL: config.embed.panel.footer.icon || null
|
||||
})
|
||||
.setThumbnail(config.embed.panel.thumbnail || null)
|
||||
.setImage(config.embed.panel.image || null)
|
||||
.setDescription(
|
||||
config.embed.panel.description
|
||||
.replace(
|
||||
"{{time}}",
|
||||
time(new Date(Date.now() + (config.refresh * 1000) + 1000), "R")
|
||||
) || null
|
||||
)
|
||||
.addFields({
|
||||
name: `Panel - ${panel ? config.status.online : config.status.offline}`,
|
||||
value:
|
||||
"```\n" +
|
||||
(config.panel_settings.host ? `Host : ${new URL(process.env.PanelURL).host}\n` : "") +
|
||||
`Nodes : ${nodes.length}\n` +
|
||||
(config.panel_settings.servers ? `Servers: ${servers || "Unknown"}\n` : "") +
|
||||
(config.panel_settings.users ? `Users : ${users || "Unknown"}\n` : "") +
|
||||
(config.panel_settings.uptime ? `Uptime : ${uptime ? uptimeFormatter(Date.now() - uptime) : "N/A"}\n` : "") +
|
||||
"```"
|
||||
});
|
||||
|
||||
if (config.panel_settings.status) embeds.unshift(panelEmbed);
|
||||
|
||||
embeds[embeds.length - 1]
|
||||
.setTimestamp(config.embed.nodes.timestamp ? new Date() : null)
|
||||
.setFooter({
|
||||
text: config.embed.nodes.footer.text || null,
|
||||
iconURL: config.embed.nodes.footer.icon || null
|
||||
})
|
||||
.setImage(config.embed.nodes.image || null)
|
||||
|
||||
if (!cache && !panel) {
|
||||
embeds[embeds.length - 1].setDescription(
|
||||
embeds[embeds.length - 1].data.description ? embeds[embeds.length - 1].data.description + "\n\nThere are no nodes to be display!" : "There are no nodes to be display!"
|
||||
);
|
||||
}
|
||||
|
||||
const components = []
|
||||
|
||||
if (config.button.enable) {
|
||||
for (const row of ["row1", "row2", "row3", "row4", "row5"]) {
|
||||
const buttons = config.button[row]?.slice(0, 5).filter(button => button.label && button.url);
|
||||
|
||||
if (buttons && buttons.length > 0) {
|
||||
components.push(
|
||||
new ActionRowBuilder().addComponents(
|
||||
buttons.map(button =>
|
||||
new ButtonBuilder()
|
||||
.setLabel(button.label)
|
||||
.setURL(button.url)
|
||||
.setStyle(ButtonStyle.Link)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const channel = await client.channels.fetch(process.env?.DiscordChannel);
|
||||
const messages = await channel.messages.fetch({ limit: 10 });
|
||||
const botMessage = messages.find(msg => msg.author.id === client.user.id);
|
||||
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.green(`Panel stats successfully posted to the ${cliColor.blueBright(channel.name)} channel!`));
|
||||
|
||||
setTimeout(() => startGetStatus(), config.refresh * 1000);
|
||||
|
||||
if (botMessage) {
|
||||
await botMessage.edit({ content: config.message.content || null, embeds, components });
|
||||
} else {
|
||||
await channel.send({ content: config.message.content || null, embeds, components });
|
||||
}
|
||||
} catch (error) {
|
||||
handleDiscordError(error);
|
||||
}
|
||||
}
|
||||
|
||||
function handleDiscordError(error) {
|
||||
try {
|
||||
if (error.rawError?.code === 429) {
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.redBright("Error 429 | Your IP has been rate limited by either Discord or your website. If it's a rate limit with Discord, you must wait. If it's a issue with your website, consider whitelisting your server IP."));
|
||||
} else if (error.rawError?.code === 403) {
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.redBright("FORBIDDEN | The channel ID you provided is incorrect. Please double check you have the right ID. If you're not sure, read our documentation: \n>>https://github.com/HirziDevs/PteroStats#getting-channel-id<<"));
|
||||
} else if (error.code === "ENOTFOUND") {
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.redBright("ENOTFOUND | DNS Error. Ensure your network connection and DNS server are functioning correctly."));
|
||||
} else if (error.rawError?.code === 50001) {
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.redBright("Discord Error | Your discord bot doesn't have access to see/send message/edit message in the channel!"));
|
||||
} else if (error.rawError?.errors && Object?.values(error.rawError.errors)[0]?._errors[0]?.code === "MAX_EMBED_SIZE_EXCEEDED") {
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.redBright("Discord Error | Embed message limit exceeded! Please limit or decrease the nodes that need to be shown in the config!"));
|
||||
} else if (error.rawError?.errors && Object?.values(error.rawError.errors)[0]?._errors[0]?.code) {
|
||||
console.log(Object.values(error.rawError.errors)[0]._errors[0].message);
|
||||
} else {
|
||||
console.error(cliColor.cyanBright("[PteroStats] ") + cliColor.redBright("Discord Error"), error);
|
||||
}
|
||||
process.exit();
|
||||
} catch (err) {
|
||||
console.error(error)
|
||||
process.exit();
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
client.login(process.env?.DiscordBotToken);
|
||||
} catch {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
const fs = require("node:fs");
|
||||
const yaml = require("js-yaml");
|
||||
const cliColor = require("cli-color");
|
||||
const yaml = require("js-yaml");
|
||||
const fs = require("node:fs");
|
||||
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.yellow("Loading configuration..."));
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const cliColor = require("cli-color");
|
||||
const config = require("./configuration.js");
|
||||
const cliColor = require("cli-color");
|
||||
const axios = require("axios");
|
||||
|
||||
module.exports = async function getAllNodes() {
|
||||
|
||||
@@ -1,92 +1,136 @@
|
||||
const { EmbedBuilder } = require("discord.js");
|
||||
const fs = require("node:fs");
|
||||
const cliColor = require("cli-color");
|
||||
const path = require('node:path');
|
||||
const webhook = require("./webhook.js");
|
||||
const config = require("./configuration.js");
|
||||
const getNodesDetails = require("./getNodesDetails.js");
|
||||
const getNodeConfiguration = require("./getNodeConfiguration.js");
|
||||
const getNodesDetails = require("./getNodesDetails.js");
|
||||
const getWingsStatus = require("./getWingsStatus.js");
|
||||
const promiseTimeout = require("./promiseTimeout.js");
|
||||
const sendMessage = require("./sendMessage.js")
|
||||
const getServers = require("./getServers.js");
|
||||
const config = require("./configuration.js");
|
||||
const getUsers = require("./getUsers.js");
|
||||
const webhook = require("./webhook.js");
|
||||
const cliColor = require("cli-color");
|
||||
const path = require('node:path');
|
||||
const fs = require("node:fs");
|
||||
|
||||
module.exports = async function getStats() {
|
||||
let cache = (() => {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(path.join(__dirname, "../cache.json")))
|
||||
} catch {
|
||||
return false
|
||||
module.exports = async function getStats(client) {
|
||||
try {
|
||||
let cache = (() => {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(path.join(__dirname, "../cache.json")))
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
})()
|
||||
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.yellow("Retrieving panel nodes..."))
|
||||
const nodesStats = await promiseTimeout(getNodesDetails(), config.timeout * 1000);
|
||||
if (!nodesStats) throw new Error("Failed to get nodes attributes");
|
||||
|
||||
const statusPromises = nodesStats.slice(0, config.nodes_settings.limit).map(async (node) => {
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.yellow(`Fetching ${cliColor.blueBright(node.attributes.name)} configuration...`))
|
||||
const nodeConfig = await getNodeConfiguration(node.attributes.id);
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.yellow(`Checking ${cliColor.blueBright(node.attributes.name)} wings status...`))
|
||||
const nodeStatus = await promiseTimeout(getWingsStatus(node, nodeConfig.token), config.timeout * 1000);
|
||||
|
||||
let nodeUptime = cache ? (() => {
|
||||
return cache.nodes.find((n) => n.attributes.id === node.attributes.id)?.uptime || Date.now()
|
||||
})() : Date.now()
|
||||
|
||||
if (!nodeUptime && nodeStatus) nodeUptime = Date.now()
|
||||
|
||||
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 {
|
||||
attributes: {
|
||||
id: node.attributes.id,
|
||||
name: node.attributes.name,
|
||||
memory: node.attributes.memory,
|
||||
disk: node.attributes.disk,
|
||||
cpu: node.attributes.cpu,
|
||||
fqdn: node.attributes.fqdn,
|
||||
allocated_resources: node.attributes.allocated_resources,
|
||||
relationships: {
|
||||
allocations: node.attributes.relationships.allocations.data.length,
|
||||
servers: node.attributes.relationships.servers.data.length
|
||||
}
|
||||
},
|
||||
uptime: nodeUptime,
|
||||
status: nodeStatus
|
||||
};
|
||||
});
|
||||
|
||||
const data = {
|
||||
uptime: cache ? (() => {
|
||||
return cache.uptime || Date.now()
|
||||
})() : Date.now(),
|
||||
servers: await getServers(),
|
||||
users: await getUsers(),
|
||||
nodes: await Promise.all(statusPromises),
|
||||
isPanelDown: !cache.uptime,
|
||||
timestamp: Date.now()
|
||||
}
|
||||
})()
|
||||
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.yellow("Retrieving panel nodes..."))
|
||||
const nodesStats = await getNodesDetails();
|
||||
if (!nodesStats) throw new Error("Failed to get nodes attributes");
|
||||
fs.writeFileSync("cache.json", JSON.stringify(data, null, 2), "utf8");
|
||||
|
||||
const statusPromises = nodesStats.slice(0, config.nodes_settings.limit).map(async (node) => {
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.yellow(`Fetching ${cliColor.blueBright(node.attributes.name)} configuration...`))
|
||||
const nodeConfig = await getNodeConfiguration(node.attributes.id);
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.yellow(`Checking ${cliColor.blueBright(node.attributes.name)} wings status...`))
|
||||
const nodeStatus = await promiseTimeout(getWingsStatus(node, nodeConfig.token), config.timeout * 1000);
|
||||
if (data.isPanelDown) webhook(
|
||||
new EmbedBuilder()
|
||||
.setTitle("Panel Online")
|
||||
.setColor("57F287")
|
||||
.setDescription(`Panel is back online`)
|
||||
)
|
||||
|
||||
let nodeUptime = cache ? (() => {
|
||||
return cache.nodes.find((n) => n.attributes.id === node.attributes.id)?.uptime || Date.now()
|
||||
})() : Date.now()
|
||||
data.client = client
|
||||
data.panel = true
|
||||
|
||||
if (!nodeUptime && nodeStatus) nodeUptime = Date.now()
|
||||
return sendMessage(data);
|
||||
} catch (error) {
|
||||
if (config.log_error) console.error(error)
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.redBright("Panel is currently offline."));
|
||||
|
||||
if (!nodeStatus) {
|
||||
nodeUptime = false
|
||||
if (cache && cache.nodes.find((n) => n.attributes.id === node.attributes.id)?.status)
|
||||
webhook(
|
||||
return fs.readFile(path.join(__dirname, "../cache.json"), (err, data) => {
|
||||
if (err) {
|
||||
sendMessage({ client, cache: false, panel: false });
|
||||
return console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.redBright("Last cache was not found!"));
|
||||
}
|
||||
|
||||
try {
|
||||
const results = JSON.parse(data);
|
||||
if (results.uptime) webhook(
|
||||
new EmbedBuilder()
|
||||
.setTitle("Node Offline")
|
||||
.setTitle("Panel 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`)
|
||||
)
|
||||
}
|
||||
.setDescription(`Panel is currently offline`)
|
||||
);
|
||||
results.uptime = false;
|
||||
fs.writeFileSync("cache.json", JSON.stringify(results, null, 2), "utf8");
|
||||
|
||||
return {
|
||||
attributes: {
|
||||
id: node.attributes.id,
|
||||
name: node.attributes.name,
|
||||
memory: node.attributes.memory,
|
||||
disk: node.attributes.disk,
|
||||
cpu: node.attributes.cpu,
|
||||
fqdn: node.attributes.fqdn,
|
||||
allocated_resources: node.attributes.allocated_resources,
|
||||
relationships: {
|
||||
allocations: node.attributes.relationships.allocations.data.length,
|
||||
servers: node.attributes.relationships.servers.data.length
|
||||
}
|
||||
},
|
||||
uptime: nodeUptime,
|
||||
status: nodeStatus
|
||||
};
|
||||
});
|
||||
results.client = client
|
||||
results.panel = false
|
||||
results.cache = true
|
||||
return sendMessage(results);
|
||||
} catch {
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.redBright("Something went wrong with cache data..."));
|
||||
|
||||
const data = {
|
||||
uptime: cache ? (() => {
|
||||
return cache.uptime || Date.now()
|
||||
})() : Date.now(),
|
||||
servers: await getServers(),
|
||||
users: await getUsers(),
|
||||
nodes: await Promise.all(statusPromises),
|
||||
isPanelDown: !cache.uptime,
|
||||
timestamp: Date.now()
|
||||
return sendMessage({ client, cache: false, panel: false });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fs.writeFileSync("cache.json", JSON.stringify(data, null, 2), "utf8");
|
||||
|
||||
return data
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
const config = require("./configuration.js");
|
||||
const cliColor = require("cli-color");
|
||||
|
||||
module.exports = async function getUsers() {
|
||||
|
||||
154
handlers/sendMessage.js
Normal file
154
handlers/sendMessage.js
Normal file
@@ -0,0 +1,154 @@
|
||||
|
||||
const { EmbedBuilder, time, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require("discord.js");
|
||||
const uptimeFormatter = require("./uptimeFormatter.js");
|
||||
const convertUnits = require("./convertUnits.js");
|
||||
const config = require("./configuration.js");
|
||||
const cliColor = require("cli-color");
|
||||
|
||||
module.exports = async function sendMessage({ client, cache, panel, uptime, nodes, servers, users }) {
|
||||
let embed = new EmbedBuilder()
|
||||
.setAuthor({
|
||||
name: config.embed.nodes.author.name || null,
|
||||
iconURL: config.embed.nodes.author.icon || null
|
||||
})
|
||||
.setDescription(config.embed.nodes.description || null)
|
||||
.setTitle(config.embed.nodes.title || null)
|
||||
.setColor(config.embed.nodes.color || null)
|
||||
.setURL(config.embed.nodes.url || null)
|
||||
.setThumbnail(config.embed.nodes.thumbnail || null)
|
||||
|
||||
let embeds = [embed];
|
||||
|
||||
if (config.nodes_settings.details) {
|
||||
nodes.forEach((node, index) => {
|
||||
if (index % 25 === 0 && index !== 0) {
|
||||
embed = new EmbedBuilder().setColor(config.embed.nodes.color);
|
||||
if (embeds.length < 9) embeds.push(embed);
|
||||
}
|
||||
|
||||
embed.addFields({
|
||||
name: `${node.attributes.name} - ${node.status ? config.status.online : config.status.offline}`,
|
||||
value:
|
||||
"```\n" +
|
||||
(config.nodes_settings.host ? `Host : ${node.attributes.fqdn}\n` : "") +
|
||||
`Memory : ${convertUnits(node.attributes.allocated_resources.memory, node.attributes.memory, config.nodes_settings.unit)}\n` +
|
||||
`Disk : ${convertUnits(node.attributes.allocated_resources.disk, node.attributes.disk, config.nodes_settings.unit)}` +
|
||||
(node.attributes?.allocated_resources?.cpu ? `\nCPU : ${node.attributes?.allocated_resources?.cpu || 0}%` : "") +
|
||||
(config.nodes_settings.servers ? `\nServers: ${node.attributes.relationships.servers}${config.nodes_settings.allocations_as_max_servers ? ` / ${node.attributes.relationships.allocations}` : ""}` : "") +
|
||||
(config.nodes_settings.uptime ? `\nUptime : ${node.uptime ? uptimeFormatter(Date.now() - node.uptime) : "N/A"}` : "") +
|
||||
"```"
|
||||
});
|
||||
});
|
||||
} else {
|
||||
embeds[0].setDescription((embed.data.description ? (embed.data.description + "\n\n") : "") + nodes.map(node => `**${node.attributes.name}** - ${node.status ? config.status.online : config.status.offline}`).join("\n"));
|
||||
}
|
||||
|
||||
let panelEmbed = new EmbedBuilder()
|
||||
.setAuthor({
|
||||
name: config.embed.panel.author.name || null,
|
||||
iconURL: config.embed.panel.author.icon || null
|
||||
})
|
||||
.setColor(config.embed.panel.color || null)
|
||||
.setTitle(config.embed.panel.title || null)
|
||||
.setURL(config.embed.panel.url || null)
|
||||
.setTimestamp(config.embed.panel.timestamp ? new Date() : null)
|
||||
.setFooter({
|
||||
text: config.embed.panel.footer.text || null,
|
||||
iconURL: config.embed.panel.footer.icon || null
|
||||
})
|
||||
.setThumbnail(config.embed.panel.thumbnail || null)
|
||||
.setImage(config.embed.panel.image || null)
|
||||
.setDescription(
|
||||
config.embed.panel.description
|
||||
.replace(
|
||||
"{{time}}",
|
||||
time(new Date(Date.now() + (config.refresh * 1000) + 1000), "R")
|
||||
) || null
|
||||
)
|
||||
.addFields({
|
||||
name: `Panel - ${panel ? config.status.online : config.status.offline}`,
|
||||
value:
|
||||
"```\n" +
|
||||
(config.panel_settings.host ? `Host : ${new URL(process.env.PanelURL).host}\n` : "") +
|
||||
`Nodes : ${nodes.length}\n` +
|
||||
(config.panel_settings.servers ? `Servers: ${servers || "Unknown"}\n` : "") +
|
||||
(config.panel_settings.users ? `Users : ${users || "Unknown"}\n` : "") +
|
||||
(config.panel_settings.uptime ? `Uptime : ${uptime ? uptimeFormatter(Date.now() - uptime) : "N/A"}\n` : "") +
|
||||
"```"
|
||||
});
|
||||
|
||||
if (config.panel_settings.status) embeds.unshift(panelEmbed);
|
||||
|
||||
embeds[embeds.length - 1]
|
||||
.setTimestamp(config.embed.nodes.timestamp ? new Date() : null)
|
||||
.setFooter({
|
||||
text: config.embed.nodes.footer.text || null,
|
||||
iconURL: config.embed.nodes.footer.icon || null
|
||||
})
|
||||
.setImage(config.embed.nodes.image || null)
|
||||
|
||||
if (!cache && !panel) {
|
||||
embeds[embeds.length - 1].setDescription(
|
||||
embeds[embeds.length - 1].data.description ? embeds[embeds.length - 1].data.description + "\n\nThere are no nodes to be display!" : "There are no nodes to be display!"
|
||||
);
|
||||
}
|
||||
|
||||
const components = []
|
||||
|
||||
if (config.button.enable) {
|
||||
for (const row of ["row1", "row2", "row3", "row4", "row5"]) {
|
||||
const buttons = config.button[row]?.slice(0, 5).filter(button => button.label && button.url);
|
||||
|
||||
if (buttons && buttons.length > 0) {
|
||||
components.push(
|
||||
new ActionRowBuilder().addComponents(
|
||||
buttons.map(button =>
|
||||
new ButtonBuilder()
|
||||
.setLabel(button.label)
|
||||
.setURL(button.url)
|
||||
.setStyle(ButtonStyle.Link)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const channel = await client.channels.fetch(process.env?.DiscordChannel);
|
||||
const messages = await channel.messages.fetch({ limit: 10 });
|
||||
const botMessage = messages.find(msg => msg.author.id === client.user.id);
|
||||
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.green(`Panel stats successfully posted to the ${cliColor.blueBright(channel.name)} channel!`));
|
||||
|
||||
setTimeout(() => client.getStats(client), config.refresh * 1000);
|
||||
|
||||
if (botMessage) {
|
||||
await botMessage.edit({ content: config.message.content || null, embeds, components });
|
||||
} else {
|
||||
await channel.send({ content: config.message.content || null, embeds, components });
|
||||
}
|
||||
} catch (error) {
|
||||
try {
|
||||
if (error.rawError?.code === 429) {
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.redBright("Error 429 | Your IP has been rate limited by either Discord or your website. If it's a rate limit with Discord, you must wait. If it's a issue with your website, consider whitelisting your server IP."));
|
||||
} else if (error.rawError?.code === 403) {
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.redBright("FORBIDDEN | The channel ID you provided is incorrect. Please double check you have the right ID. If you're not sure, read our documentation: \n>>https://github.com/HirziDevs/PteroStats#getting-channel-id<<"));
|
||||
} else if (error.code === "ENOTFOUND") {
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.redBright("ENOTFOUND | DNS Error. Ensure your network connection and DNS server are functioning correctly."));
|
||||
} else if (error.rawError?.code === 50001) {
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.redBright("Discord Error | Your discord bot doesn't have access to see/send message/edit message in the channel!"));
|
||||
} else if (error.rawError?.errors && Object?.values(error.rawError.errors)[0]?._errors[0]?.code === "MAX_EMBED_SIZE_EXCEEDED") {
|
||||
console.log(cliColor.cyanBright("[PteroStats] ") + cliColor.redBright("Discord Error | Embed message limit exceeded! Please limit or decrease the nodes that need to be shown in the config!"));
|
||||
} else if (error.rawError?.errors && Object?.values(error.rawError.errors)[0]?._errors[0]?.code) {
|
||||
console.log(Object.values(error.rawError.errors)[0]._errors[0].message);
|
||||
} else {
|
||||
console.error(cliColor.cyanBright("[PteroStats] ") + cliColor.redBright("Discord Error"), error);
|
||||
}
|
||||
process.exit();
|
||||
} catch (err) {
|
||||
console.error(error)
|
||||
process.exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
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
|
||||
});
|
||||
const cliColor = require("cli-color");
|
||||
const axios = require("axios");
|
||||
const fs = require("node:fs");
|
||||
|
||||
const questions = [
|
||||
"Please enter your panel name: ",
|
||||
|
||||
@@ -2,7 +2,7 @@ const { WebhookClient, EmbedBuilder } = require("discord.js")
|
||||
const config = require("./configuration")
|
||||
const cliColor = require("cli-color")
|
||||
|
||||
module.exports = function Webhook(embed) {
|
||||
module.exports = function webhook(embed) {
|
||||
if (config.notifier.enable) {
|
||||
try {
|
||||
const webhook = new WebhookClient({
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
"dependencies": {
|
||||
"axios": "1.7.7",
|
||||
"cli-color": "2.0.4",
|
||||
"discord.js": "14.16.2",
|
||||
"discord.js": "14.16.3",
|
||||
"dotenv": "16.4.5",
|
||||
"js-yaml": "4.1.0",
|
||||
"prettier-bytes": "1.0.4"
|
||||
|
||||
Reference in New Issue
Block a user