blob: 0b05779104a2ec090a3502170d9a20e741169c22 [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
pineafan96228bd2023-02-21 14:22:55 +000013 .createMessageComponentCollector({
14 filter: (m) => interactionFilter(m),
15 time: 300000
16 })
17 .on("collect", (m) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010018 resolve(m);
19 });
pineafan96228bd2023-02-21 14:22:55 +000020 const int = m.channel.createMessageCollector({
Skyler Grey75ea9172022-08-06 10:22:23 +010021 filter: (m) => messageFilter(m),
22 time: 300000
23 })
24 .on("collect", (m) => {
25 try {
26 m.delete();
27 } catch (e) {
PineaFan100df682023-01-02 13:26:08 +000028 client.emit("error", e as Error);
Skyler Grey75ea9172022-08-06 10:22:23 +010029 }
30 resolve(m);
31 });
32 mes.on("end", () => {
33 int.stop();
34 });
35 int.on("end", () => {
36 mes.stop();
37 });
pineafan63fc5e22022-08-04 22:04:10 +010038 });
Skyler Grey75ea9172022-08-06 10:22:23 +010039 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010040 console.log(e);
pineafan6702cef2022-06-13 17:52:37 +010041 return null;
42 }
43
44 return out;
45}
46
TheCodedProf01cba762023-02-18 15:55:05 -050047function defaultInteractionFilter(i: MessageComponentInteraction, user: User, m: Message) {
48 return i.channel!.id === m.channel!.id && i.user.id === user.id
49}
50function defaultModalFilter(i: ModalSubmitInteraction, user: User, m: Message) {
51 return i.channel!.id === m.channel!.id && i.user.id === user.id
52}
53
54
Skyler Grey75ea9172022-08-06 10:22:23 +010055export async function modalInteractionCollector(
TheCodedProf01cba762023-02-18 15:55:05 -050056 m: Message, user: User,
57 modalFilter?: (i: Interaction) => boolean | Promise<boolean>,
58 interactionFilter?: (i: MessageComponentInteraction) => boolean | Promise<boolean>
TheCodedProf4a6d5712023-01-19 15:54:40 -050059): Promise<null | ButtonInteraction | ModalSubmitInteraction> {
60 let out: ButtonInteraction | ModalSubmitInteraction;
pineafan6702cef2022-06-13 17:52:37 +010061 try {
pineafan63fc5e22022-08-04 22:04:10 +010062 out = await new Promise((resolve, _reject) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010063 const int = m
64 .createMessageComponentCollector({
TheCodedProf01cba762023-02-18 15:55:05 -050065 filter: (i: MessageComponentInteraction) => (interactionFilter ? interactionFilter(i) : true) && defaultInteractionFilter(i, user, m),
pineafanc6158ab2022-06-17 16:34:07 +010066 time: 300000
pineafan6702cef2022-06-13 17:52:37 +010067 })
TheCodedProf9bc84752023-01-21 05:19:57 -050068 .on("collect", async (i: ButtonInteraction) => {
TheCodedProf4a6d5712023-01-19 15:54:40 -050069 mod.stop();
TheCodedProf267563a2023-01-21 17:00:57 -050070 int.stop();
71 await i.deferUpdate();
Skyler Grey75ea9172022-08-06 10:22:23 +010072 resolve(i);
73 });
TheCodedProf4a6d5712023-01-19 15:54:40 -050074 const mod = new InteractionCollector(client as Client, {
TheCodedProf01cba762023-02-18 15:55:05 -050075 filter: (i: Interaction) => (modalFilter ? modalFilter(i) : true) && i.isModalSubmit() && defaultModalFilter(i, user, m),
Skyler Grey75ea9172022-08-06 10:22:23 +010076 time: 300000
TheCodedProf4a6d5712023-01-19 15:54:40 -050077 }).on("collect", async (i: ModalSubmitInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010078 int.stop();
TheCodedProf267563a2023-01-21 17:00:57 -050079 mod.stop();
80 await i.deferUpdate();
TheCodedProf4a6d5712023-01-19 15:54:40 -050081 resolve(i);
Skyler Grey75ea9172022-08-06 10:22:23 +010082 });
pineafan63fc5e22022-08-04 22:04:10 +010083 });
Skyler Grey75ea9172022-08-06 10:22:23 +010084 } catch (e) {
TheCodedProf01cba762023-02-18 15:55:05 -050085 console.log(e);
pineafan6702cef2022-06-13 17:52:37 +010086 return null;
87 }
88 return out;
Skyler Grey75ea9172022-08-06 10:22:23 +010089}