blob: 8c6e55c285ed2c9fa330320ddebed50284a0b94a [file] [log] [blame]
Skyler Grey75ea9172022-08-06 10:22:23 +01001import Discord, {
2 Interaction,
3 Message,
4 MessageComponentInteraction
5} from "discord.js";
pineafan63fc5e22022-08-04 22:04:10 +01006import client from "./client.js";
pineafan6702cef2022-06-13 17:52:37 +01007
Skyler Grey75ea9172022-08-06 10:22:23 +01008export default async function (
9 m: Message,
10 interactionFilter: (
11 i: MessageComponentInteraction
12 ) => boolean | Promise<boolean>,
13 messageFilter: (m: Message) => boolean | Promise<boolean>
14) {
pineafan6702cef2022-06-13 17:52:37 +010015 let out;
16 try {
pineafan63fc5e22022-08-04 22:04:10 +010017 out = await new Promise((resolve, _reject) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010018 const mes = m
19 .createMessageComponentCollector({
20 filter: (m) => interactionFilter(m),
21 time: 300000
22 })
23 .on("collect", (m) => {
24 resolve(m);
25 });
26 const int = m.channel
27 .createMessageCollector({
28 filter: (m) => messageFilter(m),
29 time: 300000
30 })
31 .on("collect", (m) => {
32 try {
33 m.delete();
34 } catch (e) {
35 client._error(e);
36 }
37 resolve(m);
38 });
39 mes.on("end", () => {
40 int.stop();
41 });
42 int.on("end", () => {
43 mes.stop();
44 });
pineafan63fc5e22022-08-04 22:04:10 +010045 });
Skyler Grey75ea9172022-08-06 10:22:23 +010046 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010047 console.log(e);
pineafan6702cef2022-06-13 17:52:37 +010048 return null;
49 }
50
51 return out;
52}
53
Skyler Grey75ea9172022-08-06 10:22:23 +010054export async function modalInteractionCollector(
55 m: Message,
56 modalFilter: (i: Interaction) => boolean | Promise<boolean>,
57 interactionFilter: (
58 i: MessageComponentInteraction
59 ) => boolean | Promise<boolean>
60) {
pineafan6702cef2022-06-13 17:52:37 +010061 let out;
62 try {
pineafan63fc5e22022-08-04 22:04:10 +010063 out = await new Promise((resolve, _reject) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010064 const int = m
65 .createMessageComponentCollector({
66 filter: (i: MessageComponentInteraction) =>
67 interactionFilter(i),
pineafanc6158ab2022-06-17 16:34:07 +010068 time: 300000
pineafan6702cef2022-06-13 17:52:37 +010069 })
Skyler Grey75ea9172022-08-06 10:22:23 +010070 .on("collect", (i: Interaction) => {
71 resolve(i);
72 });
73 const mod = new Discord.InteractionCollector(client, {
74 filter: (i: Interaction) => modalFilter(i),
75 time: 300000
76 }).on("collect", async (i: Interaction) => {
77 int.stop();
78 (i as Discord.ModalSubmitInteraction).deferUpdate();
79 resolve(i as Discord.ModalSubmitInteraction);
80 });
81 int.on("end", () => {
82 mod.stop();
83 });
84 mod.on("end", () => {
85 int.stop();
86 });
pineafan63fc5e22022-08-04 22:04:10 +010087 });
Skyler Grey75ea9172022-08-06 10:22:23 +010088 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010089 console.log(e);
pineafan6702cef2022-06-13 17:52:37 +010090 return null;
91 }
92 return out;
Skyler Grey75ea9172022-08-06 10:22:23 +010093}