blob: b9f2c2389f1dea1434cf075e954039237911eb91 [file] [log] [blame]
Skyler Greyda16adf2023-03-05 10:22:12 +00001import {
2 ButtonInteraction,
3 Client,
4 User,
5 Interaction,
6 InteractionCollector,
7 Message,
8 MessageComponentInteraction,
9 ModalSubmitInteraction
10} from "discord.js";
pineafan63fc5e22022-08-04 22:04:10 +010011import client from "./client.js";
pineafan6702cef2022-06-13 17:52:37 +010012
Skyler Grey75ea9172022-08-06 10:22:23 +010013export default async function (
14 m: Message,
Skyler Grey11236ba2022-08-08 21:13:33 +010015 interactionFilter: (i: MessageComponentInteraction) => boolean | Promise<boolean>,
Skyler Grey75ea9172022-08-06 10:22:23 +010016 messageFilter: (m: Message) => boolean | Promise<boolean>
17) {
pineafan6702cef2022-06-13 17:52:37 +010018 let out;
19 try {
pineafan63fc5e22022-08-04 22:04:10 +010020 out = await new Promise((resolve, _reject) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010021 const mes = m
Skyler Greyda16adf2023-03-05 10:22:12 +000022 .createMessageComponentCollector({
23 filter: (m) => interactionFilter(m),
24 time: 300000
25 })
26 .on("collect", (m) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010027 resolve(m);
28 });
Skyler Greyda16adf2023-03-05 10:22:12 +000029 const int = m.channel
30 .createMessageCollector({
Skyler Grey75ea9172022-08-06 10:22:23 +010031 filter: (m) => messageFilter(m),
32 time: 300000
33 })
34 .on("collect", (m) => {
Skyler Greyf4f21c42023-03-08 14:36:29 +000035 void m.delete().catch();
Skyler Grey75ea9172022-08-06 10:22:23 +010036 resolve(m);
37 });
38 mes.on("end", () => {
39 int.stop();
40 });
41 int.on("end", () => {
42 mes.stop();
43 });
pineafan63fc5e22022-08-04 22:04:10 +010044 });
Skyler Grey75ea9172022-08-06 10:22:23 +010045 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010046 console.log(e);
pineafan6702cef2022-06-13 17:52:37 +010047 return null;
48 }
49
50 return out;
51}
52
TheCodedProf01cba762023-02-18 15:55:05 -050053function defaultInteractionFilter(i: MessageComponentInteraction, user: User, m: Message) {
Skyler Greyda16adf2023-03-05 10:22:12 +000054 return i.channel!.id === m.channel!.id && i.user.id === user.id;
TheCodedProf01cba762023-02-18 15:55:05 -050055}
56function defaultModalFilter(i: ModalSubmitInteraction, user: User, m: Message) {
pineafan72659cc2023-05-28 13:36:44 +010057 return (i.channel ? i.channel!.id === m.channel!.id : true) && i.user.id === user.id;
TheCodedProf01cba762023-02-18 15:55:05 -050058}
59
Skyler Grey75ea9172022-08-06 10:22:23 +010060export async function modalInteractionCollector(
Skyler Greyda16adf2023-03-05 10:22:12 +000061 m: Message,
62 user: User,
TheCodedProf01cba762023-02-18 15:55:05 -050063 modalFilter?: (i: Interaction) => boolean | Promise<boolean>,
64 interactionFilter?: (i: MessageComponentInteraction) => boolean | Promise<boolean>
TheCodedProf4a6d5712023-01-19 15:54:40 -050065): Promise<null | ButtonInteraction | ModalSubmitInteraction> {
66 let out: ButtonInteraction | ModalSubmitInteraction;
pineafan6702cef2022-06-13 17:52:37 +010067 try {
pineafan63fc5e22022-08-04 22:04:10 +010068 out = await new Promise((resolve, _reject) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010069 const int = m
70 .createMessageComponentCollector({
Skyler Greyda16adf2023-03-05 10:22:12 +000071 filter: (i: MessageComponentInteraction) =>
72 (interactionFilter ? interactionFilter(i) : true) && defaultInteractionFilter(i, user, m),
pineafanc6158ab2022-06-17 16:34:07 +010073 time: 300000
pineafan6702cef2022-06-13 17:52:37 +010074 })
TheCodedProf9bc84752023-01-21 05:19:57 -050075 .on("collect", async (i: ButtonInteraction) => {
TheCodedProf4a6d5712023-01-19 15:54:40 -050076 mod.stop();
TheCodedProf267563a2023-01-21 17:00:57 -050077 int.stop();
78 await i.deferUpdate();
Skyler Grey75ea9172022-08-06 10:22:23 +010079 resolve(i);
80 });
TheCodedProf4a6d5712023-01-19 15:54:40 -050081 const mod = new InteractionCollector(client as Client, {
Skyler Greyda16adf2023-03-05 10:22:12 +000082 filter: (i: Interaction) =>
83 (modalFilter ? modalFilter(i) : true) && i.isModalSubmit() && defaultModalFilter(i, user, m),
Skyler Grey75ea9172022-08-06 10:22:23 +010084 time: 300000
TheCodedProf4a6d5712023-01-19 15:54:40 -050085 }).on("collect", async (i: ModalSubmitInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010086 int.stop();
TheCodedProf267563a2023-01-21 17:00:57 -050087 mod.stop();
88 await i.deferUpdate();
TheCodedProf4a6d5712023-01-19 15:54:40 -050089 resolve(i);
Skyler Grey75ea9172022-08-06 10:22:23 +010090 });
pineafan63fc5e22022-08-04 22:04:10 +010091 });
Skyler Grey75ea9172022-08-06 10:22:23 +010092 } catch (e) {
TheCodedProf01cba762023-02-18 15:55:05 -050093 console.log(e);
pineafan6702cef2022-06-13 17:52:37 +010094 return null;
95 }
96 return out;
Skyler Grey75ea9172022-08-06 10:22:23 +010097}