From 11bfbfd8dd3dbc040212bb187c0acfc1b2811f9a Mon Sep 17 00:00:00 2001 From: HewkawAr Date: Sat, 20 Jul 2024 19:02:09 +0700 Subject: [PATCH 1/2] Update index.js --- index.js | 65 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 90c02f7..e51ee50 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,10 @@ const { ShardingManager } = require("discord.js"); +const { REST } = require("@discordjs/rest"); +const { Routes } = require("discord-api-types/v10"); const express = require("express"); const cors = require("cors"); -const { version } = require("./package.json"); +const package = require("./package.json"); require("dotenv").config(); @@ -20,13 +22,7 @@ manager.on("shardCreate", shard => { console.log(`Launched shard ${shard.id}`); }); -manager.spawn().then(shards => { - shards.forEach(shard => { - shard.on("message", message => { - console.log(`Shard[${shard.id}] : ${message._eval} : ${message._result}`); - }); - }); -}).catch(console.error); +manager.spawn(); // API Manager const port = process.env.PORT || 8233; @@ -35,24 +31,59 @@ const api = express(); api.use(cors()); api.use(express.json()); +const rest = new REST({ + version: "10" +}).setToken(process.env.TOKEN); + +api.get("/invite", async (req, res) => { + return res.redirect(`https://discord.com/oauth2/authorize?client_id=${process.env.CLIENT_ID}`) +}); + api.get("/status", async (req, res) => { + const shards = manager.shards.map((shard) => { return { id: shard.id, online: shard.ready } }); + const status = { - version: version, - totalShards: manager.totalShards + version: package.version, + totalShards: manager.totalShards, + shards: shards }; - status.shards = []; + return res.status(200).json(status); +}); - manager.shards.forEach(shard => { - status.shards.push({ - id: shard.id, - online: shard.ready, - }); - }); +api.get("/status/all", async (req, res) => { + const shards = manager.shards.map((shard) => { return { id: shard.id, online: shard.ready } }); + + const guilds = await rest.get(Routes.userGuilds()); + const user = await rest.get(Routes.user()); + + const guildsList = guilds.map((val) => val.id); + + const status = { + name: user.username, + version: package.version, + totalShards: manager.totalShards, + shards: shards, + guilds: { + total: guildsList.length, + list: guildsList + } + }; return res.status(200).json(status); }); +api.get("/guilds", async (req, res) => { + const guilds = await rest.get(Routes.userGuilds()); + + const guildsList = guilds.map((val) => val.id); + + return res.status(200).json({ + total: guildsList.length, + list: guildsList + }); +}); + api.listen(port, () => { console.log(`Status page server is running at http://localhost:${port}/status`); }); \ No newline at end of file From 919a74857879ec1ab427ba03487d9212967dcaf0 Mon Sep 17 00:00:00 2001 From: HewkawAr Date: Sat, 20 Jul 2024 19:05:38 +0700 Subject: [PATCH 2/2] Linted index.js --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index e51ee50..30e887b 100644 --- a/index.js +++ b/index.js @@ -36,11 +36,11 @@ const rest = new REST({ }).setToken(process.env.TOKEN); api.get("/invite", async (req, res) => { - return res.redirect(`https://discord.com/oauth2/authorize?client_id=${process.env.CLIENT_ID}`) + return res.redirect(`https://discord.com/oauth2/authorize?client_id=${process.env.CLIENT_ID}`); }); api.get("/status", async (req, res) => { - const shards = manager.shards.map((shard) => { return { id: shard.id, online: shard.ready } }); + const shards = manager.shards.map((shard) => { return { id: shard.id, online: shard.ready }; }); const status = { version: package.version, @@ -52,7 +52,7 @@ api.get("/status", async (req, res) => { }); api.get("/status/all", async (req, res) => { - const shards = manager.shards.map((shard) => { return { id: shard.id, online: shard.ready } }); + const shards = manager.shards.map((shard) => { return { id: shard.id, online: shard.ready }; }); const guilds = await rest.get(Routes.userGuilds()); const user = await rest.get(Routes.user());