blob: 3c2b5d93eba2802ef855f0401f52bb4cd51ee434 [file] [log] [blame]
TheCodedProf4a6d5712023-01-19 15:54:40 -05001import Discord, { 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 })
TheCodedProf4a6d5712023-01-19 15:54:40 -050061 .on("collect", (i: ButtonInteraction) => {
62 mod.stop();
Skyler Grey75ea9172022-08-06 10:22:23 +010063 resolve(i);
64 });
TheCodedProf4a6d5712023-01-19 15:54:40 -050065 const mod = new InteractionCollector(client as Client, {
Skyler Grey75ea9172022-08-06 10:22:23 +010066 filter: (i: Interaction) => modalFilter(i),
67 time: 300000
TheCodedProf4a6d5712023-01-19 15:54:40 -050068 }).on("collect", async (i: ModalSubmitInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010069 int.stop();
TheCodedProf4a6d5712023-01-19 15:54:40 -050070 await i.deferUpdate();
71 resolve(i);
Skyler Grey75ea9172022-08-06 10:22:23 +010072 });
pineafan63fc5e22022-08-04 22:04:10 +010073 });
Skyler Grey75ea9172022-08-06 10:22:23 +010074 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010075 console.log(e);
pineafan6702cef2022-06-13 17:52:37 +010076 return null;
77 }
78 return out;
Skyler Grey75ea9172022-08-06 10:22:23 +010079}