mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-06-17 01:59:08 +00:00
feat: add discord interactions worker
This commit is contained in:
parent
dde05c402e
commit
9354047447
36 changed files with 486 additions and 178 deletions
5
packages/interactions/handlers/healthz.ts
Normal file
5
packages/interactions/handlers/healthz.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
import { respond } from '@roleypoly/worker-utils';
|
||||
|
||||
export const healthz = async (request: Request): Promise<Response> => {
|
||||
return respond({ ok: true });
|
||||
};
|
49
packages/interactions/handlers/interaction.ts
Normal file
49
packages/interactions/handlers/interaction.ts
Normal file
|
@ -0,0 +1,49 @@
|
|||
import { helloWorld } from '@roleypoly/interactions/handlers/interactions/hello-world';
|
||||
import { verifyRequest } from '@roleypoly/interactions/utils/interactions';
|
||||
import {
|
||||
InteractionData,
|
||||
InteractionRequest,
|
||||
InteractionRequestCommand,
|
||||
InteractionResponse,
|
||||
InteractionType,
|
||||
} from '@roleypoly/types';
|
||||
import { respond } from '@roleypoly/worker-utils';
|
||||
|
||||
const commands: Record<
|
||||
InteractionData['name'],
|
||||
(request: InteractionRequestCommand) => Promise<InteractionResponse>
|
||||
> = {
|
||||
'hello-world': helloWorld,
|
||||
};
|
||||
|
||||
export const interactionHandler = async (request: Request): Promise<Response> => {
|
||||
if (!(await verifyRequest(request))) {
|
||||
return new Response('invalid request signature', { status: 401 });
|
||||
}
|
||||
|
||||
const interaction = (await request.json()) as InteractionRequest;
|
||||
|
||||
if (interaction.type === InteractionType.PING) {
|
||||
return respond({ type: 1 });
|
||||
}
|
||||
|
||||
if (interaction.type !== InteractionType.APPLICATION_COMMAND) {
|
||||
return respond({ err: 'not implemented' }, { status: 400 });
|
||||
}
|
||||
|
||||
if (!interaction.data) {
|
||||
return respond({ err: 'data missing' }, { status: 400 });
|
||||
}
|
||||
|
||||
const handler = commands[interaction.data.name];
|
||||
if (!handler) {
|
||||
return respond({ err: 'not implemented' }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await handler(interaction as InteractionRequestCommand);
|
||||
return respond(response);
|
||||
} catch (e) {
|
||||
return respond({ err: 'command errored' }, { status: 500 });
|
||||
}
|
||||
};
|
16
packages/interactions/handlers/interactions/hello-world.ts
Normal file
16
packages/interactions/handlers/interactions/hello-world.ts
Normal file
|
@ -0,0 +1,16 @@
|
|||
import {
|
||||
InteractionCallbackType,
|
||||
InteractionRequestCommand,
|
||||
InteractionResponse,
|
||||
} from '@roleypoly/types';
|
||||
|
||||
export const helloWorld = async (
|
||||
interaction: InteractionRequestCommand
|
||||
): Promise<InteractionResponse> => {
|
||||
return {
|
||||
type: InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||
data: {
|
||||
content: `Hey there, ${interaction.member?.nick || interaction.user?.username}`,
|
||||
},
|
||||
};
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue