blob: bf9b0c003004359dfa8098c9fd8a125b7910b8e0 [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) => {
35 try {
36 m.delete();
37 } catch (e) {
PineaFan100df682023-01-02 13:26:08 +000038 client.emit("error", e as Error);
Skyler Grey75ea9172022-08-06 10:22:23 +010039 }
40 resolve(m);
41 });
42 mes.on("end", () => {
43 int.stop();
44 });
45 int.on("end", () => {
46 mes.stop();
47 });
pineafan63fc5e22022-08-04 22:04:10 +010048 });
Skyler Grey75ea9172022-08-06 10:22:23 +010049 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010050 console.log(e);
pineafan6702cef2022-06-13 17:52:37 +010051 return null;
52 }
53
54 return out;
55}
56
TheCodedProf01cba762023-02-18 15:55:05 -050057function defaultInteractionFilter(i: MessageComponentInteraction, user: User, m: Message) {
Skyler Greyda16adf2023-03-05 10:22:12 +000058 return i.channel!.id === m.channel!.id && i.user.id === user.id;
TheCodedProf01cba762023-02-18 15:55:05 -050059}
60function defaultModalFilter(i: ModalSubmitInteraction, user: User, m: Message) {
Skyler Greyda16adf2023-03-05 10:22:12 +000061 return i.channel!.id === m.channel!.id && i.user.id === user.id;
TheCodedProf01cba762023-02-18 15:55:05 -050062}
63
Skyler Grey75ea9172022-08-06 10:22:23 +010064export async function modalInteractionCollector(
Skyler Greyda16adf2023-03-05 10:22:12 +000065 m: Message,
66 user: User,
TheCodedProf01cba762023-02-18 15:55:05 -050067 modalFilter?: (i: Interaction) => boolean | Promise<boolean>,
68 interactionFilter?: (i: MessageComponentInteraction) => boolean | Promise<boolean>
TheCodedProf4a6d5712023-01-19 15:54:40 -050069): Promise<null | ButtonInteraction | ModalSubmitInteraction> {
70 let out: ButtonInteraction | ModalSubmitInteraction;
pineafan6702cef2022-06-13 17:52:37 +010071 try {
pineafan63fc5e22022-08-04 22:04:10 +010072 out = await new Promise((resolve, _reject) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010073 const int = m
74 .createMessageComponentCollector({
Skyler Greyda16adf2023-03-05 10:22:12 +000075 filter: (i: MessageComponentInteraction) =>
76 (interactionFilter ? interactionFilter(i) : true) && defaultInteractionFilter(i, user, m),
pineafanc6158ab2022-06-17 16:34:07 +010077 time: 300000
pineafan6702cef2022-06-13 17:52:37 +010078 })
TheCodedProf9bc84752023-01-21 05:19:57 -050079 .on("collect", async (i: ButtonInteraction) => {
TheCodedProf4a6d5712023-01-19 15:54:40 -050080 mod.stop();
TheCodedProf267563a2023-01-21 17:00:57 -050081 int.stop();
82 await i.deferUpdate();
Skyler Grey75ea9172022-08-06 10:22:23 +010083 resolve(i);
84 });
TheCodedProf4a6d5712023-01-19 15:54:40 -050085 const mod = new InteractionCollector(client as Client, {
Skyler Greyda16adf2023-03-05 10:22:12 +000086 filter: (i: Interaction) =>
87 (modalFilter ? modalFilter(i) : true) && i.isModalSubmit() && defaultModalFilter(i, user, m),
Skyler Grey75ea9172022-08-06 10:22:23 +010088 time: 300000
TheCodedProf4a6d5712023-01-19 15:54:40 -050089 }).on("collect", async (i: ModalSubmitInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010090 int.stop();
TheCodedProf267563a2023-01-21 17:00:57 -050091 mod.stop();
92 await i.deferUpdate();
TheCodedProf4a6d5712023-01-19 15:54:40 -050093 resolve(i);
Skyler Grey75ea9172022-08-06 10:22:23 +010094 });
pineafan63fc5e22022-08-04 22:04:10 +010095 });
Skyler Grey75ea9172022-08-06 10:22:23 +010096 } catch (e) {
TheCodedProf01cba762023-02-18 15:55:05 -050097 console.log(e);
pineafan6702cef2022-06-13 17:52:37 +010098 return null;
99 }
100 return out;
Skyler Grey75ea9172022-08-06 10:22:23 +0100101}