PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame^] | 1 | import Discord, { Client, Interaction, Message, MessageComponentInteraction } 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> |
| 52 | ): Promise<null | Interaction> { |
| 53 | let out: Interaction; |
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 | }) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 61 | .on("collect", (i: Interaction) => { |
| 62 | resolve(i); |
| 63 | }); |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame^] | 64 | const mod = new Discord.InteractionCollector(client as Client, { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 65 | filter: (i: Interaction) => modalFilter(i), |
| 66 | time: 300000 |
| 67 | }).on("collect", async (i: Interaction) => { |
| 68 | int.stop(); |
| 69 | (i as Discord.ModalSubmitInteraction).deferUpdate(); |
| 70 | resolve(i as Discord.ModalSubmitInteraction); |
| 71 | }); |
| 72 | int.on("end", () => { |
| 73 | mod.stop(); |
| 74 | }); |
| 75 | mod.on("end", () => { |
| 76 | int.stop(); |
| 77 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 78 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 79 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 80 | console.log(e); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 81 | return null; |
| 82 | } |
| 83 | return out; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 84 | } |