blob: abc6bb331af29de3c0f1addf2cb63c55bd5069c6 [file] [log] [blame]
Skyler Grey11236ba2022-08-08 21:13:33 +01001import Discord, { Interaction, Message, MessageComponentInteraction } 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) {
29 client._error(e);
30 }
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>
52): Promise<null | Interaction> {
53 let out: Interaction;
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 })
Skyler Grey75ea9172022-08-06 10:22:23 +010061 .on("collect", (i: Interaction) => {
62 resolve(i);
63 });
64 const mod = new Discord.InteractionCollector(client, {
65 filter: (i: Interaction) => modalFilter(i),
66 time: 300000
67 }).on("collect", async (i: Interaction) => {
68 int.stop();
69 (i as Discord.ModalSubmitInteraction).deferUpdate();
70 resolve(i as Discord.ModalSubmitInteraction);
71 });
72 int.on("end", () => {
73 mod.stop();
74 });
75 mod.on("end", () => {
76 int.stop();
77 });
pineafan63fc5e22022-08-04 22:04:10 +010078 });
Skyler Grey75ea9172022-08-06 10:22:23 +010079 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010080 console.log(e);
pineafan6702cef2022-06-13 17:52:37 +010081 return null;
82 }
83 return out;
Skyler Grey75ea9172022-08-06 10:22:23 +010084}