Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -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 |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 13 | .createMessageComponentCollector({ |
| 14 | filter: (m) => interactionFilter(m), |
| 15 | time: 300000 |
| 16 | }) |
| 17 | .on("collect", (m) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 18 | resolve(m); |
| 19 | }); |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 20 | const int = m.channel.createMessageCollector({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 21 | filter: (m) => messageFilter(m), |
| 22 | time: 300000 |
| 23 | }) |
| 24 | .on("collect", (m) => { |
| 25 | try { |
| 26 | m.delete(); |
| 27 | } catch (e) { |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 28 | client.emit("error", e as Error); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 29 | } |
| 30 | resolve(m); |
| 31 | }); |
| 32 | mes.on("end", () => { |
| 33 | int.stop(); |
| 34 | }); |
| 35 | int.on("end", () => { |
| 36 | mes.stop(); |
| 37 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 38 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 39 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 40 | console.log(e); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 41 | return null; |
| 42 | } |
| 43 | |
| 44 | return out; |
| 45 | } |
| 46 | |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 47 | function defaultInteractionFilter(i: MessageComponentInteraction, user: User, m: Message) { |
| 48 | return i.channel!.id === m.channel!.id && i.user.id === user.id |
| 49 | } |
| 50 | function defaultModalFilter(i: ModalSubmitInteraction, user: User, m: Message) { |
| 51 | return i.channel!.id === m.channel!.id && i.user.id === user.id |
| 52 | } |
| 53 | |
| 54 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 55 | export async function modalInteractionCollector( |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 56 | 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; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 61 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 62 | out = await new Promise((resolve, _reject) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 63 | const int = m |
| 64 | .createMessageComponentCollector({ |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 65 | filter: (i: MessageComponentInteraction) => (interactionFilter ? interactionFilter(i) : true) && defaultInteractionFilter(i, user, m), |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 66 | time: 300000 |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 67 | }) |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 68 | .on("collect", async (i: ButtonInteraction) => { |
| 69 | mod.stop(); |
| 70 | int.stop(); |
| 71 | await i.deferUpdate(); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 72 | resolve(i); |
| 73 | }); |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 74 | const mod = new InteractionCollector(client as Client, { |
| 75 | 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] | 76 | time: 300000 |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 77 | }).on("collect", async (i: ModalSubmitInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 78 | int.stop(); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 79 | mod.stop(); |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 80 | await i.deferUpdate(); |
| 81 | resolve(i); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 82 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 83 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 84 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 85 | console.log(e); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 86 | return null; |
| 87 | } |
| 88 | return out; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 89 | } |