-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
198 lines (174 loc) · 5.4 KB
/
index.js
File metadata and controls
198 lines (174 loc) · 5.4 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
require("dotenv").config();
const { Client, GatewayIntentBits, EmbedBuilder } = require("discord.js");
const { getSystemStats } = require("./src/utils/status");
const fs = require("fs");
const path = require("path");
const { Collection } = require("discord.js");
const deployCommands = require("./deploy-commands");
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.commands = new Collection();
deployCommands()
.then(() => {
console.log("✅ Slash commands deployed!");
})
.catch((err) => {
console.error("❌ Failed to deploy commands:", err);
});
const commandsPath = path.join(__dirname, "src", "commands");
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js"));
let statusMessage = null;
let isRateLimited = false;
let lastStats = null;
let channel = null;
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
client.commands.set(command.data.name, command);
}
function hasStatsChanged(newStats, oldStats) {
if (!oldStats) return true;
return (
newStats.cpuUsage !== oldStats.cpuUsage ||
newStats.memory !== oldStats.memory ||
newStats.temperature !== oldStats.temperature ||
newStats.diskUsage !== oldStats.diskUsage ||
newStats.uptime !== oldStats.uptime
);
}
async function updateLoop() {
const start = Date.now();
if (!isRateLimited) {
try {
const stats = await getSystemStats();
if (!hasStatsChanged(stats, lastStats)) {
scheduleNext(start);
return;
}
lastStats = stats;
const embed = new EmbedBuilder()
.setColor(0x00b0f4)
.setAuthor({
name: "📡 Status",
iconURL: "https://cdn-icons-png.flaticon.com/512/2920/2920346.png",
})
.addFields(
{
name: "🧠 RAM Memory",
value: `\`\`\`yaml\n${stats.memory}\n\`\`\``,
inline: true,
},
{
name: "🔥 CPU Temperature",
value:
stats.temperature === "N/A"
? "`No data`"
: `\`\`\`fix\n${stats.temperature} °C\n\`\`\``,
inline: true,
},
{
name: "⚙️ CPU Load",
value: `\`\`\`css\n${stats.cpuUsage} %\n\`\`\``,
inline: true,
},
{
name: "💾 Disk",
value: `\`\`\`yaml\n${stats.diskUsage}\n\`\`\``,
inline: true,
},
{
name: "⏱️ Uptime",
value: `\`\`\`diff\n+ ${stats.uptime}\n\`\`\``,
inline: true,
}
)
.setFooter({
text: `Monitoring • ${new Date().toLocaleString()}`,
iconURL: "https://cdn-icons-png.flaticon.com/512/2698/2698993.png",
})
.setTimestamp();
if (!statusMessage) {
console.log(
`Sending initial status message to channel ${channel.id}.`
);
statusMessage = await channel.send({ embeds: [embed] });
} else {
await statusMessage.edit({ embeds: [embed] });
}
} catch (error) {
if (
error.code === 20028 ||
error.code === 50013 ||
error.httpStatus === 429
) {
console.warn("Rate limit hit!");
isRateLimited = true;
setTimeout(() => {
isRateLimited = false;
}, 5000);
} else {
console.error("Error:", error);
}
}
}
scheduleNext(start);
}
function scheduleNext(startTime) {
const elapsed = Date.now() - startTime;
const delay = Math.max(1000 - elapsed, 0);
setTimeout(updateLoop, delay);
}
async function clearChannelMessages(channel) {
try {
while (true) {
const fetched = await channel.messages.fetch({ limit: 100 });
const deletable = fetched.filter((msg) => msg.deletable);
if (deletable.size > 0) {
console.log(
`Deleting ${deletable.size} existing message(s) before sending status.`
);
await channel.bulkDelete(deletable, true);
}
// Stop when fewer than 100 messages were fetched (end of history)
// or when there is nothing left the bot is allowed to delete.
if (fetched.size < 100 || deletable.size === 0) {
break;
}
}
console.log("Channel has been cleared.");
} catch (err) {
console.error("Error while clearing channel:", err);
}
}
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
console.log(
`Executing command /${interaction.commandName} by ${interaction.user.tag} (${interaction.user.id})`
);
await command.execute(interaction);
} catch (error) {
console.error("Command error:", error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({
content: "❌ An error occurred while executing the command.",
ephemeral: true,
});
} else {
await interaction.reply({
content: "❌ An error occurred while executing the command.",
ephemeral: true,
});
}
}
});
client.once("clientReady", async () => {
console.log(`Logged in as ${client.user.tag}`);
channel = await client.channels.fetch(process.env.DISCORD_CHANNEL_ID);
await clearChannelMessages(channel);
updateLoop();
});
client.login(process.env.DISCORD_TOKEN);