mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-06-16 09:39:09 +00:00
fix(interactions): add async responses
This commit is contained in:
parent
3601d435b2
commit
26bc74bcbc
11 changed files with 220 additions and 149 deletions
|
@ -1,5 +1,12 @@
|
|||
import { publicKey } from '@roleypoly/interactions/utils/config';
|
||||
import { InteractionRequest } from '@roleypoly/types';
|
||||
import {
|
||||
InteractionCallbackType,
|
||||
InteractionFlags,
|
||||
InteractionRequest,
|
||||
InteractionRequestCommand,
|
||||
InteractionResponse,
|
||||
} from '@roleypoly/types';
|
||||
import { AuthType, discordFetch, HandlerTools } from '@roleypoly/worker-utils';
|
||||
import nacl from 'tweetnacl';
|
||||
|
||||
export const verifyRequest = (
|
||||
|
@ -25,3 +32,47 @@ export const verifyRequest = (
|
|||
|
||||
return true;
|
||||
};
|
||||
|
||||
export type RequestInfo = HandlerTools & { request: Request };
|
||||
|
||||
export type CommandHandler = (
|
||||
request: InteractionRequestCommand,
|
||||
requestInfo: RequestInfo
|
||||
) => Promise<InteractionResponse>;
|
||||
|
||||
export const asyncResponse =
|
||||
(handler: CommandHandler): CommandHandler =>
|
||||
async (
|
||||
command: InteractionRequestCommand,
|
||||
requestInfo: RequestInfo
|
||||
): Promise<InteractionResponse> => {
|
||||
requestInfo.waitUntil(
|
||||
(async () => {
|
||||
const response = await handler(command, requestInfo);
|
||||
await updateOriginalMessage(command.application_id, command.token, response);
|
||||
})()
|
||||
);
|
||||
|
||||
return {
|
||||
type: InteractionCallbackType.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE,
|
||||
data: {
|
||||
flags: InteractionFlags.EPHEMERAL,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const updateOriginalMessage = async (
|
||||
appID: string,
|
||||
token: string,
|
||||
response: InteractionResponse
|
||||
) => {
|
||||
const url = `/webhooks/${appID}/${token}/messages/@original`;
|
||||
|
||||
return await discordFetch(url, '', AuthType.None, {
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify(response.data),
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue