TheCodedProf | 456a20f | 2023-01-19 15:55:34 -0500 | [diff] [blame] | 1 | import { ButtonInteraction, Client, Interaction, InteractionCollector, Message, MessageComponentInteraction, ModalSubmitInteraction } from "discord.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 2 | import client from "./client.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 3 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 4 | export default async function ( |
| 5 | m: Message, |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 6 | interactionFilter: (i: MessageComponentInteraction) => boolean | Promise<boolean>, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 7 | messageFilter: (m: Message) => boolean | Promise<boolean> |
| 8 | ) { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 9 | let out; |
| 10 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 11 | out = await new Promise((resolve, _reject) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 12 | 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) { |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 29 | client.emit("error", e as Error); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 30 | } |
| 31 | resolve(m); |
| 32 | }); |
| 33 | mes.on("end", () => { |
| 34 | int.stop(); |
| 35 | }); |
| 36 | int.on("end", () => { |
| 37 | mes.stop(); |
| 38 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 39 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 40 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 41 | console.log(e); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 42 | return null; |
| 43 | } |
| 44 | |
| 45 | return out; |
| 46 | } |
| 47 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 48 | export async function modalInteractionCollector( |
| 49 | m: Message, |
| 50 | modalFilter: (i: Interaction) => boolean | Promise<boolean>, |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 51 | interactionFilter: (i: MessageComponentInteraction) => boolean | Promise<boolean> |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 52 | ): Promise<null | ButtonInteraction | ModalSubmitInteraction> { |
| 53 | let out: ButtonInteraction | ModalSubmitInteraction; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 54 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 55 | out = await new Promise((resolve, _reject) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 56 | const int = m |
| 57 | .createMessageComponentCollector({ |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 58 | filter: (i: MessageComponentInteraction) => interactionFilter(i), |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 59 | time: 300000 |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 60 | }) |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame^] | 61 | .on("collect", async (i: ButtonInteraction) => { |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 62 | mod.stop(); |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame^] | 63 | if (!i.deferred) await i.deferUpdate(); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 64 | resolve(i); |
| 65 | }); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 66 | const mod = new InteractionCollector(client as Client, { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 67 | filter: (i: Interaction) => modalFilter(i), |
| 68 | time: 300000 |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 69 | }).on("collect", async (i: ModalSubmitInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 70 | int.stop(); |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame^] | 71 | if (!i.deferred) await i.deferUpdate(); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 72 | resolve(i); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 73 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 74 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 75 | } catch (e) { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 76 | return null; |
| 77 | } |
| 78 | return out; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 79 | } |