blob: 0b05779104a2ec090a3502170d9a20e741169c22 [file] [log] [blame]
Samuel Shuert27bf3cd2023-03-03 15:51:25 -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
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050013 .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 });
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050020 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
Samuel Shuert27bf3cd2023-03-03 15:51:25 -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(
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050056 m: Message, user: User,
57 modalFilter?: (i: Interaction) => boolean | Promise<boolean>,
58 interactionFilter?: (i: MessageComponentInteraction) => boolean | Promise<boolean>
59): 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({
Samuel Shuert27bf3cd2023-03-03 15:51:25 -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 })
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050068 .on("collect", async (i: ButtonInteraction) => {
69 mod.stop();
70 int.stop();
71 await i.deferUpdate();
Skyler Grey75ea9172022-08-06 10:22:23 +010072 resolve(i);
73 });
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050074 const mod = new InteractionCollector(client as Client, {
75 filter: (i: Interaction) => (modalFilter ? modalFilter(i) : true) && i.isModalSubmit() && defaultModalFilter(i, user, m),
Skyler Grey75ea9172022-08-06 10:22:23 +010076 time: 300000
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050077 }).on("collect", async (i: ModalSubmitInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010078 int.stop();
Skyler Grey75ea9172022-08-06 10:22:23 +010079 mod.stop();
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050080 await i.deferUpdate();
81 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) {
pineafan63fc5e22022-08-04 22:04:10 +010085 console.log(e);
pineafan6702cef2022-06-13 17:52:37 +010086 return null;
87 }
88 return out;
Skyler Grey75ea9172022-08-06 10:22:23 +010089}