generated from mezotv/Discord-Bot-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: add express server * version bump
- Loading branch information
Showing
5 changed files
with
68 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import express, { Request, Response } from 'express'; | ||
import type { WebSocketShard, Guild, Shard } from 'discord.js'; | ||
import type WouldYou from '../util/wouldYou'; | ||
|
||
interface ShardStats { | ||
id: number; | ||
status: string; | ||
ping: number; | ||
guilds: number; | ||
members: number; | ||
} | ||
|
||
export default class ExpressServer { | ||
private client: WouldYou; | ||
private port: number; | ||
private app: express.Application; | ||
|
||
constructor(client: WouldYou, port: number = 3000) { | ||
this.client = client; | ||
this.port = process.env.PORT ? parseInt(process.env.PORT) : port; | ||
this.app = express(); | ||
this.initializeRoutes(); | ||
} | ||
|
||
private async getRequestStats(): Promise<ShardStats[]> { | ||
const results = await this.client?.cluster.broadcastEval((client) => { | ||
const statsPerShard = client.ws.shards.map((shard: WebSocketShard) => { | ||
return { | ||
id: shard.id, | ||
status: shard.status, | ||
ping: Math.floor(shard.ping), | ||
guilds: client.guilds.cache.filter((g: Guild) => g.shardId === shard.id).size, | ||
members: client.guilds.cache | ||
.filter((g: Guild) => g.shardId === shard.id) | ||
.reduce((a: number, b: Guild) => a + b.memberCount, 0), | ||
}; | ||
}); | ||
return statsPerShard; | ||
}); | ||
return results as unknown as ShardStats[]; | ||
} | ||
|
||
private initializeRoutes(): void { | ||
this.app.get('/api/status', async (req: Request, res: Response) => { | ||
if(req.headers.authorization !== process.env.AUTH) return res.status(401).json({ error: 'Unauthorized' }); | ||
try { | ||
const stats = await this.getRequestStats(); | ||
res.json(stats); | ||
} catch (error) { | ||
res.status(500).json({ error: 'Failed to fetch stats' }); | ||
} | ||
}); | ||
} | ||
|
||
public startServer(): void { | ||
this.app.listen(this.port); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters