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,12 @@
|
|
|
1
|
+
import { ComponentType } from 'discord-api-types/v10';
|
|
2
|
+
/**
|
|
3
|
+
* Checkboxes are modal-only components.
|
|
4
|
+
* A single checkbox (type 23) holds no options; a checkbox group (type 22)
|
|
5
|
+
* holds up to 10 selectable options.
|
|
6
|
+
*
|
|
7
|
+
* @see {@link https://discord.com/developers/docs/components/reference#checkbox}
|
|
8
|
+
* @see {@link https://discord.com/developers/docs/components/reference#checkbox-group}
|
|
9
|
+
*/
|
|
10
|
+
export const CHECKBOX_COMPONENT_TYPE = ComponentType.Checkbox;
|
|
11
|
+
/** Type value for multi-select checkbox groups (type 22). */
|
|
12
|
+
export const CHECKBOX_GROUP_COMPONENT_TYPE = ComponentType.CheckboxGroup;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ComponentType, type APIRadioGroupComponent, type APIRadioGroupOption } from 'discord-api-types/v10';
|
|
2
|
+
/**
|
|
3
|
+
* Radio groups are modal-only components (must be placed inside a Label).
|
|
4
|
+
* Serialised as Discord's official RadioGroup component (type 21).
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://discord.com/developers/docs/components/reference#radio-group}
|
|
7
|
+
*/
|
|
8
|
+
export declare const RADIO_COMPONENT_TYPE = ComponentType.RadioGroup;
|
|
9
|
+
/** Alias matching Discord's own naming for the component type. */
|
|
10
|
+
export declare const RADIO_GROUP_COMPONENT_TYPE = ComponentType.RadioGroup;
|
|
11
|
+
export type APIRadioComponent = APIRadioGroupComponent;
|
|
12
|
+
export type APIRadioOption = APIRadioGroupOption;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ComponentType, } from 'discord-api-types/v10';
|
|
2
|
+
/**
|
|
3
|
+
* Radio groups are modal-only components (must be placed inside a Label).
|
|
4
|
+
* Serialised as Discord's official RadioGroup component (type 21).
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://discord.com/developers/docs/components/reference#radio-group}
|
|
7
|
+
*/
|
|
8
|
+
export const RADIO_COMPONENT_TYPE = ComponentType.RadioGroup;
|
|
9
|
+
/** Alias matching Discord's own naming for the component type. */
|
|
10
|
+
export const RADIO_GROUP_COMPONENT_TYPE = ComponentType.RadioGroup;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare class ValidationError extends Error {
|
|
2
|
+
readonly builder: string;
|
|
3
|
+
readonly field: string;
|
|
4
|
+
constructor(builder: string, field: string, message: string);
|
|
5
|
+
}
|
|
6
|
+
export declare function assertStringLength(builder: string, field: string, value: string, min: number, max: number): void;
|
|
7
|
+
export declare function assertRange(builder: string, field: string, value: number, min: number, max: number): void;
|
|
8
|
+
export declare function assertDefined<T>(builder: string, field: string, value: T | null | undefined): T;
|
|
9
|
+
export declare function assertSelectMenuConstraints(builder: string, data: {
|
|
10
|
+
customId?: string;
|
|
11
|
+
placeholder?: string;
|
|
12
|
+
minValues?: number;
|
|
13
|
+
maxValues?: number;
|
|
14
|
+
}): string;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export class ValidationError extends Error {
|
|
2
|
+
builder;
|
|
3
|
+
field;
|
|
4
|
+
constructor(builder, field, message) {
|
|
5
|
+
super(`[${builder}] ${field}: ${message}`);
|
|
6
|
+
this.builder = builder;
|
|
7
|
+
this.field = field;
|
|
8
|
+
this.name = 'ValidationError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function assertStringLength(builder, field, value, min, max) {
|
|
12
|
+
if (value.length < min || value.length > max) {
|
|
13
|
+
throw new ValidationError(builder, field, `expected length between ${min} and ${max}, got ${value.length}`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export function assertRange(builder, field, value, min, max) {
|
|
17
|
+
if (!Number.isInteger(value) || value < min || value > max) {
|
|
18
|
+
throw new ValidationError(builder, field, `expected integer between ${min} and ${max}, got ${value}`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export function assertDefined(builder, field, value) {
|
|
22
|
+
if (value === undefined || value === null) {
|
|
23
|
+
throw new ValidationError(builder, field, 'is required');
|
|
24
|
+
}
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
export function assertSelectMenuConstraints(builder, data) {
|
|
28
|
+
const customId = assertDefined(builder, 'custom_id', data.customId);
|
|
29
|
+
assertStringLength(builder, 'custom_id', customId, 1, 100);
|
|
30
|
+
if (data.placeholder)
|
|
31
|
+
assertStringLength(builder, 'placeholder', data.placeholder, 1, 150);
|
|
32
|
+
if (data.minValues !== undefined)
|
|
33
|
+
assertRange(builder, 'min_values', data.minValues, 0, 25);
|
|
34
|
+
if (data.maxValues !== undefined)
|
|
35
|
+
assertRange(builder, 'max_values', data.maxValues, 1, 25);
|
|
36
|
+
if (data.minValues !== undefined && data.maxValues !== undefined && data.minValues > data.maxValues) {
|
|
37
|
+
throw new ValidationError(builder, 'min_values', 'cannot exceed max_values');
|
|
38
|
+
}
|
|
39
|
+
return customId;
|
|
40
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { type APIInteractionResponse, type APIApplicationCommandInteractionDataOption, type APIChatInputApplicationCommandInteraction, type APIInteractionDataResolved, type APIInteractionDataResolvedChannel, type APIInteractionDataResolvedGuildMember, type APIModalInteractionResponse, type APIModalInteractionResponseCallbackData, type APIInteractionResponseChannelMessageWithSource, type APIInteractionResponseDeferredChannelMessageWithSource, type APIInteractionResponseUpdateMessage, type APIRole, type APIUser, type APIAttachment } from "discord-api-types/v10";
|
|
2
|
+
import { DeferReplyOptions, InteractionMessageData } from "./interactionMessageHelpers.js";
|
|
3
|
+
export type ResolvedUserOption = {
|
|
4
|
+
user: APIUser;
|
|
5
|
+
member?: APIInteractionDataResolvedGuildMember;
|
|
6
|
+
};
|
|
7
|
+
export type MentionableOption = {
|
|
8
|
+
type: "user";
|
|
9
|
+
value: ResolvedUserOption;
|
|
10
|
+
} | {
|
|
11
|
+
type: "role";
|
|
12
|
+
value: APIRole;
|
|
13
|
+
};
|
|
14
|
+
export declare class CommandInteractionOptionResolver {
|
|
15
|
+
readonly raw: ReadonlyArray<APIApplicationCommandInteractionDataOption>;
|
|
16
|
+
private readonly resolved;
|
|
17
|
+
private readonly focusedOptions;
|
|
18
|
+
private readonly subcommand;
|
|
19
|
+
private readonly subcommandGroup;
|
|
20
|
+
constructor(options: APIApplicationCommandInteractionDataOption[] | undefined, resolved: APIInteractionDataResolved | undefined);
|
|
21
|
+
getSubcommand(required?: boolean): string | null;
|
|
22
|
+
getSubcommandGroup(required?: boolean): string | null;
|
|
23
|
+
getString(name: string, required?: boolean): string | null;
|
|
24
|
+
getInteger(name: string, required?: boolean): number | null;
|
|
25
|
+
getNumber(name: string, required?: boolean): number | null;
|
|
26
|
+
getBoolean(name: string, required?: boolean): boolean | null;
|
|
27
|
+
getUser(name: string, required?: boolean): ResolvedUserOption | null;
|
|
28
|
+
getRole(name: string, required?: boolean): APIRole | null;
|
|
29
|
+
getChannel(name: string, required?: boolean): APIInteractionDataResolvedChannel | null;
|
|
30
|
+
getAttachment(name: string, required?: boolean): APIAttachment | null;
|
|
31
|
+
getMentionable(name: string, required?: boolean): MentionableOption | null;
|
|
32
|
+
getRawOption(name: string): APIApplicationCommandInteractionDataOption | null;
|
|
33
|
+
private extractOption;
|
|
34
|
+
private resolveAttachment;
|
|
35
|
+
private resolveChannel;
|
|
36
|
+
private resolveRole;
|
|
37
|
+
private resolveUser;
|
|
38
|
+
private getResolvedRecord;
|
|
39
|
+
}
|
|
40
|
+
export interface CommandInteraction extends Omit<APIChatInputApplicationCommandInteraction, "data"> {
|
|
41
|
+
data: Omit<APIChatInputApplicationCommandInteraction["data"], "options"> & {
|
|
42
|
+
options: CommandInteractionOptionResolver;
|
|
43
|
+
};
|
|
44
|
+
options: CommandInteractionOptionResolver;
|
|
45
|
+
getResponse(): APIInteractionResponse | null;
|
|
46
|
+
reply(data: InteractionMessageData): Promise<APIInteractionResponseChannelMessageWithSource>;
|
|
47
|
+
edit(data?: InteractionMessageData): APIInteractionResponseUpdateMessage;
|
|
48
|
+
followUp(data: InteractionMessageData): Promise<void>;
|
|
49
|
+
editReply(data?: InteractionMessageData): Promise<APIInteractionResponseChannelMessageWithSource | APIInteractionResponseUpdateMessage>;
|
|
50
|
+
deferReply(options?: DeferReplyOptions): APIInteractionResponseDeferredChannelMessageWithSource;
|
|
51
|
+
showModal(data: APIModalInteractionResponseCallbackData | {
|
|
52
|
+
toJSON(): APIModalInteractionResponseCallbackData;
|
|
53
|
+
}): APIModalInteractionResponse;
|
|
54
|
+
withTimeoutProtection<T>(operation: () => Promise<T>, deferOptions?: DeferReplyOptions): Promise<T>;
|
|
55
|
+
canRespond?(interactionId: string): boolean;
|
|
56
|
+
trackResponse?(interactionId: string, token: string, state: 'responded' | 'deferred'): void;
|
|
57
|
+
onAck?(response: APIInteractionResponse): void;
|
|
58
|
+
sendFollowUp?(token: string, response: APIInteractionResponse, messageId?: string): Promise<void>;
|
|
59
|
+
}
|
|
60
|
+
export declare function createCommandInteraction(interaction: APIChatInputApplicationCommandInteraction, helpers?: {
|
|
61
|
+
canRespond?: (interactionId: string) => boolean;
|
|
62
|
+
trackResponse?: (interactionId: string, token: string, state: 'responded' | 'deferred') => void;
|
|
63
|
+
logTiming?: (interactionId: string, operation: string, startTime: number, success: boolean) => void;
|
|
64
|
+
onAck?: (response: APIInteractionResponse) => void;
|
|
65
|
+
sendFollowUp?: (token: string, response: APIInteractionResponse, messageId?: string) => Promise<void>;
|
|
66
|
+
}): CommandInteraction;
|
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
import { ApplicationCommandOptionType, InteractionResponseType, } from "discord-api-types/v10";
|
|
2
|
+
import { normaliseInteractionMessageData, normaliseMessageFlags, } from "./interactionMessageHelpers.js";
|
|
3
|
+
const OPTION_TYPE_LABEL = {
|
|
4
|
+
[ApplicationCommandOptionType.Subcommand]: "subcommand",
|
|
5
|
+
[ApplicationCommandOptionType.SubcommandGroup]: "subcommand group",
|
|
6
|
+
[ApplicationCommandOptionType.String]: "string",
|
|
7
|
+
[ApplicationCommandOptionType.Integer]: "integer",
|
|
8
|
+
[ApplicationCommandOptionType.Boolean]: "boolean",
|
|
9
|
+
[ApplicationCommandOptionType.User]: "user",
|
|
10
|
+
[ApplicationCommandOptionType.Channel]: "channel",
|
|
11
|
+
[ApplicationCommandOptionType.Role]: "role",
|
|
12
|
+
[ApplicationCommandOptionType.Mentionable]: "mentionable",
|
|
13
|
+
[ApplicationCommandOptionType.Number]: "number",
|
|
14
|
+
[ApplicationCommandOptionType.Attachment]: "attachment",
|
|
15
|
+
};
|
|
16
|
+
function isSubcommandOption(option) {
|
|
17
|
+
return option.type === ApplicationCommandOptionType.Subcommand;
|
|
18
|
+
}
|
|
19
|
+
function isSubcommandGroupOption(option) {
|
|
20
|
+
return option.type === ApplicationCommandOptionType.SubcommandGroup;
|
|
21
|
+
}
|
|
22
|
+
function resolveFocusedOptions(options) {
|
|
23
|
+
if (!options || options.length === 0) {
|
|
24
|
+
return { subcommandGroup: null, subcommand: null, options: [] };
|
|
25
|
+
}
|
|
26
|
+
const [firstOption] = options;
|
|
27
|
+
if (isSubcommandGroupOption(firstOption)) {
|
|
28
|
+
const [firstSubcommand] = firstOption.options ?? [];
|
|
29
|
+
if (firstSubcommand && isSubcommandOption(firstSubcommand)) {
|
|
30
|
+
return {
|
|
31
|
+
subcommandGroup: firstOption.name,
|
|
32
|
+
subcommand: firstSubcommand.name,
|
|
33
|
+
options: firstSubcommand.options ?? [],
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
subcommandGroup: firstOption.name,
|
|
38
|
+
subcommand: null,
|
|
39
|
+
options: [],
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
if (isSubcommandOption(firstOption)) {
|
|
43
|
+
return {
|
|
44
|
+
subcommandGroup: null,
|
|
45
|
+
subcommand: firstOption.name,
|
|
46
|
+
options: firstOption.options ?? [],
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
subcommandGroup: null,
|
|
51
|
+
subcommand: null,
|
|
52
|
+
options: options,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
export class CommandInteractionOptionResolver {
|
|
56
|
+
raw;
|
|
57
|
+
resolved;
|
|
58
|
+
focusedOptions;
|
|
59
|
+
subcommand;
|
|
60
|
+
subcommandGroup;
|
|
61
|
+
constructor(options, resolved) {
|
|
62
|
+
this.raw = Object.freeze([...(options ?? [])]);
|
|
63
|
+
this.resolved = resolved;
|
|
64
|
+
const focused = resolveFocusedOptions(options);
|
|
65
|
+
this.focusedOptions = focused.options;
|
|
66
|
+
this.subcommand = focused.subcommand;
|
|
67
|
+
this.subcommandGroup = focused.subcommandGroup;
|
|
68
|
+
}
|
|
69
|
+
getSubcommand(required = true) {
|
|
70
|
+
if (this.subcommand) {
|
|
71
|
+
return this.subcommand;
|
|
72
|
+
}
|
|
73
|
+
if (required) {
|
|
74
|
+
throw new Error("Expected a subcommand to be present on this interaction");
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
getSubcommandGroup(required = true) {
|
|
79
|
+
if (this.subcommandGroup) {
|
|
80
|
+
return this.subcommandGroup;
|
|
81
|
+
}
|
|
82
|
+
if (required) {
|
|
83
|
+
throw new Error("Expected a subcommand group to be present on this interaction");
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
getString(name, required = false) {
|
|
88
|
+
const option = this.extractOption(name, ApplicationCommandOptionType.String, required);
|
|
89
|
+
return option ? option.value : null;
|
|
90
|
+
}
|
|
91
|
+
getInteger(name, required = false) {
|
|
92
|
+
const option = this.extractOption(name, ApplicationCommandOptionType.Integer, required);
|
|
93
|
+
return option ? option.value : null;
|
|
94
|
+
}
|
|
95
|
+
getNumber(name, required = false) {
|
|
96
|
+
const option = this.extractOption(name, ApplicationCommandOptionType.Number, required);
|
|
97
|
+
return option ? option.value : null;
|
|
98
|
+
}
|
|
99
|
+
getBoolean(name, required = false) {
|
|
100
|
+
const option = this.extractOption(name, ApplicationCommandOptionType.Boolean, required);
|
|
101
|
+
return option ? option.value : null;
|
|
102
|
+
}
|
|
103
|
+
getUser(name, required = false) {
|
|
104
|
+
const option = this.extractOption(name, ApplicationCommandOptionType.User, required);
|
|
105
|
+
if (!option) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
const resolvedUser = this.resolveUser(option.value);
|
|
109
|
+
if (!resolvedUser) {
|
|
110
|
+
if (required) {
|
|
111
|
+
throw new Error(`Resolved data for user option "${name}" is missing`);
|
|
112
|
+
}
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
return resolvedUser;
|
|
116
|
+
}
|
|
117
|
+
getRole(name, required = false) {
|
|
118
|
+
const option = this.extractOption(name, ApplicationCommandOptionType.Role, required);
|
|
119
|
+
if (!option) {
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
const role = this.resolveRole(option.value);
|
|
123
|
+
if (!role && required) {
|
|
124
|
+
throw new Error(`Resolved data for role option "${name}" is missing`);
|
|
125
|
+
}
|
|
126
|
+
return role ?? null;
|
|
127
|
+
}
|
|
128
|
+
getChannel(name, required = false) {
|
|
129
|
+
const option = this.extractOption(name, ApplicationCommandOptionType.Channel, required);
|
|
130
|
+
if (!option) {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
const channel = this.resolveChannel(option.value);
|
|
134
|
+
if (!channel && required) {
|
|
135
|
+
throw new Error(`Resolved data for channel option "${name}" is missing`);
|
|
136
|
+
}
|
|
137
|
+
return channel ?? null;
|
|
138
|
+
}
|
|
139
|
+
getAttachment(name, required = false) {
|
|
140
|
+
const option = this.extractOption(name, ApplicationCommandOptionType.Attachment, required);
|
|
141
|
+
if (!option) {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
const attachment = this.resolveAttachment(option.value);
|
|
145
|
+
if (!attachment && required) {
|
|
146
|
+
throw new Error(`Resolved data for attachment option "${name}" is missing`);
|
|
147
|
+
}
|
|
148
|
+
return attachment ?? null;
|
|
149
|
+
}
|
|
150
|
+
getMentionable(name, required = false) {
|
|
151
|
+
const option = this.extractOption(name, ApplicationCommandOptionType.Mentionable, required);
|
|
152
|
+
if (!option) {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
const role = this.resolveRole(option.value);
|
|
156
|
+
if (role) {
|
|
157
|
+
return { type: "role", value: role };
|
|
158
|
+
}
|
|
159
|
+
const user = this.resolveUser(option.value);
|
|
160
|
+
if (user) {
|
|
161
|
+
return { type: "user", value: user };
|
|
162
|
+
}
|
|
163
|
+
if (required) {
|
|
164
|
+
throw new Error(`Resolved data for mentionable option "${name}" is missing`);
|
|
165
|
+
}
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
getRawOption(name) {
|
|
169
|
+
return (this.focusedOptions.find((option) => option.name === name) ?? null);
|
|
170
|
+
}
|
|
171
|
+
extractOption(name, type, required) {
|
|
172
|
+
const option = this.focusedOptions.find((candidate) => candidate.name === name);
|
|
173
|
+
if (!option) {
|
|
174
|
+
if (required) {
|
|
175
|
+
throw new Error(`Required ${OPTION_TYPE_LABEL[type]} option "${name}" is missing`);
|
|
176
|
+
}
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
if (option.type !== type) {
|
|
180
|
+
throw new Error(`Option "${name}" is a ${OPTION_TYPE_LABEL[option.type]}, expected ${OPTION_TYPE_LABEL[type]}`);
|
|
181
|
+
}
|
|
182
|
+
return option;
|
|
183
|
+
}
|
|
184
|
+
resolveAttachment(id) {
|
|
185
|
+
return this.getResolvedRecord(this.resolved?.attachments, id);
|
|
186
|
+
}
|
|
187
|
+
resolveChannel(id) {
|
|
188
|
+
return this.getResolvedRecord(this.resolved?.channels, id);
|
|
189
|
+
}
|
|
190
|
+
resolveRole(id) {
|
|
191
|
+
return this.getResolvedRecord(this.resolved?.roles, id);
|
|
192
|
+
}
|
|
193
|
+
resolveUser(id) {
|
|
194
|
+
const user = this.getResolvedRecord(this.resolved?.users, id);
|
|
195
|
+
if (!user) {
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
const member = this.getResolvedRecord(this.resolved?.members, id);
|
|
199
|
+
return { user, member: member ?? undefined };
|
|
200
|
+
}
|
|
201
|
+
getResolvedRecord(records, id) {
|
|
202
|
+
return records?.[id];
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
export function createCommandInteraction(interaction, helpers) {
|
|
206
|
+
const options = new CommandInteractionOptionResolver(interaction.data.options, interaction.data.resolved);
|
|
207
|
+
let capturedResponse = null;
|
|
208
|
+
let isDeferred = false;
|
|
209
|
+
let hasResponded = false;
|
|
210
|
+
const captureResponse = (response) => {
|
|
211
|
+
capturedResponse = response;
|
|
212
|
+
return response;
|
|
213
|
+
};
|
|
214
|
+
function createMessageResponse(type, data) {
|
|
215
|
+
const normalisedData = normaliseInteractionMessageData(data);
|
|
216
|
+
if (type === InteractionResponseType.ChannelMessageWithSource) {
|
|
217
|
+
if (!normalisedData) {
|
|
218
|
+
throw new Error("[MiniInteraction] Channel message responses require data to be provided.");
|
|
219
|
+
}
|
|
220
|
+
return captureResponse({
|
|
221
|
+
type,
|
|
222
|
+
data: normalisedData,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
if (normalisedData) {
|
|
226
|
+
return captureResponse({ type, data: normalisedData });
|
|
227
|
+
}
|
|
228
|
+
return captureResponse({ type });
|
|
229
|
+
}
|
|
230
|
+
const createDeferredResponse = (data) => {
|
|
231
|
+
if (!data) {
|
|
232
|
+
return captureResponse({
|
|
233
|
+
type: InteractionResponseType.DeferredChannelMessageWithSource,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
return captureResponse({
|
|
237
|
+
type: InteractionResponseType.DeferredChannelMessageWithSource,
|
|
238
|
+
data: { flags: normaliseMessageFlags(data.flags) },
|
|
239
|
+
});
|
|
240
|
+
};
|
|
241
|
+
const commandInteraction = {
|
|
242
|
+
...interaction,
|
|
243
|
+
data: {
|
|
244
|
+
...interaction.data,
|
|
245
|
+
options,
|
|
246
|
+
},
|
|
247
|
+
options,
|
|
248
|
+
getResponse() {
|
|
249
|
+
return capturedResponse;
|
|
250
|
+
},
|
|
251
|
+
async reply(data) {
|
|
252
|
+
if (this.canRespond && !this.canRespond(this.id)) {
|
|
253
|
+
throw new Error('Interaction cannot respond: already responded or expired');
|
|
254
|
+
}
|
|
255
|
+
const response = createMessageResponse(InteractionResponseType.ChannelMessageWithSource, data);
|
|
256
|
+
if (isDeferred && this.sendFollowUp) {
|
|
257
|
+
await this.sendFollowUp(this.token, response, '@original');
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
this.onAck?.(response);
|
|
261
|
+
}
|
|
262
|
+
this.trackResponse?.(this.id, this.token, 'responded');
|
|
263
|
+
hasResponded = true;
|
|
264
|
+
return response;
|
|
265
|
+
},
|
|
266
|
+
async followUp(data) {
|
|
267
|
+
const normalisedData = normaliseInteractionMessageData(data);
|
|
268
|
+
if (!normalisedData) {
|
|
269
|
+
throw new Error('[MiniInteraction] followUp requires data');
|
|
270
|
+
}
|
|
271
|
+
const response = {
|
|
272
|
+
type: InteractionResponseType.ChannelMessageWithSource,
|
|
273
|
+
data: normalisedData,
|
|
274
|
+
};
|
|
275
|
+
if (this.sendFollowUp) {
|
|
276
|
+
await this.sendFollowUp(this.token, response, '');
|
|
277
|
+
}
|
|
278
|
+
this.trackResponse?.(this.id, this.token, 'responded');
|
|
279
|
+
hasResponded = true;
|
|
280
|
+
},
|
|
281
|
+
edit(data) {
|
|
282
|
+
return createMessageResponse(InteractionResponseType.UpdateMessage, data);
|
|
283
|
+
},
|
|
284
|
+
async editReply(data) {
|
|
285
|
+
if (this.canRespond && !this.canRespond(this.id)) {
|
|
286
|
+
throw new Error('Interaction cannot edit reply: expired');
|
|
287
|
+
}
|
|
288
|
+
const normalizedData = normaliseInteractionMessageData(data);
|
|
289
|
+
if (!normalizedData) {
|
|
290
|
+
throw new Error('[MiniInteraction] editReply requires data');
|
|
291
|
+
}
|
|
292
|
+
const response = {
|
|
293
|
+
type: InteractionResponseType.ChannelMessageWithSource,
|
|
294
|
+
data: normalizedData,
|
|
295
|
+
};
|
|
296
|
+
if (this.sendFollowUp && (isDeferred || hasResponded)) {
|
|
297
|
+
await this.sendFollowUp(this.token, response, '@original');
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
captureResponse(response);
|
|
301
|
+
this.onAck?.(response);
|
|
302
|
+
}
|
|
303
|
+
this.trackResponse?.(this.id, this.token, 'responded');
|
|
304
|
+
hasResponded = true;
|
|
305
|
+
return response;
|
|
306
|
+
},
|
|
307
|
+
deferReply(options) {
|
|
308
|
+
if (this.canRespond && !this.canRespond(this.id)) {
|
|
309
|
+
throw new Error('Interaction cannot defer: already responded or expired');
|
|
310
|
+
}
|
|
311
|
+
const response = createDeferredResponse(options?.flags !== undefined
|
|
312
|
+
? { flags: options.flags }
|
|
313
|
+
: undefined);
|
|
314
|
+
this.trackResponse?.(this.id, this.token, 'deferred');
|
|
315
|
+
isDeferred = true;
|
|
316
|
+
this.onAck?.(response);
|
|
317
|
+
return response;
|
|
318
|
+
},
|
|
319
|
+
showModal(data) {
|
|
320
|
+
const resolvedData = typeof data === "object" &&
|
|
321
|
+
"toJSON" in data &&
|
|
322
|
+
typeof data.toJSON === "function"
|
|
323
|
+
? data.toJSON()
|
|
324
|
+
: data;
|
|
325
|
+
const response = captureResponse({
|
|
326
|
+
type: InteractionResponseType.Modal,
|
|
327
|
+
data: resolvedData,
|
|
328
|
+
});
|
|
329
|
+
this.onAck?.(response);
|
|
330
|
+
return response;
|
|
331
|
+
},
|
|
332
|
+
async withTimeoutProtection(operation, deferOptions) {
|
|
333
|
+
const startTime = Date.now();
|
|
334
|
+
let deferred = false;
|
|
335
|
+
const deferTimer = setTimeout(async () => {
|
|
336
|
+
if (!deferred) {
|
|
337
|
+
console.warn("[MiniInteraction] Auto-deferring interaction due to slow operation. " +
|
|
338
|
+
"Consider using deferReply() explicitly for better user experience.");
|
|
339
|
+
this.deferReply(deferOptions);
|
|
340
|
+
deferred = true;
|
|
341
|
+
}
|
|
342
|
+
}, 2500);
|
|
343
|
+
try {
|
|
344
|
+
const result = await operation();
|
|
345
|
+
clearTimeout(deferTimer);
|
|
346
|
+
const elapsed = Date.now() - startTime;
|
|
347
|
+
if (elapsed > 2000 && !deferred) {
|
|
348
|
+
console.warn(`[MiniInteraction] Operation completed in ${elapsed}ms. ` +
|
|
349
|
+
"Consider using deferReply() for operations > 2 seconds.");
|
|
350
|
+
}
|
|
351
|
+
return result;
|
|
352
|
+
}
|
|
353
|
+
catch (error) {
|
|
354
|
+
clearTimeout(deferTimer);
|
|
355
|
+
throw error;
|
|
356
|
+
}
|
|
357
|
+
},
|
|
358
|
+
canRespond: helpers?.canRespond,
|
|
359
|
+
trackResponse: helpers?.trackResponse,
|
|
360
|
+
onAck: helpers?.onAck,
|
|
361
|
+
sendFollowUp: helpers?.sendFollowUp,
|
|
362
|
+
};
|
|
363
|
+
return commandInteraction;
|
|
364
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { type APIInteractionResponse, type APIInteractionResponseChannelMessageWithSource, type APIInteractionResponseDeferredChannelMessageWithSource, type APIInteractionResponseUpdateMessage, type APIMessage, type APIMessageApplicationCommandInteraction, type APIModalInteractionResponse, type APIModalInteractionResponseCallbackData, type APIPrimaryEntryPointCommandInteraction, type APIUser, type APIUserApplicationCommandInteraction } from "discord-api-types/v10";
|
|
2
|
+
import { DeferReplyOptions, InteractionMessageData } from "./interactionMessageHelpers.js";
|
|
3
|
+
/**
|
|
4
|
+
* Base helper methods for context menu interactions.
|
|
5
|
+
*/
|
|
6
|
+
export type ContextMenuInteractionHelpers = {
|
|
7
|
+
getResponse: () => APIInteractionResponse | null;
|
|
8
|
+
reply: (data: InteractionMessageData) => Promise<APIInteractionResponseChannelMessageWithSource>;
|
|
9
|
+
followUp: (data: InteractionMessageData) => Promise<APIInteractionResponseChannelMessageWithSource>;
|
|
10
|
+
editReply: (data?: InteractionMessageData) => Promise<APIInteractionResponseUpdateMessage>;
|
|
11
|
+
deferReply: (options?: DeferReplyOptions) => APIInteractionResponseDeferredChannelMessageWithSource;
|
|
12
|
+
showModal: (data: APIModalInteractionResponseCallbackData | {
|
|
13
|
+
toJSON(): APIModalInteractionResponseCallbackData;
|
|
14
|
+
}) => APIModalInteractionResponse;
|
|
15
|
+
onAck?: (response: APIInteractionResponse) => void;
|
|
16
|
+
sendFollowUp?: (token: string, response: APIInteractionResponse, messageId?: string) => Promise<void>;
|
|
17
|
+
canRespond?: (interactionId: string) => boolean;
|
|
18
|
+
trackResponse?: (interactionId: string, token: string, state: 'responded' | 'deferred') => void;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* User context menu interaction with helper methods.
|
|
22
|
+
*/
|
|
23
|
+
export type UserContextMenuInteraction = APIUserApplicationCommandInteraction & ContextMenuInteractionHelpers & {
|
|
24
|
+
/** Resolved user targeted by this user context menu command. */
|
|
25
|
+
targetUser?: APIUser;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Message context menu interaction with helper methods.
|
|
29
|
+
*/
|
|
30
|
+
export type MessageContextMenuInteraction = APIMessageApplicationCommandInteraction & ContextMenuInteractionHelpers & {
|
|
31
|
+
/** Resolved message targeted by this message context menu command. */
|
|
32
|
+
targetMessage?: APIMessage;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Primary entry point interaction with helper methods.
|
|
36
|
+
*/
|
|
37
|
+
export type AppCommandInteraction = APIPrimaryEntryPointCommandInteraction & ContextMenuInteractionHelpers;
|
|
38
|
+
/**
|
|
39
|
+
* Wraps a raw user context menu interaction with helper methods.
|
|
40
|
+
*
|
|
41
|
+
* @param interaction - The raw user context menu interaction payload from Discord.
|
|
42
|
+
* @param helpers - Optional callback helpers.
|
|
43
|
+
* @returns A helper-augmented interaction object.
|
|
44
|
+
*/
|
|
45
|
+
export declare function createUserContextMenuInteraction(interaction: APIUserApplicationCommandInteraction, helpers?: {
|
|
46
|
+
onAck?: (response: APIInteractionResponse) => void;
|
|
47
|
+
sendFollowUp?: (token: string, response: APIInteractionResponse, messageId?: string) => Promise<void>;
|
|
48
|
+
canRespond?: (interactionId: string) => boolean;
|
|
49
|
+
trackResponse?: (interactionId: string, token: string, state: 'responded' | 'deferred') => void;
|
|
50
|
+
}): UserContextMenuInteraction;
|
|
51
|
+
/**
|
|
52
|
+
* Wraps a raw message context menu interaction with helper methods.
|
|
53
|
+
*
|
|
54
|
+
* @param interaction - The raw message context menu interaction payload from Discord.
|
|
55
|
+
* @param helpers - Optional callback helpers.
|
|
56
|
+
* @returns A helper-augmented interaction object.
|
|
57
|
+
*/
|
|
58
|
+
export declare function createMessageContextMenuInteraction(interaction: APIMessageApplicationCommandInteraction, helpers?: {
|
|
59
|
+
onAck?: (response: APIInteractionResponse) => void;
|
|
60
|
+
sendFollowUp?: (token: string, response: APIInteractionResponse, messageId?: string) => Promise<void>;
|
|
61
|
+
canRespond?: (interactionId: string) => boolean;
|
|
62
|
+
trackResponse?: (interactionId: string, token: string, state: 'responded' | 'deferred') => void;
|
|
63
|
+
}): MessageContextMenuInteraction;
|
|
64
|
+
/**
|
|
65
|
+
* Wraps a raw primary entry point interaction with helper methods.
|
|
66
|
+
*
|
|
67
|
+
* @param interaction - The raw primary entry point interaction payload from Discord.
|
|
68
|
+
* @param helpers - Optional callback helpers.
|
|
69
|
+
* @returns A helper-augmented interaction object.
|
|
70
|
+
*/
|
|
71
|
+
export declare function createAppCommandInteraction(interaction: APIPrimaryEntryPointCommandInteraction, helpers?: {
|
|
72
|
+
onAck?: (response: APIInteractionResponse) => void;
|
|
73
|
+
sendFollowUp?: (token: string, response: APIInteractionResponse, messageId?: string) => Promise<void>;
|
|
74
|
+
canRespond?: (interactionId: string) => boolean;
|
|
75
|
+
trackResponse?: (interactionId: string, token: string, state: 'responded' | 'deferred') => void;
|
|
76
|
+
}): AppCommandInteraction;
|