-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
355 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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); | ||
} |
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,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; | ||
} |
Oops, something went wrong.