Modal handler :D

This commit is contained in:
cirroskais 2024-03-05 23:07:54 -05:00
parent 23cf68ac38
commit c9ff3cc83e
No known key found for this signature in database
GPG key ID: 5FC73EBF2678E33D
3 changed files with 80 additions and 24 deletions

View file

@ -1,7 +1,8 @@
import type { Command } from "../lib/types/command";
import { MessageComponentTypes, TextStyles } from "@discordeno/bot";
import { MessageComponentTypes, TextStyles, type Interaction } from "@discordeno/bot";
import SlashCommand from "../lib/classes/SlashCommand";
import Modal from "../lib/classes/Modal";
import { collectModal } from "../lib/handlers/ModalHandler";
export default class extends SlashCommand implements Command {
static name = "ping";
@ -13,10 +14,7 @@ export default class extends SlashCommand implements Command {
}
async run() {
return new Modal()
.setTitle("dope")
.setId("dope")
.setComponents([
const modal = new Modal().setTitle("dope").setComponents([
{
type: MessageComponentTypes.ActionRow,
components: [
@ -29,5 +27,16 @@ export default class extends SlashCommand implements Command {
],
},
]);
collectModal(modal)
.then(({ interaction, values }) => {
if (!interaction.data?.components) return;
interaction.respond("dope: " + values.get("dope"));
})
.catch((_) => {
//whatever bro dont respond to my modal 🙄
});
return modal;
}
}

View file

@ -3,6 +3,7 @@ import { readdir } from "node:fs/promises";
import REST from "./RESTHandler";
import * as CommandHandler from "./CommandHandler";
import * as ModalHandler from "./ModalHandler";
export const interactions = new Map();
@ -39,12 +40,6 @@ export function handle(interaction: Interaction) {
},
});
} else if (type == InteractionTypes.ModalSubmit) {
REST.sendInteractionResponse(interaction.id, interaction.token, {
type: InteractionResponseTypes.ChannelMessageWithSource,
data: {
content: "Not Implemented",
flags: 64,
},
});
ModalHandler.handle(interaction);
}
}

View file

@ -0,0 +1,52 @@
import { randomUUID } from "node:crypto";
import Modal from "../classes/Modal";
import { InteractionResponseTypes, type Interaction, type Component } from "@discordeno/bot";
import REST from "./RESTHandler";
const modals = new Map();
type ModalResponse = {
interaction: Interaction;
values: Map<string, string>;
};
function getModalValues(interaction: Interaction): Map<string, string> {
const values = new Map();
if (!interaction.data?.components) return values;
const actionRow: Component = interaction.data?.components[0];
if (actionRow.components?.length) {
actionRow.components.forEach((value, index) => {
values.set(value.customId, value.value);
});
}
return values;
}
export function collectModal(modal: Modal): Promise<ModalResponse> {
return new Promise((resolve, reject) => {
const id = randomUUID();
modal.setId(id);
modals.set(id, resolve);
setTimeout(() => reject(new Error("Modal timeout")), 1000 * 30); //, 1000 * 60 * 30);
});
}
export function handle(interaction: Interaction) {
if (!interaction.data) return;
const { customId } = interaction.data;
const modal = modals.get(customId);
if (!modal)
return REST.sendInteractionResponse(interaction.id, interaction.token, {
type: InteractionResponseTypes.ChannelMessageWithSource,
data: {
content: "Internal Error: MODAL_NOT_FOUND",
flags: 64,
},
});
return modal({ interaction, values: getModalValues(interaction) });
}