add component support

This commit is contained in:
ITOH
2021-05-07 14:14:02 +02:00
parent 3fda6de728
commit b4d2398e6c
2 changed files with 16 additions and 13 deletions
@@ -3,6 +3,7 @@ import { cache } from "../../cache.ts";
import { rest } from "../../rest/rest.ts"; import { rest } from "../../rest/rest.ts";
import type { DiscordenoInteractionResponse } from "../../types/discordeno/interaction_response.ts"; import type { DiscordenoInteractionResponse } from "../../types/discordeno/interaction_response.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
import { validateComponents } from "../../util/utils.ts";
/** /**
* Send a response to a users slash command. The command data will have the id and token necessary to respond. * Send a response to a users slash command. The command data will have the id and token necessary to respond.
@@ -13,8 +14,10 @@ import { endpoints } from "../../util/constants.ts";
export async function sendInteractionResponse( export async function sendInteractionResponse(
id: bigint, id: bigint,
token: string, token: string,
options: DiscordenoInteractionResponse, options: DiscordenoInteractionResponse
) { ) {
// TODO: add more options validations
if (options.data?.components) validateComponents(options.data?.components);
// If its already been executed, we need to send a followup response // If its already been executed, we need to send a followup response
if (cache.executedSlashCommands.has(token)) { if (cache.executedSlashCommands.has(token)) {
return await rest.runMethod( return await rest.runMethod(
@@ -22,22 +25,19 @@ export async function sendInteractionResponse(
endpoints.WEBHOOK(applicationId, token), endpoints.WEBHOOK(applicationId, token),
{ {
...options, ...options,
}, }
); );
} }
// Expire in 15 minutes // Expire in 15 minutes
cache.executedSlashCommands.add(token); cache.executedSlashCommands.add(token);
setTimeout( setTimeout(() => {
() => { eventHandlers.debug?.(
eventHandlers.debug?.( "loop",
"loop", `Running setTimeout in send_interaction_response file.`
`Running setTimeout in send_interaction_response file.`, );
); cache.executedSlashCommands.delete(token);
cache.executedSlashCommands.delete(token); }, 900000);
},
900000,
);
// If the user wants this as a private message mark it ephemeral // If the user wants this as a private message mark it ephemeral
if (options.private) { if (options.private) {
@@ -52,6 +52,6 @@ export async function sendInteractionResponse(
return await rest.runMethod( return await rest.runMethod(
"post", "post",
endpoints.INTERACTION_ID_TOKEN(id, token), endpoints.INTERACTION_ID_TOKEN(id, token),
options, options
); );
} }
@@ -1,5 +1,6 @@
import { Embed } from "../../embeds/embed.ts"; import { Embed } from "../../embeds/embed.ts";
import { AllowedMentions } from "../../messages/allowed_mentions.ts"; import { AllowedMentions } from "../../messages/allowed_mentions.ts";
import { MessageComponents } from "../../messages/components/message_components.ts";
/** https://discord.com/developers/docs/interactions/slash-commands#interaction-response-interactionapplicationcommandcallbackdata */ /** https://discord.com/developers/docs/interactions/slash-commands#interaction-response-interactionapplicationcommandcallbackdata */
export interface InteractionApplicationCommandCallbackData { export interface InteractionApplicationCommandCallbackData {
@@ -13,4 +14,6 @@ export interface InteractionApplicationCommandCallbackData {
allowedMentions?: AllowedMentions; allowedMentions?: AllowedMentions;
/** Set to `64` to make your response ephemeral */ /** Set to `64` to make your response ephemeral */
flags?: number; flags?: number;
/** The components you would like to have sent in this message */
components?: MessageComponents;
} }