Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 1 | import { |
| 2 | ButtonInteraction, |
| 3 | Client, |
| 4 | User, |
| 5 | Interaction, |
| 6 | InteractionCollector, |
| 7 | Message, |
| 8 | MessageComponentInteraction, |
| 9 | ModalSubmitInteraction |
| 10 | } from "discord.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 11 | import client from "./client.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 12 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 13 | export default async function ( |
| 14 | m: Message, |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 15 | interactionFilter: (i: MessageComponentInteraction) => boolean | Promise<boolean>, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 16 | messageFilter: (m: Message) => boolean | Promise<boolean> |
| 17 | ) { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 18 | let out; |
| 19 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 20 | out = await new Promise((resolve, _reject) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 21 | const mes = m |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 22 | .createMessageComponentCollector({ |
| 23 | filter: (m) => interactionFilter(m), |
| 24 | time: 300000 |
| 25 | }) |
| 26 | .on("collect", (m) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 27 | resolve(m); |
| 28 | }); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 29 | const int = m.channel |
| 30 | .createMessageCollector({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 31 | filter: (m) => messageFilter(m), |
| 32 | time: 300000 |
| 33 | }) |
| 34 | .on("collect", (m) => { |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame^] | 35 | void m.delete().catch(); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 36 | resolve(m); |
| 37 | }); |
| 38 | mes.on("end", () => { |
| 39 | int.stop(); |
| 40 | }); |
| 41 | int.on("end", () => { |
| 42 | mes.stop(); |
| 43 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 44 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 45 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 46 | console.log(e); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 47 | return null; |
| 48 | } |
| 49 | |
| 50 | return out; |
| 51 | } |
| 52 | |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 53 | function defaultInteractionFilter(i: MessageComponentInteraction, user: User, m: Message) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 54 | return i.channel!.id === m.channel!.id && i.user.id === user.id; |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 55 | } |
| 56 | function defaultModalFilter(i: ModalSubmitInteraction, user: User, m: Message) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 57 | return i.channel!.id === m.channel!.id && i.user.id === user.id; |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 58 | } |
| 59 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 60 | export async function modalInteractionCollector( |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 61 | m: Message, |
| 62 | user: User, |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 63 | modalFilter?: (i: Interaction) => boolean | Promise<boolean>, |
| 64 | interactionFilter?: (i: MessageComponentInteraction) => boolean | Promise<boolean> |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 65 | ): Promise<null | ButtonInteraction | ModalSubmitInteraction> { |
| 66 | let out: ButtonInteraction | ModalSubmitInteraction; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 67 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 68 | out = await new Promise((resolve, _reject) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 69 | const int = m |
| 70 | .createMessageComponentCollector({ |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 71 | filter: (i: MessageComponentInteraction) => |
| 72 | (interactionFilter ? interactionFilter(i) : true) && defaultInteractionFilter(i, user, m), |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 73 | time: 300000 |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 74 | }) |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 75 | .on("collect", async (i: ButtonInteraction) => { |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 76 | mod.stop(); |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 77 | int.stop(); |
| 78 | await i.deferUpdate(); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 79 | resolve(i); |
| 80 | }); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 81 | const mod = new InteractionCollector(client as Client, { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 82 | filter: (i: Interaction) => |
| 83 | (modalFilter ? modalFilter(i) : true) && i.isModalSubmit() && defaultModalFilter(i, user, m), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 84 | time: 300000 |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 85 | }).on("collect", async (i: ModalSubmitInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 86 | int.stop(); |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 87 | mod.stop(); |
| 88 | await i.deferUpdate(); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 89 | resolve(i); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 90 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 91 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 92 | } catch (e) { |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 93 | console.log(e); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 94 | return null; |
| 95 | } |
| 96 | return out; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 97 | } |