TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 1 | import { ButtonInteraction, Client, User, 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 | |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 48 | function defaultInteractionFilter(i: MessageComponentInteraction, user: User, m: Message) { |
| 49 | return i.channel!.id === m.channel!.id && i.user.id === user.id |
| 50 | } |
| 51 | function defaultModalFilter(i: ModalSubmitInteraction, user: User, m: Message) { |
| 52 | return i.channel!.id === m.channel!.id && i.user.id === user.id |
| 53 | } |
| 54 | |
| 55 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 56 | export async function modalInteractionCollector( |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 57 | m: Message, user: User, |
| 58 | modalFilter?: (i: Interaction) => boolean | Promise<boolean>, |
| 59 | interactionFilter?: (i: MessageComponentInteraction) => boolean | Promise<boolean> |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 60 | ): Promise<null | ButtonInteraction | ModalSubmitInteraction> { |
| 61 | let out: ButtonInteraction | ModalSubmitInteraction; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 62 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 63 | out = await new Promise((resolve, _reject) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 64 | const int = m |
| 65 | .createMessageComponentCollector({ |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 66 | filter: (i: MessageComponentInteraction) => (interactionFilter ? interactionFilter(i) : true) && defaultInteractionFilter(i, user, m), |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 67 | time: 300000 |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 68 | }) |
TheCodedProf | 9bc8475 | 2023-01-21 05:19:57 -0500 | [diff] [blame] | 69 | .on("collect", async (i: ButtonInteraction) => { |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 70 | mod.stop(); |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 71 | int.stop(); |
| 72 | await i.deferUpdate(); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 73 | resolve(i); |
| 74 | }); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 75 | const mod = new InteractionCollector(client as Client, { |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 76 | filter: (i: Interaction) => (modalFilter ? modalFilter(i) : true) && i.isModalSubmit() && defaultModalFilter(i, user, m), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 77 | time: 300000 |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 78 | }).on("collect", async (i: ModalSubmitInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 79 | int.stop(); |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 80 | mod.stop(); |
| 81 | await i.deferUpdate(); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 82 | resolve(i); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 83 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 84 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 85 | } catch (e) { |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 86 | console.log(e); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 87 | return null; |
| 88 | } |
| 89 | return out; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 90 | } |