blob: 64e87b8bf8907b5f73c90eced2d28a24b571a869 [file] [log] [blame]
TheCodedProf456a20f2023-01-19 15:55:34 -05001import { ButtonInteraction, Client, Interaction, InteractionCollector, Message, MessageComponentInteraction, ModalSubmitInteraction } from "discord.js";
pineafan63fc5e22022-08-04 22:04:10 +01002import client from "./client.js";
pineafan6702cef2022-06-13 17:52:37 +01003
Skyler Grey75ea9172022-08-06 10:22:23 +01004export default async function (
5 m: Message,
Skyler Grey11236ba2022-08-08 21:13:33 +01006 interactionFilter: (i: MessageComponentInteraction) => boolean | Promise<boolean>,
Skyler Grey75ea9172022-08-06 10:22:23 +01007 messageFilter: (m: Message) => boolean | Promise<boolean>
8) {
pineafan6702cef2022-06-13 17:52:37 +01009 let out;
10 try {
pineafan63fc5e22022-08-04 22:04:10 +010011 out = await new Promise((resolve, _reject) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010012 const mes = m
13 .createMessageComponentCollector({
14 filter: (m) => interactionFilter(m),
15 time: 300000
16 })
17 .on("collect", (m) => {
18 resolve(m);
19 });
20 const int = m.channel
21 .createMessageCollector({
22 filter: (m) => messageFilter(m),
23 time: 300000
24 })
25 .on("collect", (m) => {
26 try {
27 m.delete();
28 } catch (e) {
PineaFan100df682023-01-02 13:26:08 +000029 client.emit("error", e as Error);
Skyler Grey75ea9172022-08-06 10:22:23 +010030 }
31 resolve(m);
32 });
33 mes.on("end", () => {
34 int.stop();
35 });
36 int.on("end", () => {
37 mes.stop();
38 });
pineafan63fc5e22022-08-04 22:04:10 +010039 });
Skyler Grey75ea9172022-08-06 10:22:23 +010040 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010041 console.log(e);
pineafan6702cef2022-06-13 17:52:37 +010042 return null;
43 }
44
45 return out;
46}
47
Skyler Grey75ea9172022-08-06 10:22:23 +010048export async function modalInteractionCollector(
49 m: Message,
50 modalFilter: (i: Interaction) => boolean | Promise<boolean>,
Skyler Grey11236ba2022-08-08 21:13:33 +010051 interactionFilter: (i: MessageComponentInteraction) => boolean | Promise<boolean>
TheCodedProf4a6d5712023-01-19 15:54:40 -050052): Promise<null | ButtonInteraction | ModalSubmitInteraction> {
53 let out: ButtonInteraction | ModalSubmitInteraction;
pineafan6702cef2022-06-13 17:52:37 +010054 try {
pineafan63fc5e22022-08-04 22:04:10 +010055 out = await new Promise((resolve, _reject) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010056 const int = m
57 .createMessageComponentCollector({
Skyler Grey11236ba2022-08-08 21:13:33 +010058 filter: (i: MessageComponentInteraction) => interactionFilter(i),
pineafanc6158ab2022-06-17 16:34:07 +010059 time: 300000
pineafan6702cef2022-06-13 17:52:37 +010060 })
TheCodedProf9bc84752023-01-21 05:19:57 -050061 .on("collect", async (i: ButtonInteraction) => {
TheCodedProf4a6d5712023-01-19 15:54:40 -050062 mod.stop();
TheCodedProf9bc84752023-01-21 05:19:57 -050063 if (!i.deferred) await i.deferUpdate();
Skyler Grey75ea9172022-08-06 10:22:23 +010064 resolve(i);
65 });
TheCodedProf4a6d5712023-01-19 15:54:40 -050066 const mod = new InteractionCollector(client as Client, {
Skyler Grey75ea9172022-08-06 10:22:23 +010067 filter: (i: Interaction) => modalFilter(i),
68 time: 300000
TheCodedProf4a6d5712023-01-19 15:54:40 -050069 }).on("collect", async (i: ModalSubmitInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010070 int.stop();
TheCodedProf9bc84752023-01-21 05:19:57 -050071 if (!i.deferred) await i.deferUpdate();
TheCodedProf4a6d5712023-01-19 15:54:40 -050072 resolve(i);
Skyler Grey75ea9172022-08-06 10:22:23 +010073 });
pineafan63fc5e22022-08-04 22:04:10 +010074 });
Skyler Grey75ea9172022-08-06 10:22:23 +010075 } catch (e) {
pineafan6702cef2022-06-13 17:52:37 +010076 return null;
77 }
78 return out;
Skyler Grey75ea9172022-08-06 10:22:23 +010079}