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