|
1 | | -import { ComponentInteraction, Guild, Member, MessageFlags, Shard, User, type AnyTextableGuildChannel, type MessageComponentTypes } from "oceanic.js"; |
| 1 | +import { ComponentInteraction, ComponentTypes, Guild, Member, MessageFlags, Shard, User, type AnyTextableGuildChannel, type MessageComponentTypes } from "oceanic.js"; |
2 | 2 | import { TTLMap } from "../../../../../common/ttlMap.ts"; |
3 | | -import type { ActionRowComponent, ComponentCallback, ComponentContext, Reply } from "../../public/command.ts"; |
| 3 | +import type { AnyCommandComponentWithCallback, CommandActionRow, CommandComponent, CommandComponentCallback, CommandContainerComponent, CommandSectionComponent, ComponentContext, Reply } from "../../public/command.ts"; |
4 | 4 | import { defineEventListener } from "../../public/eventListener.ts"; |
5 | 5 | import { AUTO_DEFER_AFTER, STATE_CLEANUP_INTERVAL, STATE_EXPIRE_AFTER, transformReply } from "../index.ts"; |
6 | 6 |
|
7 | 7 | interface ComponentData { |
8 | | - callbacks: Map<string, Required<ComponentCallback>>; |
| 8 | + callbacks: Map<string, Required<CommandComponentCallback>>; |
9 | 9 | invokerID: string; |
10 | 10 | } |
11 | 11 |
|
@@ -52,25 +52,49 @@ export const componentInterationHandler = defineEventListener("interactionCreate |
52 | 52 | } |
53 | 53 | }); |
54 | 54 |
|
55 | | -export function listenForInteractions(messageID: string, invokerID: string, components: ActionRowComponent[][]): void { |
| 55 | +export function listenForInteractions(messageID: string, invokerID: string, components: CommandComponent[]): void { |
56 | 56 | const callbacks: ComponentData["callbacks"] = new Map; |
57 | 57 |
|
58 | | - for (const row of components) { |
59 | | - for (const component of row) { |
60 | | - if (!("callback" in component)) |
61 | | - continue; |
62 | | - |
63 | | - if (component.disabled) |
64 | | - continue; |
65 | | - |
66 | | - const { customID, callback, invokerOnly } = component; |
67 | | - callbacks.set(customID, { callback, invokerOnly: invokerOnly ?? true }); |
| 58 | + for (const component of components) { |
| 59 | + if (component.type === ComponentTypes.ACTION_ROW) |
| 60 | + putCallbacksForActionRow(callbacks, component); |
| 61 | + else if (component.type === ComponentTypes.SECTION) |
| 62 | + putCallbackForSection(callbacks, component); |
| 63 | + else if (component.type === ComponentTypes.CONTAINER) { |
| 64 | + putCallbacksForContainer(callbacks, component); |
68 | 65 | } |
69 | 66 | } |
70 | 67 |
|
71 | 68 | activeComponents.set(messageID, { callbacks, invokerID: invokerID }); |
72 | 69 | } |
73 | 70 |
|
| 71 | +export function putCallbacksForActionRow(callbacks: ComponentData["callbacks"], row: CommandActionRow): void { |
| 72 | + for (const action of row.components) |
| 73 | + if ("callback" in action) |
| 74 | + putCallback(callbacks, action); |
| 75 | +} |
| 76 | + |
| 77 | +export function putCallbackForSection(callbacks: ComponentData["callbacks"], section: CommandSectionComponent): void { |
| 78 | + if ("callback" in section.accessory) |
| 79 | + putCallback(callbacks, section.accessory); |
| 80 | +} |
| 81 | +export function putCallbacksForContainer(callbacks: ComponentData["callbacks"], section: CommandContainerComponent): void { |
| 82 | + for (const component of section.components) { |
| 83 | + if (component.type === ComponentTypes.ACTION_ROW) |
| 84 | + putCallbacksForActionRow(callbacks, component); |
| 85 | + else if (component.type === ComponentTypes.SECTION) |
| 86 | + putCallbackForSection(callbacks, component); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +export function putCallback(callbacks: ComponentData["callbacks"], component: AnyCommandComponentWithCallback): void { |
| 92 | + callbacks.set( |
| 93 | + component.customID, |
| 94 | + { callback: component.callback, invokerOnly: component.invokerOnly ?? true } |
| 95 | + ); |
| 96 | +} |
| 97 | + |
74 | 98 | export function unlistenForInteractions(messageID: string): void { |
75 | 99 | activeComponents.delete(messageID); |
76 | 100 | } |
|
0 commit comments