Brozy123 commited on
Commit
6b07dee
·
verified ·
1 Parent(s): ac5aa34

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +55 -2
index.js CHANGED
@@ -133,6 +133,32 @@ client.on('invalidated', () => {
133
  process.exit(1);
134
  });
135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  client.commands = new Collection();
137
 
138
  // --- 1. CONFIG ---
@@ -970,7 +996,7 @@ client.on('messageCreate', async (message) => {
970
  const mentions = scorers.match(/<@!?(\d+)>/g) || [];
971
  mentions.forEach(m => storage.addStat(m.replace(/[<@!>]/g, ''), 'goals'));
972
 
973
- storage.save('records');
974
  const embed = UI.createEmbed(`⭐ ${type.toUpperCase()} RECORDED`, `🏟️ **Result**: ${res}\n\n⚽ **Scorers**:\n${scorers || "None"}\n\n📊 *Matches have been recorded for everyone on the current lineup board.*`, type === 'win' ? UI.colors.success : UI.colors.danger);
975
  await message.channel.send({ embeds: [embed] });
976
  return;
@@ -1522,6 +1548,11 @@ Tip: All commands support Discord Slash Commands (/) for a better experience!`;
1522
 
1523
  async function safeReply(interaction, options) {
1524
  try {
 
 
 
 
 
1525
  if (interaction.deferred || interaction.replied) {
1526
  return await interaction.editReply(options);
1527
  } else {
@@ -1840,7 +1871,7 @@ client.on('interactionCreate', async (interaction) => {
1840
  storage.addStat(uid, 'goals');
1841
  });
1842
 
1843
- storage.save('records');
1844
  const embed = UI.createEmbed(`⭐ ${type.toUpperCase()} RECORDED`, `🏟️ **Result**: ${res}\n\n⚽ **Scorers**:\n${scorers || "None"}\n\n📊 *Matches have been recorded for everyone on the current lineup board.*`, type === 'win' ? UI.colors.success : UI.colors.danger);
1845
  await safeReply(interaction, { embeds: [embed] });
1846
  }
@@ -3741,6 +3772,28 @@ async function initialize() {
3741
 
3742
  console.log(`[24/7 HEARTBEAT] Uptime: ${h}h ${m}m | Discord: ${connected ? 'CONNECTED ✓' : 'DISCONNECTED ✗'} | WS: ${wsStatus}`);
3743
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3744
  // Track how long we've been disconnected
3745
  if (!connected) {
3746
  if (!disconnectedSince) disconnectedSince = Date.now();
 
133
  process.exit(1);
134
  });
135
 
136
+ // Clean shutdown handlers for container restarts (Hugging Face)
137
+ process.on('SIGTERM', () => {
138
+ console.log('[SYSTEM] SIGTERM received. Shutting down cleanly...');
139
+ if (client) {
140
+ try {
141
+ client.destroy();
142
+ console.log('[SYSTEM] Discord client destroyed.');
143
+ } catch (err) {
144
+ console.error('[SYSTEM] Error destroying client:', err);
145
+ }
146
+ }
147
+ process.exit(0);
148
+ });
149
+ process.on('SIGINT', () => {
150
+ console.log('[SYSTEM] SIGINT received. Shutting down cleanly...');
151
+ if (client) {
152
+ try {
153
+ client.destroy();
154
+ console.log('[SYSTEM] Discord client destroyed.');
155
+ } catch (err) {
156
+ console.error('[SYSTEM] Error destroying client:', err);
157
+ }
158
+ }
159
+ process.exit(0);
160
+ });
161
+
162
  client.commands = new Collection();
163
 
164
  // --- 1. CONFIG ---
 
996
  const mentions = scorers.match(/<@!?(\d+)>/g) || [];
997
  mentions.forEach(m => storage.addStat(m.replace(/[<@!>]/g, ''), 'goals'));
998
 
999
+ storage.save('config');
1000
  const embed = UI.createEmbed(`⭐ ${type.toUpperCase()} RECORDED`, `🏟️ **Result**: ${res}\n\n⚽ **Scorers**:\n${scorers || "None"}\n\n📊 *Matches have been recorded for everyone on the current lineup board.*`, type === 'win' ? UI.colors.success : UI.colors.danger);
1001
  await message.channel.send({ embeds: [embed] });
1002
  return;
 
1548
 
1549
  async function safeReply(interaction, options) {
1550
  try {
1551
+ if (typeof options === 'string') {
1552
+ options = options.slice(0, 2000);
1553
+ } else if (options && typeof options.content === 'string') {
1554
+ options.content = options.content.slice(0, 2000);
1555
+ }
1556
  if (interaction.deferred || interaction.replied) {
1557
  return await interaction.editReply(options);
1558
  } else {
 
1871
  storage.addStat(uid, 'goals');
1872
  });
1873
 
1874
+ storage.save('config');
1875
  const embed = UI.createEmbed(`⭐ ${type.toUpperCase()} RECORDED`, `🏟️ **Result**: ${res}\n\n⚽ **Scorers**:\n${scorers || "None"}\n\n📊 *Matches have been recorded for everyone on the current lineup board.*`, type === 'win' ? UI.colors.success : UI.colors.danger);
1876
  await safeReply(interaction, { embeds: [embed] });
1877
  }
 
3772
 
3773
  console.log(`[24/7 HEARTBEAT] Uptime: ${h}h ${m}m | Discord: ${connected ? 'CONNECTED ✓' : 'DISCONNECTED ✗'} | WS: ${wsStatus}`);
3774
 
3775
+ // REST Health Check
3776
+ let restHealthy = false;
3777
+ if (connected) {
3778
+ try {
3779
+ // Perform a simple lightweight API request to verify the proxy/REST channel is healthy
3780
+ await client.users.fetch(client.user.id);
3781
+ restHealthy = true;
3782
+ } catch (err) {
3783
+ console.error(`[REST HEALTH] REST request failed via ${client.options.rest.api || 'default'}: ${err?.message || err}`);
3784
+ }
3785
+
3786
+ // If REST failed but Gateway is connected, rotate REST endpoints to recover connectivity
3787
+ if (!restHealthy) {
3788
+ console.warn("[REST HEALTH] REST API is unresponsive. Attempting to rotate REST endpoint...");
3789
+ currentEndpointIndex = (currentEndpointIndex + 1) % DISCORD_ENDPOINTS.length;
3790
+ const nextApi = DISCORD_ENDPOINTS[currentEndpointIndex];
3791
+ console.log(`[REST HEALTH] Switching REST API endpoint to: ${nextApi}`);
3792
+ client.options.rest.api = nextApi;
3793
+ if (client.rest) client.rest.options.api = nextApi;
3794
+ }
3795
+ }
3796
+
3797
  // Track how long we've been disconnected
3798
  if (!connected) {
3799
  if (!disconnectedSince) disconnectedSince = Date.now();