mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-25 11:39:51 +01:00
more adrenaline
This commit is contained in:
1 parent
f396d6c4a1
commit
f6cb4f2518
1 file changed
+59
-16
+59
-16
@@ -5,40 +5,77 @@ import {
|
|||||||
} from './core/blitzkit/nonBlockingPromises';
|
} from './core/blitzkit/nonBlockingPromises';
|
||||||
import { manager } from './core/discord/manager';
|
import { manager } from './core/discord/manager';
|
||||||
|
|
||||||
console.log('pre manager event listeners and spawn');
|
async function setupShardManager() {
|
||||||
|
console.log('Pre-manager setup starting...');
|
||||||
|
|
||||||
const shards = await manager
|
// Set up a promise that resolves when all shards are ready
|
||||||
.on('shardCreate', (shard) => {
|
const readyShards = new Set();
|
||||||
|
const totalShards = manager.totalShards;
|
||||||
|
|
||||||
|
// Create a promise that resolves when all shards are ready
|
||||||
|
const allShardsReady = new Promise((resolve, reject) => {
|
||||||
|
const timeout = setTimeout(() => {
|
||||||
|
reject(new Error('Shard initialization timed out after 60 seconds'));
|
||||||
|
}, 60000); // 60 second timeout
|
||||||
|
|
||||||
|
manager.on('shardCreate', (shard) => {
|
||||||
console.log(`🟡 Launching shard ${shard.id}`);
|
console.log(`🟡 Launching shard ${shard.id}`);
|
||||||
shard.on('ready', () => console.log(`🟢 Launched shard ${shard.id}`));
|
|
||||||
})
|
|
||||||
.spawn();
|
|
||||||
|
|
||||||
console.log('post manager event listeners and spawn');
|
shard.on('ready', () => {
|
||||||
|
console.log(`🟢 Launched shard ${shard.id}`);
|
||||||
|
readyShards.add(shard.id);
|
||||||
|
|
||||||
|
if (readyShards.size === totalShards) {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
resolve(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
shard.on('error', (error) => {
|
||||||
|
console.error(`❌ Shard ${shard.id} encountered an error:`, error);
|
||||||
|
});
|
||||||
|
|
||||||
|
shard.on('disconnect', () => {
|
||||||
|
console.warn(`⚠️ Shard ${shard.id} disconnected`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
console.log('Spawning shards...');
|
||||||
|
const shards = await manager.spawn();
|
||||||
|
console.log('Shards spawned, waiting for ready state...');
|
||||||
|
|
||||||
|
// Wait for all shards to be ready
|
||||||
|
await allShardsReady;
|
||||||
|
console.log('All shards ready, setting up presence...');
|
||||||
|
|
||||||
|
// Fetch statistics
|
||||||
let servers = 0;
|
let servers = 0;
|
||||||
let channels = 0;
|
let channels = 0;
|
||||||
let users = 0;
|
let users = 0;
|
||||||
|
|
||||||
for (const [, shard] of shards) {
|
for (const [, shard] of shards) {
|
||||||
// wtf is this typing bro???
|
try {
|
||||||
const guilds = (await shard.fetchClientValue('guilds.cache')) as any;
|
const guilds = (await shard.fetchClientValue('guilds.cache')) as any;
|
||||||
|
|
||||||
servers += guilds.length;
|
servers += guilds.length;
|
||||||
|
|
||||||
for (const guild of guilds) {
|
for (const guild of guilds) {
|
||||||
channels += guild.channels.length;
|
channels += guild.channels.length;
|
||||||
users += guild.memberCount;
|
users += guild.memberCount;
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Failed to fetch statistics from shard:`, error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const awaitedTankDefinitions = await tankDefinitions;
|
const [awaitedTankDefinitions, awaitedDiscoveredIdsDefinitions] =
|
||||||
|
await Promise.all([tankDefinitions, discoveredIdsDefinitions]);
|
||||||
|
|
||||||
const tanks = Object.values(awaitedTankDefinitions.tanks).length;
|
const tanks = Object.values(awaitedTankDefinitions.tanks).length;
|
||||||
const awaitedDiscoveredIdsDefinitions = await discoveredIdsDefinitions;
|
|
||||||
const usernames = awaitedDiscoveredIdsDefinitions.count;
|
const usernames = awaitedDiscoveredIdsDefinitions.count;
|
||||||
|
|
||||||
manager.broadcastEval(
|
await manager.broadcastEval(
|
||||||
(client, { servers, users, channels, tanks, usernames }) => {
|
(client, { servers, users, channels, tanks, usernames }) => {
|
||||||
const bios = [
|
const bios = [
|
||||||
`Living in ${servers.toLocaleString()} servers`,
|
`Living in ${servers.toLocaleString()} servers`,
|
||||||
@@ -59,9 +96,15 @@ try {
|
|||||||
},
|
},
|
||||||
{ context: { servers, channels, users, tanks, usernames } },
|
{ context: { servers, channels, users, tanks, usernames } },
|
||||||
);
|
);
|
||||||
|
|
||||||
|
console.log('Bot startup complete!');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn(
|
console.error('Critical error during startup:', error);
|
||||||
'An error occurred when setting up presence, ignoring to keep the process alive',
|
throw error;
|
||||||
error,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setupShardManager().catch((error) => {
|
||||||
|
console.error('Failed to start the bot:', error);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
Reference in new issue
Block a user