Skip to content

Commit

Permalink
Add HTTP interaction handler
Browse files Browse the repository at this point in the history
  • Loading branch information
D4isDAVID committed Sep 9, 2024
1 parent 16fa99f commit d5a4162
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 3 deletions.
3 changes: 3 additions & 0 deletions discordbot.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
# - copy this file elsewhere, modify, and execute it from another cfg file;
# - copy and modify the individual convars you wish to configure;
# - or modify this file, and execute it directly from another cfg file via the command `exec @discordbot/discordbot.cfg`.

# The bot token to use for Discord API authentication.
set discordbot:botToken "YOUR_BOT_TOKEN_HERE"
248 changes: 247 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
"typecheck": "tsc -p server",
"build": "npm run typecheck && rimraf dist && node build.js"
},
"dependencies": {
"@discordjs/core": "1.0.0",
"@discordjs/rest": "2.0.0",
"tweetnacl": "^1.0.3"
},
"devDependencies": {
"@citizenfx/server": "^2.0.9780-1",
"@types/node": "16.9.1",
Expand Down
39 changes: 39 additions & 0 deletions server/http/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {
APIInteraction,
APIInteractionResponse,
InteractionResponseType,
InteractionType,
} from '@discordjs/core/http-only';
import { HttpHandlerRequest, HttpHandlerResponse } from './types.js';
import { verifyDiscordRequest } from './verify.js';

async function handleInteraction(
interaction: APIInteraction,
): Promise<APIInteractionResponse | void> {
switch (interaction.type) {
case InteractionType.Ping:
return { type: InteractionResponseType.Pong };
}
}

export function setupHttpHandler(verifyKey: string) {
const verify = verifyDiscordRequest(verifyKey);

async function handler(
request: HttpHandlerRequest,
response: HttpHandlerResponse,
) {
request.setDataHandler(async (body) => {
if (!verify(request, body)) {
response.writeHead(401);
return response.send('invalid request signature');
}

const interaction = JSON.parse(body) as APIInteraction;
const result = await handleInteraction(interaction);
result && response.send(JSON.stringify(result));
});
}

SetHttpHandler(handler);
}
18 changes: 18 additions & 0 deletions server/http/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export interface HttpHandlerRequest {
address: string;
headers: Record<string, string>;
method: string;
path: string;
setDataHandler(handler: (data: string) => void): void;
setDataHandler(
handler: (data: ArrayBuffer) => void,
binary: 'binary',
): void;
setCancelHandler(handler: () => void): void;
}

export interface HttpHandlerResponse {
writeHead(code: number, headers?: Record<string, string | string[]>): void;
write(data: string): void;
send(data?: string): void;
}
Loading

0 comments on commit d5a4162

Please sign in to comment.