This repository was archived by the owner on Sep 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
168 lines (138 loc) Β· 5.33 KB
/
index.js
File metadata and controls
168 lines (138 loc) Β· 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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 readline = require("readline");
const handleCommands = require("./utils/commands");
const package = require("./package.json");
require("dotenv").config();
(async () => {
// Console Log
console.log("\n");
console.log("π€ Bot is starting...");
console.log(`π¦ HStudioSource@${package.version}`);
console.log(`π€ Bot is running on version ${package.version}`);
console.log(`π Node.js version: ${process.version}`);
console.log(`π§ Environment: ${process.env.NODE_ENV || "development"}`);
console.log(`π Token: ${process.env.TOKEN}`);
console.log(`𦴠Shards: ${process.env.SHARDS || "auto"}`);
console.log("\n");
console.log("π Versions");
console.log(`π¦ Discord.js: ${package.dependencies["discord.js"]}`);
console.log(`π¦ Moonlink.js: ${package.dependencies["moonlink.js"]}`);
// Register Bot Commands
console.log("\n");
await handleCommands();
// Sharding Manager
const manager = new ShardingManager("bot.js", {
token: process.env.TOKEN,
totalShards: parseInt(process.env.SHARDS) || process.env.SHARDS || "auto"
});
manager.on("shardCreate", shard => {
shard.on("death", () => {
console.log(`[${shard.id}] is Death`);
console.log(`[${shard.id}] Reswawning...`);
});
console.log(`π Launched shard ${shard.id}`);
});
manager.spawn();
// API Manager
const port = process.env.PORT || 8233;
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: package.version,
totalShards: manager.totalShards,
shards: shards
};
return res.status(200).json(status);
});
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) => {
let guildsList = [];
let after = null;
try {
while (true) {
const queryParams = after ? { after, limit: 200 } : { limit: 200 };
const guilds = await rest.get(Routes.userGuilds(), { query: queryParams });
if (guilds.length === 0) break;
guildsList.push(...guilds.map((val) => val.id));
if (guilds.length < 200) break;
after = guilds[guilds.length - 1].id;
};
return res.status(200).json({
total: guildsList.length,
list: guildsList
});
} catch (error) {
console.error(error);
return res.status(500).json({ error: "An error occurred while fetching guilds" });
}
});
api.listen(port, () => {
console.log(`\n\nπ Status page server is running at http://localhost:${port}/status\n\n`);
});
// Handle Console Input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on("line", async (input) => {
const cmdCommand = input.trim().toLowerCase();
switch (cmdCommand) {
case "offline": {
console.log("π Soft Offline all shards...");
await manager.broadcast({ operation: "offline", value: true });
console.log("β
All shards soft offline successfully.");
break;
}
case "online": {
console.log("π’ Online all shards...");
await manager.broadcast({ operation: "online", value: true });
console.log("β
All shards online successfully.");
break;
}
case "restart": {
console.log("π Restarting all shards...");
try {
// Restart all shards
await manager.respawnAll();
console.log("β
All shards restarted successfully.");
} catch (error) {
console.error("β Error while restarting shards:", error);
}
break;
}
default: {
console.log(`Unknown command: ${input}`);
break;
}
}
});
})();