Skip to content

Commit

Permalink
fix: add express server (#631)
Browse files Browse the repository at this point in the history
* fix: add express server

* version bump
  • Loading branch information
mezotv authored Sep 7, 2024
1 parent d0f5c4f commit b51b12f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ services:
context: .
dockerfile: Dockerfile
restart: unless-stopped
ports:
- "4959"
stdin_open: true
tty: true
env_file:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "would-you",
"version": "2.0.2",
"version": "2.0.3",
"description": "Would You is a popular Discord bot that allows you to play the classic game of Would You Rather with your friends!",
"main": "dist/cluster.js",
"scripts": {
Expand Down
3 changes: 3 additions & 0 deletions src/events/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const event: Event = {
event: "ready",
execute: async (client: WouldYou) => {
if (client.cluster.id === 0) {

client.server.startServer();

let globalCommands = Array.from(
client.commands.filter((x) => x.requireGuild === true).values(),
).map((x) => x.data.toJSON()) as RESTPostAPIApplicationCommandsJSONBody[];
Expand Down
58 changes: 58 additions & 0 deletions src/util/expressServer.ts
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);
}
}
6 changes: 4 additions & 2 deletions src/util/wouldYou.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import PremiumHandler from "./premiumHandler";
import TranslationHandler from "./translationHandler";
import Voting from "./votingHandler";
import WebhookHandler from "./webhookHandler";
import ExpressServer from "./expressServer";

export default class WouldYou extends Client {
public commands: Collection<string, ChatInputCommand>;
Expand All @@ -44,6 +45,7 @@ export default class WouldYou extends Client {
public dailyMessage: DailyMessage;
public voting: Voting;
public config: IConfig;
public server: ExpressServer;
translate: any;

constructor() {
Expand Down Expand Up @@ -101,8 +103,6 @@ export default class WouldYou extends Client {
// Init the cluster client
this.cluster = new ClusterClient(this);

this.config = Config;

this.cluster.on("ready", async () => {
// The database handler
this.database = new DatabaseHandler(process.env.MONGO_URI as string);
Expand All @@ -115,6 +115,8 @@ export default class WouldYou extends Client {
});
this.database.startSweeper(this);

this.server = new ExpressServer(this, parseInt(process.env.PORT!));

// The translations handler
this.translation = new TranslationHandler();

Expand Down

0 comments on commit b51b12f

Please sign in to comment.