blob: 072f73fdd36252230b39eca5edce0921e5c9e29b [file] [log] [blame]
TheCodedProf01cba762023-02-18 15:55:05 -05001import { ButtonInteraction, Client, User, 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
TheCodedProf01cba762023-02-18 15:55:05 -050048function defaultInteractionFilter(i: MessageComponentInteraction, user: User, m: Message) {
49 return i.channel!.id === m.channel!.id && i.user.id === user.id
50}
51function defaultModalFilter(i: ModalSubmitInteraction, user: User, m: Message) {
52 return i.channel!.id === m.channel!.id && i.user.id === user.id
53}
54
55
Skyler Grey75ea9172022-08-06 10:22:23 +010056export async function modalInteractionCollector(
TheCodedProf01cba762023-02-18 15:55:05 -050057 m: Message, user: User,
58 modalFilter?: (i: Interaction) => boolean | Promise<boolean>,
59 interactionFilter?: (i: MessageComponentInteraction) => boolean | Promise<boolean>
TheCodedProf4a6d5712023-01-19 15:54:40 -050060): Promise<null | ButtonInteraction | ModalSubmitInteraction> {
61 let out: ButtonInteraction | ModalSubmitInteraction;
pineafan6702cef2022-06-13 17:52:37 +010062 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({
TheCodedProf01cba762023-02-18 15:55:05 -050066 filter: (i: MessageComponentInteraction) => (interactionFilter ? interactionFilter(i) : true) && defaultInteractionFilter(i, user, m),
pineafanc6158ab2022-06-17 16:34:07 +010067 time: 300000
pineafan6702cef2022-06-13 17:52:37 +010068 })
TheCodedProf9bc84752023-01-21 05:19:57 -050069 .on("collect", async (i: ButtonInteraction) => {
TheCodedProf4a6d5712023-01-19 15:54:40 -050070 mod.stop();
TheCodedProf267563a2023-01-21 17:00:57 -050071 int.stop();
72 await i.deferUpdate();
Skyler Grey75ea9172022-08-06 10:22:23 +010073 resolve(i);
74 });
TheCodedProf4a6d5712023-01-19 15:54:40 -050075 const mod = new InteractionCollector(client as Client, {
TheCodedProf01cba762023-02-18 15:55:05 -050076 filter: (i: Interaction) => (modalFilter ? modalFilter(i) : true) && i.isModalSubmit() && defaultModalFilter(i, user, m),
Skyler Grey75ea9172022-08-06 10:22:23 +010077 time: 300000
TheCodedProf4a6d5712023-01-19 15:54:40 -050078 }).on("collect", async (i: ModalSubmitInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010079 int.stop();
TheCodedProf267563a2023-01-21 17:00:57 -050080 mod.stop();
81 await i.deferUpdate();
TheCodedProf4a6d5712023-01-19 15:54:40 -050082 resolve(i);
Skyler Grey75ea9172022-08-06 10:22:23 +010083 });
pineafan63fc5e22022-08-04 22:04:10 +010084 });
Skyler Grey75ea9172022-08-06 10:22:23 +010085 } catch (e) {
TheCodedProf01cba762023-02-18 15:55:05 -050086 console.log(e);
pineafan6702cef2022-06-13 17:52:37 +010087 return null;
88 }
89 return out;
Skyler Grey75ea9172022-08-06 10:22:23 +010090}