mini-interactions 0.0.0-stage → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/MIGRATION.md +126 -0
- package/README.md +303 -2
- package/dist/bot.d.ts +113 -0
- package/dist/bot.js +293 -0
- package/dist/builders/ActionRowBuilder.d.ts +22 -0
- package/dist/builders/ActionRowBuilder.js +33 -0
- package/dist/builders/AutomodRuleBuilder.d.ts +45 -0
- package/dist/builders/AutomodRuleBuilder.js +151 -0
- package/dist/builders/ButtonBuilder.d.ts +53 -0
- package/dist/builders/ButtonBuilder.js +124 -0
- package/dist/builders/ChannelSelectMenuBuilder.d.ts +53 -0
- package/dist/builders/ChannelSelectMenuBuilder.js +105 -0
- package/dist/builders/CheckboxBuilder.d.ts +20 -0
- package/dist/builders/CheckboxBuilder.js +20 -0
- package/dist/builders/CheckboxGroupBuilder.d.ts +25 -0
- package/dist/builders/CheckboxGroupBuilder.js +48 -0
- package/dist/builders/EmbedBuilder.d.ts +33 -0
- package/dist/builders/EmbedBuilder.js +103 -0
- package/dist/builders/FileUploadBuilder.d.ts +17 -0
- package/dist/builders/FileUploadBuilder.js +22 -0
- package/dist/builders/LabelBuilder.d.ts +16 -0
- package/dist/builders/LabelBuilder.js +21 -0
- package/dist/builders/MiniContainerBuilder.d.ts +131 -0
- package/dist/builders/MiniContainerBuilder.js +347 -0
- package/dist/builders/ModalBuilder.d.ts +16 -0
- package/dist/builders/ModalBuilder.js +36 -0
- package/dist/builders/ModalChannelSelectMenuBuilder.d.ts +29 -0
- package/dist/builders/ModalChannelSelectMenuBuilder.js +25 -0
- package/dist/builders/ModalMentionableSelectMenuBuilder.d.ts +53 -0
- package/dist/builders/ModalMentionableSelectMenuBuilder.js +87 -0
- package/dist/builders/ModalRoleSelectMenuBuilder.d.ts +27 -0
- package/dist/builders/ModalRoleSelectMenuBuilder.js +24 -0
- package/dist/builders/ModalStringSelectMenuBuilder.d.ts +62 -0
- package/dist/builders/ModalStringSelectMenuBuilder.js +120 -0
- package/dist/builders/ModalUserSelectMenuBuilder.d.ts +53 -0
- package/dist/builders/ModalUserSelectMenuBuilder.js +96 -0
- package/dist/builders/RadioBuilder.d.ts +22 -0
- package/dist/builders/RadioBuilder.js +36 -0
- package/dist/builders/RoleSelectMenuBuilder.d.ts +47 -0
- package/dist/builders/RoleSelectMenuBuilder.js +92 -0
- package/dist/builders/StringSelectMenuBuilder.d.ts +52 -0
- package/dist/builders/StringSelectMenuBuilder.js +104 -0
- package/dist/builders/StringSelectMenuOptionBuilder.d.ts +46 -0
- package/dist/builders/StringSelectMenuOptionBuilder.js +106 -0
- package/dist/builders/TextInputBuilder.d.ts +23 -0
- package/dist/builders/TextInputBuilder.js +26 -0
- package/dist/builders/index.d.ts +44 -0
- package/dist/builders/index.js +22 -0
- package/dist/builders/shared.d.ts +8 -0
- package/dist/builders/shared.js +11 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +71 -0
- package/dist/commands/CommandBuilder.d.ts +278 -0
- package/dist/commands/CommandBuilder.js +687 -0
- package/dist/commands/ContextMenuCommandBuilder.d.ts +94 -0
- package/dist/commands/ContextMenuCommandBuilder.js +145 -0
- package/dist/core/http/DiscordRestClient.d.ts +400 -0
- package/dist/core/http/DiscordRestClient.js +988 -0
- package/dist/core/interactions/InteractionContext.d.ts +33 -0
- package/dist/core/interactions/InteractionContext.js +61 -0
- package/dist/core/interactions/InteractionVerifier.d.ts +8 -0
- package/dist/core/interactions/InteractionVerifier.js +9 -0
- package/dist/core/messages/DiscordSentMessage.d.ts +32 -0
- package/dist/core/messages/DiscordSentMessage.js +64 -0
- package/dist/core/messages/message-payloads.d.ts +94 -0
- package/dist/core/messages/message-payloads.js +99 -0
- package/dist/core/webhooks/DiscordWebhook.d.ts +14 -0
- package/dist/core/webhooks/DiscordWebhook.js +69 -0
- package/dist/database/MiniDataBuilder.d.ts +61 -0
- package/dist/database/MiniDataBuilder.js +98 -0
- package/dist/database/MiniDatabase.d.ts +67 -0
- package/dist/database/MiniDatabase.js +222 -0
- package/dist/database/MiniDatabaseBuilder.d.ts +58 -0
- package/dist/database/MiniDatabaseBuilder.js +84 -0
- package/dist/database/index.d.ts +5 -0
- package/dist/database/index.js +3 -0
- package/dist/identity/ApplicationIdentityProfile.d.ts +188 -0
- package/dist/identity/ApplicationIdentityProfile.js +207 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +34 -0
- package/dist/lobby/Lobby.d.ts +150 -0
- package/dist/lobby/Lobby.js +90 -0
- package/dist/node/MiniInteraction.d.ts +199 -0
- package/dist/node/MiniInteraction.js +861 -0
- package/dist/node/index.d.ts +2 -0
- package/dist/node/index.js +1 -0
- package/dist/oauth2/DiscordOAuth.d.ts +49 -0
- package/dist/oauth2/DiscordOAuth.js +113 -0
- package/dist/oauth2/OAuth2Builder.d.ts +296 -0
- package/dist/oauth2/OAuth2Builder.js +435 -0
- package/dist/oauth2/OAuth2Scopes.d.ts +285 -0
- package/dist/oauth2/OAuth2Scopes.js +438 -0
- package/dist/oauth2/OAuthTokenStorage.d.ts +37 -0
- package/dist/oauth2/OAuthTokenStorage.js +109 -0
- package/dist/oauth2/index.d.ts +8 -0
- package/dist/oauth2/index.js +4 -0
- package/dist/router/AutocompleteContext.d.ts +36 -0
- package/dist/router/AutocompleteContext.js +82 -0
- package/dist/router/InteractionRouter.d.ts +82 -0
- package/dist/router/InteractionRouter.js +225 -0
- package/dist/types/ButtonStyle.d.ts +16 -0
- package/dist/types/ButtonStyle.js +17 -0
- package/dist/types/ChannelType.d.ts +2 -0
- package/dist/types/ChannelType.js +2 -0
- package/dist/types/Commands.d.ts +35 -0
- package/dist/types/Commands.js +1 -0
- package/dist/types/ComponentTypes.d.ts +19 -0
- package/dist/types/ComponentTypes.js +1 -0
- package/dist/types/InteractionFlags.d.ts +5 -0
- package/dist/types/InteractionFlags.js +6 -0
- package/dist/types/PermissionFlags.d.ts +56 -0
- package/dist/types/PermissionFlags.js +5 -0
- package/dist/types/RoleConnectionMetadata.d.ts +14 -0
- package/dist/types/RoleConnectionMetadata.js +1 -0
- package/dist/types/RoleConnectionMetadataTypes.d.ts +11 -0
- package/dist/types/RoleConnectionMetadataTypes.js +12 -0
- package/dist/types/SeparatorSpacingSize.d.ts +2 -0
- package/dist/types/SeparatorSpacingSize.js +2 -0
- package/dist/types/checkbox.d.ts +16 -0
- package/dist/types/checkbox.js +12 -0
- package/dist/types/discord.d.ts +2 -0
- package/dist/types/discord.js +1 -0
- package/dist/types/radio.d.ts +12 -0
- package/dist/types/radio.js +10 -0
- package/dist/types/validation.d.ts +14 -0
- package/dist/types/validation.js +40 -0
- package/dist/utils/CommandInteractionOptions.d.ts +66 -0
- package/dist/utils/CommandInteractionOptions.js +364 -0
- package/dist/utils/ContextMenuInteraction.d.ts +76 -0
- package/dist/utils/ContextMenuInteraction.js +152 -0
- package/dist/utils/MessageComponentInteraction.d.ts +148 -0
- package/dist/utils/MessageComponentInteraction.js +259 -0
- package/dist/utils/ModalSubmitInteraction.d.ts +86 -0
- package/dist/utils/ModalSubmitInteraction.js +267 -0
- package/dist/utils/cdn.d.ts +30 -0
- package/dist/utils/cdn.js +60 -0
- package/dist/utils/formatting.d.ts +76 -0
- package/dist/utils/formatting.js +132 -0
- package/dist/utils/interactionMessageHelpers.d.ts +40 -0
- package/dist/utils/interactionMessageHelpers.js +118 -0
- package/dist/utils/localization.d.ts +25 -0
- package/dist/utils/localization.js +38 -0
- package/dist/utils/snowflake.d.ts +19 -0
- package/dist/utils/snowflake.js +32 -0
- package/dist/webhooks/WebhookEvent.d.ts +229 -0
- package/dist/webhooks/WebhookEvent.js +59 -0
- package/dist/webhooks/WebhookEventRouter.d.ts +123 -0
- package/dist/webhooks/WebhookEventRouter.js +160 -0
- package/dist/webhooks/WebhookEventVerifier.d.ts +33 -0
- package/dist/webhooks/WebhookEventVerifier.js +37 -0
- package/dist/webhooks/index.d.ts +6 -0
- package/dist/webhooks/index.js +3 -0
- package/package.json +93 -4
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { InteractionResponseType, } from "discord-api-types/v10";
|
|
2
|
+
import { normaliseInteractionMessageData, normaliseMessageFlags, } from "./interactionMessageHelpers.js";
|
|
3
|
+
function createContextMenuInteractionHelpers(interaction, helpers) {
|
|
4
|
+
let capturedResponse = null;
|
|
5
|
+
let isDeferred = false;
|
|
6
|
+
let hasResponded = false;
|
|
7
|
+
const captureResponse = (response) => {
|
|
8
|
+
capturedResponse = response;
|
|
9
|
+
return response;
|
|
10
|
+
};
|
|
11
|
+
function createMessageResponse(type, data) {
|
|
12
|
+
const normalised = normaliseInteractionMessageData(data);
|
|
13
|
+
if (type === InteractionResponseType.ChannelMessageWithSource) {
|
|
14
|
+
if (!normalised) {
|
|
15
|
+
throw new Error("[MiniInteraction] Channel message responses require data to be provided.");
|
|
16
|
+
}
|
|
17
|
+
return captureResponse({
|
|
18
|
+
type,
|
|
19
|
+
data: normalised,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
if (normalised) {
|
|
23
|
+
return captureResponse({ type, data: normalised });
|
|
24
|
+
}
|
|
25
|
+
return captureResponse({ type });
|
|
26
|
+
}
|
|
27
|
+
const reply = async (data) => {
|
|
28
|
+
if (helpers?.canRespond && !helpers.canRespond(interaction.id)) {
|
|
29
|
+
throw new Error("[MiniInteraction] Interaction cannot respond: already responded or expired");
|
|
30
|
+
}
|
|
31
|
+
const response = createMessageResponse(InteractionResponseType.ChannelMessageWithSource, data);
|
|
32
|
+
if (isDeferred && helpers?.sendFollowUp) {
|
|
33
|
+
await helpers.sendFollowUp(interaction.token, response, '@original');
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
helpers?.onAck?.(response);
|
|
37
|
+
}
|
|
38
|
+
hasResponded = true;
|
|
39
|
+
helpers?.trackResponse?.(interaction.id, interaction.token, 'responded');
|
|
40
|
+
return response;
|
|
41
|
+
};
|
|
42
|
+
const followUp = async (data) => {
|
|
43
|
+
const response = createMessageResponse(InteractionResponseType.ChannelMessageWithSource, data);
|
|
44
|
+
if (helpers?.sendFollowUp) {
|
|
45
|
+
await helpers.sendFollowUp(interaction.token, response, '');
|
|
46
|
+
}
|
|
47
|
+
hasResponded = true;
|
|
48
|
+
helpers?.trackResponse?.(interaction.id, interaction.token, 'responded');
|
|
49
|
+
return response;
|
|
50
|
+
};
|
|
51
|
+
const editReply = async (data) => {
|
|
52
|
+
if (helpers?.canRespond && !helpers.canRespond(interaction.id)) {
|
|
53
|
+
throw new Error("[MiniInteraction] Interaction cannot edit reply: expired");
|
|
54
|
+
}
|
|
55
|
+
// Context menu commands (User/Message) MUST use ChannelMessageWithSource (4)
|
|
56
|
+
// for their initial response if it's the first response.
|
|
57
|
+
const response = createMessageResponse(InteractionResponseType.ChannelMessageWithSource, data);
|
|
58
|
+
if (helpers?.sendFollowUp && (isDeferred || hasResponded)) {
|
|
59
|
+
await helpers.sendFollowUp(interaction.token, response, '@original');
|
|
60
|
+
return capturedResponse;
|
|
61
|
+
}
|
|
62
|
+
captureResponse(response);
|
|
63
|
+
helpers?.onAck?.(response);
|
|
64
|
+
hasResponded = true;
|
|
65
|
+
helpers?.trackResponse?.(interaction.id, interaction.token, 'responded');
|
|
66
|
+
return response;
|
|
67
|
+
};
|
|
68
|
+
const deferReply = (options = {}) => {
|
|
69
|
+
if (helpers?.canRespond && !helpers.canRespond(interaction.id)) {
|
|
70
|
+
throw new Error("[MiniInteraction] Interaction cannot defer: already responded or expired");
|
|
71
|
+
}
|
|
72
|
+
const flags = normaliseMessageFlags(options.flags);
|
|
73
|
+
const response = captureResponse({
|
|
74
|
+
type: InteractionResponseType.DeferredChannelMessageWithSource,
|
|
75
|
+
data: flags ? { flags } : undefined,
|
|
76
|
+
});
|
|
77
|
+
isDeferred = true;
|
|
78
|
+
helpers?.trackResponse?.(interaction.id, interaction.token, 'deferred');
|
|
79
|
+
helpers?.onAck?.(response);
|
|
80
|
+
return response;
|
|
81
|
+
};
|
|
82
|
+
const showModal = (data) => {
|
|
83
|
+
const modalData = typeof data === "object" && "toJSON" in data ? data.toJSON() : data;
|
|
84
|
+
const response = captureResponse({
|
|
85
|
+
type: InteractionResponseType.Modal,
|
|
86
|
+
data: modalData,
|
|
87
|
+
});
|
|
88
|
+
helpers?.onAck?.(response);
|
|
89
|
+
return response;
|
|
90
|
+
};
|
|
91
|
+
const getResponse = () => capturedResponse;
|
|
92
|
+
return {
|
|
93
|
+
getResponse,
|
|
94
|
+
reply,
|
|
95
|
+
followUp,
|
|
96
|
+
editReply: editReply,
|
|
97
|
+
deferReply,
|
|
98
|
+
showModal,
|
|
99
|
+
onAck: helpers?.onAck,
|
|
100
|
+
sendFollowUp: helpers?.sendFollowUp,
|
|
101
|
+
canRespond: helpers?.canRespond,
|
|
102
|
+
trackResponse: helpers?.trackResponse,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Wraps a raw user context menu interaction with helper methods.
|
|
107
|
+
*
|
|
108
|
+
* @param interaction - The raw user context menu interaction payload from Discord.
|
|
109
|
+
* @param helpers - Optional callback helpers.
|
|
110
|
+
* @returns A helper-augmented interaction object.
|
|
111
|
+
*/
|
|
112
|
+
export function createUserContextMenuInteraction(interaction, helpers) {
|
|
113
|
+
return Object.assign(interaction, createContextMenuInteractionHelpers(interaction, helpers), {
|
|
114
|
+
targetUser: resolveTargetUser(interaction),
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Wraps a raw message context menu interaction with helper methods.
|
|
119
|
+
*
|
|
120
|
+
* @param interaction - The raw message context menu interaction payload from Discord.
|
|
121
|
+
* @param helpers - Optional callback helpers.
|
|
122
|
+
* @returns A helper-augmented interaction object.
|
|
123
|
+
*/
|
|
124
|
+
export function createMessageContextMenuInteraction(interaction, helpers) {
|
|
125
|
+
return Object.assign(interaction, createContextMenuInteractionHelpers(interaction, helpers), {
|
|
126
|
+
targetMessage: resolveTargetMessage(interaction),
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Wraps a raw primary entry point interaction with helper methods.
|
|
131
|
+
*
|
|
132
|
+
* @param interaction - The raw primary entry point interaction payload from Discord.
|
|
133
|
+
* @param helpers - Optional callback helpers.
|
|
134
|
+
* @returns A helper-augmented interaction object.
|
|
135
|
+
*/
|
|
136
|
+
export function createAppCommandInteraction(interaction, helpers) {
|
|
137
|
+
return Object.assign(interaction, createContextMenuInteractionHelpers(interaction, helpers));
|
|
138
|
+
}
|
|
139
|
+
function resolveTargetMessage(interaction) {
|
|
140
|
+
const targetId = interaction.data?.target_id;
|
|
141
|
+
if (!targetId) {
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
return interaction.data?.resolved?.messages?.[targetId];
|
|
145
|
+
}
|
|
146
|
+
function resolveTargetUser(interaction) {
|
|
147
|
+
const targetId = interaction.data?.target_id;
|
|
148
|
+
if (!targetId) {
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
return interaction.data?.resolved?.users?.[targetId];
|
|
152
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { type APIInteractionDataResolvedChannel, type APIInteractionDataResolvedGuildMember, type APIInteractionResponse, type APIInteractionResponseChannelMessageWithSource, type APIInteractionResponseDeferredChannelMessageWithSource, type APIInteractionResponseDeferredMessageUpdate, type APIInteractionResponseUpdateMessage, type APIMessageComponentInteraction, type APIMessageButtonInteractionData, type APIMessageChannelSelectInteractionData, type APIMessageMentionableSelectInteractionData, type APIMessageRoleSelectInteractionData, type APIMessageStringSelectInteractionData, type APIMessageUserSelectInteractionData, type APIModalInteractionResponse, type APIModalInteractionResponseCallbackData, type APIRole, type APIUser, type APIAttachment } from "discord-api-types/v10";
|
|
2
|
+
import { DeferReplyOptions, InteractionMessageData } from "./interactionMessageHelpers.js";
|
|
3
|
+
/** Resolved user option including optional guild member data. */
|
|
4
|
+
export type ResolvedUserOption = {
|
|
5
|
+
user: APIUser;
|
|
6
|
+
member?: APIInteractionDataResolvedGuildMember;
|
|
7
|
+
};
|
|
8
|
+
/** Resolved mentionable option (either a user or role). */
|
|
9
|
+
export type ResolvedMentionableOption = {
|
|
10
|
+
type: "user";
|
|
11
|
+
value: ResolvedUserOption;
|
|
12
|
+
} | {
|
|
13
|
+
type: "role";
|
|
14
|
+
value: APIRole;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Base helper methods available on all component interactions.
|
|
18
|
+
*/
|
|
19
|
+
export type BaseComponentInteractionHelpers = {
|
|
20
|
+
trackTiming?: (interactionId: string, operation: string, startTime: number, success: boolean) => void;
|
|
21
|
+
onAck?: (response: APIInteractionResponse) => void;
|
|
22
|
+
sendFollowUp?: (token: string, response: APIInteractionResponse, messageId?: string) => Promise<void>;
|
|
23
|
+
canRespond?: (interactionId: string) => boolean;
|
|
24
|
+
trackResponse?: (interactionId: string, token: string, state: 'responded' | 'deferred') => void;
|
|
25
|
+
};
|
|
26
|
+
type ComponentInteractionBase<Data> = Omit<APIMessageComponentInteraction, "data"> & {
|
|
27
|
+
data: Data;
|
|
28
|
+
getResponse: () => APIInteractionResponse | null;
|
|
29
|
+
reply: (data: InteractionMessageData) => Promise<APIInteractionResponseChannelMessageWithSource>;
|
|
30
|
+
deferReply: (options?: DeferReplyOptions) => APIInteractionResponseDeferredChannelMessageWithSource;
|
|
31
|
+
update: (data?: InteractionMessageData) => Promise<APIInteractionResponseUpdateMessage>;
|
|
32
|
+
deferUpdate: () => APIInteractionResponseDeferredMessageUpdate;
|
|
33
|
+
showModal: (data: APIModalInteractionResponseCallbackData | {
|
|
34
|
+
toJSON(): APIModalInteractionResponseCallbackData;
|
|
35
|
+
}) => APIModalInteractionResponse;
|
|
36
|
+
canRespond?: (interactionId: string) => boolean;
|
|
37
|
+
trackResponse?: (interactionId: string, token: string, state: 'responded' | 'deferred') => void;
|
|
38
|
+
};
|
|
39
|
+
type StringValuedComponentInteraction<Data> = ComponentInteractionBase<Data> & {
|
|
40
|
+
values: string[];
|
|
41
|
+
getStringValues: () => string[];
|
|
42
|
+
};
|
|
43
|
+
/** Button interaction with helper methods. Buttons don't have values or resolved data. */
|
|
44
|
+
export type ButtonInteraction = ComponentInteractionBase<APIMessageButtonInteractionData>;
|
|
45
|
+
/** String select menu interaction with helper methods. */
|
|
46
|
+
export type StringSelectInteraction = StringValuedComponentInteraction<APIMessageStringSelectInteractionData>;
|
|
47
|
+
/** Role select menu interaction with helper methods. */
|
|
48
|
+
export type RoleSelectInteraction = ComponentInteractionBase<APIMessageRoleSelectInteractionData> & {
|
|
49
|
+
values: string[];
|
|
50
|
+
getRoles: () => APIRole[];
|
|
51
|
+
};
|
|
52
|
+
/** User select menu interaction with helper methods. */
|
|
53
|
+
export type UserSelectInteraction = ComponentInteractionBase<APIMessageUserSelectInteractionData> & {
|
|
54
|
+
values: string[];
|
|
55
|
+
getUsers: () => ResolvedUserOption[];
|
|
56
|
+
};
|
|
57
|
+
/** Channel select menu interaction with helper methods. */
|
|
58
|
+
export type ChannelSelectInteraction = ComponentInteractionBase<APIMessageChannelSelectInteractionData> & {
|
|
59
|
+
values: string[];
|
|
60
|
+
getChannels: () => APIInteractionDataResolvedChannel[];
|
|
61
|
+
};
|
|
62
|
+
/** Mentionable select menu interaction with helper methods. */
|
|
63
|
+
export type MentionableSelectInteraction = ComponentInteractionBase<APIMessageMentionableSelectInteractionData> & {
|
|
64
|
+
values: string[];
|
|
65
|
+
getMentionables: () => ResolvedMentionableOption[];
|
|
66
|
+
};
|
|
67
|
+
/** Radio interaction with helper methods. */
|
|
68
|
+
export type RadioInteraction = StringValuedComponentInteraction<APIMessageStringSelectInteractionData>;
|
|
69
|
+
/** Checkbox interaction with helper methods. */
|
|
70
|
+
export type CheckboxInteraction = StringValuedComponentInteraction<APIMessageStringSelectInteractionData>;
|
|
71
|
+
/**
|
|
72
|
+
* Represents a component interaction augmented with helper response methods.
|
|
73
|
+
*
|
|
74
|
+
* Note: The `values` property is available on select menu interactions (data.values).
|
|
75
|
+
* For button interactions, this property will be undefined.
|
|
76
|
+
*/
|
|
77
|
+
export type MessageComponentInteraction = APIMessageComponentInteraction & {
|
|
78
|
+
getResponse: () => APIInteractionResponse | null;
|
|
79
|
+
reply: (data: InteractionMessageData) => Promise<APIInteractionResponseChannelMessageWithSource>;
|
|
80
|
+
deferReply: (options?: DeferReplyOptions) => APIInteractionResponseDeferredChannelMessageWithSource;
|
|
81
|
+
update: (data?: InteractionMessageData) => Promise<APIInteractionResponseUpdateMessage>;
|
|
82
|
+
deferUpdate: () => APIInteractionResponseDeferredMessageUpdate;
|
|
83
|
+
showModal: (data: APIModalInteractionResponseCallbackData | {
|
|
84
|
+
toJSON(): APIModalInteractionResponseCallbackData;
|
|
85
|
+
}) => APIModalInteractionResponse;
|
|
86
|
+
/**
|
|
87
|
+
* Edit the initial interaction response.
|
|
88
|
+
*/
|
|
89
|
+
editReply: (data?: InteractionMessageData) => Promise<APIInteractionResponseUpdateMessage | APIInteractionResponseChannelMessageWithSource>;
|
|
90
|
+
/**
|
|
91
|
+
* Finalise the interaction response via a webhook follow-up.
|
|
92
|
+
* This is automatically called by reply() and update() if the interaction is deferred.
|
|
93
|
+
*/
|
|
94
|
+
sendFollowUp?: (token: string, response: APIInteractionResponse, messageId?: string) => Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* Optional state management helpers.
|
|
97
|
+
*/
|
|
98
|
+
canRespond?: (interactionId: string) => boolean;
|
|
99
|
+
trackResponse?: (interactionId: string, token: string, state: 'responded' | 'deferred') => void;
|
|
100
|
+
/**
|
|
101
|
+
* The selected values from a select menu interaction.
|
|
102
|
+
* This property is only present for select menu interactions.
|
|
103
|
+
* For button interactions, this will be undefined.
|
|
104
|
+
*/
|
|
105
|
+
values?: string[];
|
|
106
|
+
/**
|
|
107
|
+
* Helper method to get selected string values from a string select menu.
|
|
108
|
+
* @returns Array of selected string values, or empty array if not a string select menu
|
|
109
|
+
*/
|
|
110
|
+
getStringValues: () => string[];
|
|
111
|
+
/**
|
|
112
|
+
* Helper method to get selected roles from a role select menu.
|
|
113
|
+
* @returns Array of resolved role objects, or empty array if not a role select menu
|
|
114
|
+
*/
|
|
115
|
+
getRoles: () => APIRole[];
|
|
116
|
+
getRole: () => APIRole | undefined;
|
|
117
|
+
/**
|
|
118
|
+
* Helper method to get selected channels from a channel select menu.
|
|
119
|
+
* @returns Array of resolved channel objects, or empty array if not a channel select menu
|
|
120
|
+
*/
|
|
121
|
+
getChannels: () => APIInteractionDataResolvedChannel[];
|
|
122
|
+
getChannel: () => APIInteractionDataResolvedChannel | undefined;
|
|
123
|
+
/**
|
|
124
|
+
* Helper method to get selected users from a user select menu.
|
|
125
|
+
* @returns Array of resolved user objects (with optional member data), or empty array if not a user select menu
|
|
126
|
+
*/
|
|
127
|
+
getUsers: () => ResolvedUserOption[];
|
|
128
|
+
getUser: () => ResolvedUserOption | undefined;
|
|
129
|
+
/**
|
|
130
|
+
* Helper method to get selected mentionables from a mentionable select menu.
|
|
131
|
+
* @returns Array of resolved mentionable objects (users or roles), or empty array if not a mentionable select menu
|
|
132
|
+
*/
|
|
133
|
+
getMentionables: () => ResolvedMentionableOption[];
|
|
134
|
+
getMentionable: () => ResolvedMentionableOption | undefined;
|
|
135
|
+
/**
|
|
136
|
+
* Helper method to get an attachment value (if applicable).
|
|
137
|
+
*/
|
|
138
|
+
getAttachment: () => APIAttachment | undefined;
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* Wraps a raw component interaction with helper methods mirroring Discord's expected responses.
|
|
142
|
+
*
|
|
143
|
+
* @param interaction - The raw interaction payload from Discord.
|
|
144
|
+
* @param helpers - Optional callback to capture the final interaction response.
|
|
145
|
+
* @returns A helper-augmented interaction object.
|
|
146
|
+
*/
|
|
147
|
+
export declare function createMessageComponentInteraction(interaction: APIMessageComponentInteraction, helpers?: BaseComponentInteractionHelpers): MessageComponentInteraction;
|
|
148
|
+
export {};
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { InteractionResponseType, } from "discord-api-types/v10";
|
|
2
|
+
import { normaliseInteractionMessageData, normaliseMessageFlags, } from "./interactionMessageHelpers.js";
|
|
3
|
+
/**
|
|
4
|
+
* Wraps a raw component interaction with helper methods mirroring Discord's expected responses.
|
|
5
|
+
*
|
|
6
|
+
* @param interaction - The raw interaction payload from Discord.
|
|
7
|
+
* @param helpers - Optional callback to capture the final interaction response.
|
|
8
|
+
* @returns A helper-augmented interaction object.
|
|
9
|
+
*/
|
|
10
|
+
export function createMessageComponentInteraction(interaction, helpers) {
|
|
11
|
+
let capturedResponse = null;
|
|
12
|
+
let isDeferred = false;
|
|
13
|
+
const captureResponse = (response) => {
|
|
14
|
+
capturedResponse = response;
|
|
15
|
+
return response;
|
|
16
|
+
};
|
|
17
|
+
const reply = async (data) => {
|
|
18
|
+
if (helpers?.canRespond && !helpers.canRespond(interaction.id)) {
|
|
19
|
+
throw new Error("[MiniInteraction] Interaction cannot respond: already responded or expired");
|
|
20
|
+
}
|
|
21
|
+
const normalisedData = normaliseInteractionMessageData(data);
|
|
22
|
+
if (!normalisedData) {
|
|
23
|
+
throw new Error("[MiniInteraction] Component replies require response data to be provided.");
|
|
24
|
+
}
|
|
25
|
+
const response = captureResponse({
|
|
26
|
+
type: InteractionResponseType.ChannelMessageWithSource,
|
|
27
|
+
data: normalisedData,
|
|
28
|
+
});
|
|
29
|
+
if (isDeferred && helpers?.sendFollowUp) {
|
|
30
|
+
await helpers.sendFollowUp(interaction.token, response, '@original');
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
helpers?.onAck?.(response);
|
|
34
|
+
}
|
|
35
|
+
helpers?.trackResponse?.(interaction.id, interaction.token, 'responded');
|
|
36
|
+
return response;
|
|
37
|
+
};
|
|
38
|
+
const deferReply = (options) => {
|
|
39
|
+
if (helpers?.canRespond && !helpers.canRespond(interaction.id)) {
|
|
40
|
+
throw new Error("[MiniInteraction] Interaction cannot defer: already responded or expired");
|
|
41
|
+
}
|
|
42
|
+
const flags = normaliseMessageFlags(options?.flags);
|
|
43
|
+
const response = flags !== undefined
|
|
44
|
+
? {
|
|
45
|
+
type: InteractionResponseType.DeferredChannelMessageWithSource,
|
|
46
|
+
data: { flags },
|
|
47
|
+
}
|
|
48
|
+
: {
|
|
49
|
+
type: InteractionResponseType.DeferredChannelMessageWithSource,
|
|
50
|
+
};
|
|
51
|
+
captureResponse(response);
|
|
52
|
+
isDeferred = true;
|
|
53
|
+
helpers?.trackResponse?.(interaction.id, interaction.token, 'deferred');
|
|
54
|
+
helpers?.onAck?.(response);
|
|
55
|
+
return response;
|
|
56
|
+
};
|
|
57
|
+
const update = async (data) => {
|
|
58
|
+
if (helpers?.canRespond && !helpers.canRespond(interaction.id)) {
|
|
59
|
+
throw new Error("[MiniInteraction] Interaction cannot update: already responded or expired");
|
|
60
|
+
}
|
|
61
|
+
const normalisedData = normaliseInteractionMessageData(data);
|
|
62
|
+
const response = captureResponse(normalisedData
|
|
63
|
+
? {
|
|
64
|
+
type: InteractionResponseType.UpdateMessage,
|
|
65
|
+
data: normalisedData,
|
|
66
|
+
}
|
|
67
|
+
: {
|
|
68
|
+
type: InteractionResponseType.UpdateMessage,
|
|
69
|
+
});
|
|
70
|
+
if (isDeferred && helpers?.sendFollowUp) {
|
|
71
|
+
await helpers.sendFollowUp(interaction.token, response, '@original');
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
helpers?.onAck?.(response);
|
|
75
|
+
}
|
|
76
|
+
helpers?.trackResponse?.(interaction.id, interaction.token, 'responded');
|
|
77
|
+
return response;
|
|
78
|
+
};
|
|
79
|
+
const deferUpdate = () => {
|
|
80
|
+
if (helpers?.canRespond && !helpers.canRespond(interaction.id)) {
|
|
81
|
+
throw new Error("[MiniInteraction] Interaction cannot defer update: already responded or expired");
|
|
82
|
+
}
|
|
83
|
+
const response = captureResponse({
|
|
84
|
+
type: InteractionResponseType.DeferredMessageUpdate,
|
|
85
|
+
});
|
|
86
|
+
isDeferred = true;
|
|
87
|
+
helpers?.trackResponse?.(interaction.id, interaction.token, 'deferred');
|
|
88
|
+
helpers?.onAck?.(response);
|
|
89
|
+
return response;
|
|
90
|
+
};
|
|
91
|
+
const showModal = (data) => {
|
|
92
|
+
const resolvedData = typeof data === "object" &&
|
|
93
|
+
"toJSON" in data &&
|
|
94
|
+
typeof data.toJSON === "function"
|
|
95
|
+
? data.toJSON()
|
|
96
|
+
: data;
|
|
97
|
+
const response = captureResponse({
|
|
98
|
+
type: InteractionResponseType.Modal,
|
|
99
|
+
data: resolvedData,
|
|
100
|
+
});
|
|
101
|
+
helpers?.onAck?.(response);
|
|
102
|
+
return response;
|
|
103
|
+
};
|
|
104
|
+
const editReply = async (data) => {
|
|
105
|
+
if (helpers?.canRespond && !helpers.canRespond(interaction.id)) {
|
|
106
|
+
throw new Error("[MiniInteraction] Interaction cannot edit reply: expired");
|
|
107
|
+
}
|
|
108
|
+
const normalisedData = normaliseInteractionMessageData(data);
|
|
109
|
+
const response = captureResponse(normalisedData
|
|
110
|
+
? {
|
|
111
|
+
type: InteractionResponseType.ChannelMessageWithSource,
|
|
112
|
+
data: normalisedData,
|
|
113
|
+
}
|
|
114
|
+
: {
|
|
115
|
+
type: InteractionResponseType.ChannelMessageWithSource,
|
|
116
|
+
data: { content: "" }
|
|
117
|
+
});
|
|
118
|
+
if (helpers?.sendFollowUp) {
|
|
119
|
+
await helpers.sendFollowUp(interaction.token, response, "@original");
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
helpers?.onAck?.(response);
|
|
123
|
+
}
|
|
124
|
+
helpers?.trackResponse?.(interaction.id, interaction.token, "responded");
|
|
125
|
+
return response;
|
|
126
|
+
};
|
|
127
|
+
const getResponse = () => capturedResponse;
|
|
128
|
+
// Extract values from select menu interactions
|
|
129
|
+
const values = "values" in interaction.data ? interaction.data.values : undefined;
|
|
130
|
+
// Helper methods for select menu interactions
|
|
131
|
+
const getStringValues = () => {
|
|
132
|
+
return values ?? [];
|
|
133
|
+
};
|
|
134
|
+
const getRoles = () => {
|
|
135
|
+
if (!values) {
|
|
136
|
+
return [];
|
|
137
|
+
}
|
|
138
|
+
// Type guard: check if resolved data exists
|
|
139
|
+
const resolved = "resolved" in interaction.data
|
|
140
|
+
? interaction.data.resolved
|
|
141
|
+
: undefined;
|
|
142
|
+
if (!resolved?.roles) {
|
|
143
|
+
return [];
|
|
144
|
+
}
|
|
145
|
+
const roles = [];
|
|
146
|
+
for (const roleId of values) {
|
|
147
|
+
const role = resolved.roles[roleId];
|
|
148
|
+
if (role) {
|
|
149
|
+
roles.push(role);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return roles;
|
|
153
|
+
};
|
|
154
|
+
const getRole = () => getRoles()[0];
|
|
155
|
+
const getChannels = () => {
|
|
156
|
+
if (!values) {
|
|
157
|
+
return [];
|
|
158
|
+
}
|
|
159
|
+
// Type guard: check if resolved data exists
|
|
160
|
+
const resolved = "resolved" in interaction.data
|
|
161
|
+
? interaction.data.resolved
|
|
162
|
+
: undefined;
|
|
163
|
+
if (!resolved?.channels) {
|
|
164
|
+
return [];
|
|
165
|
+
}
|
|
166
|
+
const channels = [];
|
|
167
|
+
for (const channelId of values) {
|
|
168
|
+
const channel = resolved.channels[channelId];
|
|
169
|
+
if (channel) {
|
|
170
|
+
channels.push(channel);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return channels;
|
|
174
|
+
};
|
|
175
|
+
const getChannel = () => getChannels()[0];
|
|
176
|
+
const getUsers = () => {
|
|
177
|
+
if (!values) {
|
|
178
|
+
return [];
|
|
179
|
+
}
|
|
180
|
+
// Type guard: check if resolved data exists
|
|
181
|
+
const resolved = "resolved" in interaction.data
|
|
182
|
+
? interaction.data.resolved
|
|
183
|
+
: undefined;
|
|
184
|
+
if (!resolved?.users) {
|
|
185
|
+
return [];
|
|
186
|
+
}
|
|
187
|
+
const users = [];
|
|
188
|
+
for (const userId of values) {
|
|
189
|
+
const user = resolved.users[userId];
|
|
190
|
+
if (user) {
|
|
191
|
+
const member = resolved.members?.[userId];
|
|
192
|
+
users.push({ user, member });
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return users;
|
|
196
|
+
};
|
|
197
|
+
const getUser = () => getUsers()[0];
|
|
198
|
+
const getMentionables = () => {
|
|
199
|
+
if (!values) {
|
|
200
|
+
return [];
|
|
201
|
+
}
|
|
202
|
+
// Type guard: check if resolved data exists
|
|
203
|
+
const resolved = "resolved" in interaction.data
|
|
204
|
+
? interaction.data.resolved
|
|
205
|
+
: undefined;
|
|
206
|
+
if (!resolved) {
|
|
207
|
+
return [];
|
|
208
|
+
}
|
|
209
|
+
const mentionables = [];
|
|
210
|
+
for (const id of values) {
|
|
211
|
+
// Check if it's a role
|
|
212
|
+
const role = resolved.roles?.[id];
|
|
213
|
+
if (role) {
|
|
214
|
+
mentionables.push({ type: "role", value: role });
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
// Check if it's a user
|
|
218
|
+
const user = resolved.users?.[id];
|
|
219
|
+
if (user) {
|
|
220
|
+
const member = resolved.members?.[id];
|
|
221
|
+
mentionables.push({ type: "user", value: { user, member } });
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return mentionables;
|
|
225
|
+
};
|
|
226
|
+
const getMentionable = () => getMentionables()[0];
|
|
227
|
+
const getAttachment = () => {
|
|
228
|
+
if (!values || values.length === 0)
|
|
229
|
+
return undefined;
|
|
230
|
+
const resolved = "resolved" in interaction.data
|
|
231
|
+
? interaction.data.resolved
|
|
232
|
+
: undefined;
|
|
233
|
+
return resolved?.attachments?.[values[0]];
|
|
234
|
+
};
|
|
235
|
+
return Object.assign(interaction, {
|
|
236
|
+
reply,
|
|
237
|
+
deferReply,
|
|
238
|
+
update,
|
|
239
|
+
deferUpdate,
|
|
240
|
+
showModal,
|
|
241
|
+
editReply,
|
|
242
|
+
getResponse,
|
|
243
|
+
values,
|
|
244
|
+
getStringValues,
|
|
245
|
+
getRoles,
|
|
246
|
+
getRole,
|
|
247
|
+
getChannels,
|
|
248
|
+
getChannel,
|
|
249
|
+
getUsers,
|
|
250
|
+
getUser,
|
|
251
|
+
getMentionables,
|
|
252
|
+
getMentionable,
|
|
253
|
+
getAttachment,
|
|
254
|
+
onAck: helpers?.onAck,
|
|
255
|
+
sendFollowUp: helpers?.sendFollowUp,
|
|
256
|
+
canRespond: helpers?.canRespond,
|
|
257
|
+
trackResponse: helpers?.trackResponse,
|
|
258
|
+
});
|
|
259
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { type APIInteractionResponse, type APIInteractionResponseChannelMessageWithSource, type APIInteractionResponseDeferredChannelMessageWithSource, type APIModalSubmitInteraction, type APIRole, type APIInteractionDataResolvedChannel, type APIAttachment } from "discord-api-types/v10";
|
|
2
|
+
import { DeferReplyOptions, InteractionMessageData } from "./interactionMessageHelpers.js";
|
|
3
|
+
import type { ResolvedUserOption } from "./MessageComponentInteraction.js";
|
|
4
|
+
/**
|
|
5
|
+
* Represents a modal submit interaction augmented with helper response methods.
|
|
6
|
+
*/
|
|
7
|
+
export type ModalSubmitInteraction = APIModalSubmitInteraction & {
|
|
8
|
+
getResponse: () => APIInteractionResponse | null;
|
|
9
|
+
reply: (data: InteractionMessageData) => Promise<APIInteractionResponseChannelMessageWithSource>;
|
|
10
|
+
deferReply: (options?: DeferReplyOptions) => APIInteractionResponseDeferredChannelMessageWithSource;
|
|
11
|
+
/**
|
|
12
|
+
* Helper method to get the value of a text input component by its custom ID.
|
|
13
|
+
*/
|
|
14
|
+
getTextFieldValue: (customId: string) => string | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Helper method to get the value(s) of a select menu component by its custom ID.
|
|
17
|
+
*/
|
|
18
|
+
getSelectMenuValues: (customId: string) => string[] | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Helper method to get the value of any component by its custom ID.
|
|
21
|
+
* Returns string for text inputs, string[] for select menus, or undefined.
|
|
22
|
+
*/
|
|
23
|
+
getComponentValue: (customId: string) => string | string[] | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* Helper method to get selected roles from a role select menu in the modal.
|
|
26
|
+
*/
|
|
27
|
+
getRoles: (customId: string) => APIRole[];
|
|
28
|
+
getRole: (customId: string) => APIRole | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Helper method to get selected users from a user select menu in the modal.
|
|
31
|
+
*/
|
|
32
|
+
getUsers: (customId: string) => ResolvedUserOption[];
|
|
33
|
+
getUser: (customId: string) => ResolvedUserOption | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* Helper method to get selected channels from a channel select menu in the modal.
|
|
36
|
+
*/
|
|
37
|
+
getChannels: (customId: string) => APIInteractionDataResolvedChannel[];
|
|
38
|
+
getChannel: (customId: string) => APIInteractionDataResolvedChannel | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Helper method to get the selected value of a RadioGroup component (type 21).
|
|
41
|
+
*/
|
|
42
|
+
getRadioGroupValue: (customId: string) => string | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Helper method to get selected values of a CheckboxGroup component (type 22).
|
|
45
|
+
*/
|
|
46
|
+
getCheckboxGroupValues: (customId: string) => string[];
|
|
47
|
+
/**
|
|
48
|
+
* Helper method to get the boolean state of a single Checkbox component (type 23).
|
|
49
|
+
*/
|
|
50
|
+
getCheckboxValue: (customId: string) => boolean | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* Helper method to get uploaded attachment ids of a FileUpload component (type 19).
|
|
53
|
+
*/
|
|
54
|
+
getFileUploadValues: (customId: string) => string[];
|
|
55
|
+
/**
|
|
56
|
+
* Helper method to get an attachment value (e.g. from FileUpload component).
|
|
57
|
+
*/
|
|
58
|
+
getAttachment: (customId: string) => APIAttachment | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Finalise the interaction response via a webhook follow-up.
|
|
61
|
+
* This is automatically called by reply() if the interaction is deferred.
|
|
62
|
+
*/
|
|
63
|
+
sendFollowUp?: (token: string, response: APIInteractionResponse, messageId?: string) => Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* Optional state management helpers.
|
|
66
|
+
*/
|
|
67
|
+
canRespond?: (interactionId: string) => boolean;
|
|
68
|
+
trackResponse?: (interactionId: string, token: string, state: 'responded' | 'deferred') => void;
|
|
69
|
+
/**
|
|
70
|
+
* Edit the initial interaction response.
|
|
71
|
+
*/
|
|
72
|
+
editReply: (data?: InteractionMessageData) => Promise<APIInteractionResponseChannelMessageWithSource>;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Wraps a raw modal submit interaction with helper methods mirroring Discord's expected responses.
|
|
76
|
+
*
|
|
77
|
+
* @param interaction - The raw interaction payload from Discord.
|
|
78
|
+
* @param helpers - Optional callback to capture the final interaction response.
|
|
79
|
+
* @returns A helper-augmented interaction object.
|
|
80
|
+
*/
|
|
81
|
+
export declare function createModalSubmitInteraction(interaction: APIModalSubmitInteraction, helpers?: {
|
|
82
|
+
onAck?: (response: APIInteractionResponse) => void;
|
|
83
|
+
sendFollowUp?: (token: string, response: APIInteractionResponse, messageId?: string) => Promise<void>;
|
|
84
|
+
canRespond?: (interactionId: string) => boolean;
|
|
85
|
+
trackResponse?: (interactionId: string, token: string, state: 'responded' | 'deferred') => void;
|
|
86
|
+
}): ModalSubmitInteraction;
|