@minesa-org/mini-interaction 0.0.12 → 0.0.14

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.
@@ -2,7 +2,7 @@ import type { IncomingMessage, ServerResponse } from "node:http";
2
2
  import { APIInteractionResponse, RESTPostAPIChatInputApplicationCommandsJSONBody, RESTPostAPIContextMenuApplicationCommandsJSONBody } from "discord-api-types/v10";
3
3
  import type { MiniInteractionCommand } from "../types/Commands.js";
4
4
  import { RoleConnectionMetadataTypes } from "../types/RoleConnectionMetadataTypes.js";
5
- import { type MessageComponentInteraction } from "../utils/MessageComponentInteraction.js";
5
+ import { type MessageComponentInteraction, type ButtonInteraction, type StringSelectInteraction, type RoleSelectInteraction, type UserSelectInteraction, type ChannelSelectInteraction, type MentionableSelectInteraction } from "../utils/MessageComponentInteraction.js";
6
6
  import { type ModalSubmitInteraction } from "../utils/ModalSubmitInteraction.js";
7
7
  /** Configuration parameters for the MiniInteraction client. */
8
8
  export type MiniInteractionOptions = {
@@ -35,12 +35,24 @@ export type MiniInteractionHandlerResult = {
35
35
  error: string;
36
36
  };
37
37
  };
38
- /** Handler signature invoked for Discord message component interactions. */
38
+ /** Handler signature invoked for Discord button interactions. */
39
+ export type MiniInteractionButtonHandler = (interaction: ButtonInteraction) => Promise<APIInteractionResponse | void> | APIInteractionResponse | void;
40
+ /** Handler signature invoked for Discord string select menu interactions. */
41
+ export type MiniInteractionStringSelectHandler = (interaction: StringSelectInteraction) => Promise<APIInteractionResponse | void> | APIInteractionResponse | void;
42
+ /** Handler signature invoked for Discord role select menu interactions. */
43
+ export type MiniInteractionRoleSelectHandler = (interaction: RoleSelectInteraction) => Promise<APIInteractionResponse | void> | APIInteractionResponse | void;
44
+ /** Handler signature invoked for Discord user select menu interactions. */
45
+ export type MiniInteractionUserSelectHandler = (interaction: UserSelectInteraction) => Promise<APIInteractionResponse | void> | APIInteractionResponse | void;
46
+ /** Handler signature invoked for Discord channel select menu interactions. */
47
+ export type MiniInteractionChannelSelectHandler = (interaction: ChannelSelectInteraction) => Promise<APIInteractionResponse | void> | APIInteractionResponse | void;
48
+ /** Handler signature invoked for Discord mentionable select menu interactions. */
49
+ export type MiniInteractionMentionableSelectHandler = (interaction: MentionableSelectInteraction) => Promise<APIInteractionResponse | void> | APIInteractionResponse | void;
50
+ /** Handler signature invoked for Discord message component interactions (generic). */
39
51
  export type MiniInteractionComponentHandler = (interaction: MessageComponentInteraction) => Promise<APIInteractionResponse | void> | APIInteractionResponse | void;
40
52
  /** Handler signature invoked for Discord modal submit interactions. */
41
53
  export type MiniInteractionModalHandler = (interaction: ModalSubmitInteraction) => Promise<APIInteractionResponse | void> | APIInteractionResponse | void;
42
- /** Unified handler signature that accepts both component and modal interactions. */
43
- export type MiniInteractionHandler = MiniInteractionComponentHandler | MiniInteractionModalHandler;
54
+ /** Unified handler signature that accepts any component or modal interaction. */
55
+ export type MiniInteractionHandler = MiniInteractionButtonHandler | MiniInteractionStringSelectHandler | MiniInteractionRoleSelectHandler | MiniInteractionUserSelectHandler | MiniInteractionChannelSelectHandler | MiniInteractionMentionableSelectHandler | MiniInteractionComponentHandler | MiniInteractionModalHandler;
44
56
  /**
45
57
  * Structure describing a component or modal handler mapped to a custom id.
46
58
  * When auto-loading from the components directory:
@@ -8,6 +8,7 @@ import { DISCORD_BASE_URL } from "../utils/constants.js";
8
8
  import { createCommandInteraction } from "../utils/CommandInteractionOptions.js";
9
9
  import { createMessageComponentInteraction, } from "../utils/MessageComponentInteraction.js";
10
10
  import { createModalSubmitInteraction, } from "../utils/ModalSubmitInteraction.js";
11
+ import { createUserContextMenuInteraction, createMessageContextMenuInteraction, } from "../utils/ContextMenuInteraction.js";
11
12
  /** File extensions that are treated as loadable modules when auto-loading. */
12
13
  const SUPPORTED_MODULE_EXTENSIONS = new Set([
13
14
  ".js",
@@ -842,8 +843,22 @@ export class MiniInteraction {
842
843
  resolvedResponse =
843
844
  response ?? interactionWithHelpers.getResponse();
844
845
  }
846
+ else if (commandInteraction.data.type === ApplicationCommandType.User) {
847
+ // User context menu command
848
+ const interactionWithHelpers = createUserContextMenuInteraction(commandInteraction);
849
+ response = await command.handler(interactionWithHelpers);
850
+ resolvedResponse =
851
+ response ?? interactionWithHelpers.getResponse();
852
+ }
853
+ else if (commandInteraction.data.type === ApplicationCommandType.Message) {
854
+ // Message context menu command
855
+ const interactionWithHelpers = createMessageContextMenuInteraction(commandInteraction);
856
+ response = await command.handler(interactionWithHelpers);
857
+ resolvedResponse =
858
+ response ?? interactionWithHelpers.getResponse();
859
+ }
845
860
  else {
846
- // Context menu commands (User or Message) - pass raw interaction
861
+ // Unknown command type
847
862
  response = await command.handler(commandInteraction);
848
863
  resolvedResponse = response ?? null;
849
864
  }
package/dist/index.d.ts CHANGED
@@ -6,10 +6,11 @@ export { UserCommandBuilder, MessageCommandBuilder, } from "./commands/ContextMe
6
6
  export type { AttachmentOptionBuilder, ChannelOptionBuilder, MentionableOptionBuilder, NumberOptionBuilder, RoleOptionBuilder, StringOptionBuilder, SubcommandBuilder, SubcommandGroupBuilder, UserOptionBuilder, } from "./commands/CommandBuilder.js";
7
7
  export { CommandInteractionOptionResolver, createCommandInteraction, } from "./utils/CommandInteractionOptions.js";
8
8
  export type { CommandInteraction, MentionableOption, ResolvedUserOption, } from "./utils/CommandInteractionOptions.js";
9
+ export type { UserContextMenuInteraction, MessageContextMenuInteraction, } from "./utils/ContextMenuInteraction.js";
9
10
  export type { MiniInteractionFetchHandler, MiniInteractionNodeHandler, MiniInteractionHandlerResult, MiniInteractionRequest, MiniInteractionOptions, } from "./clients/MiniInteraction.js";
10
11
  export type { MiniInteractionCommand, SlashCommandHandler, UserCommandHandler, MessageCommandHandler, CommandHandler, } from "./types/Commands.js";
11
- export type { MiniInteractionComponent, MiniInteractionComponentHandler, MiniInteractionModal, MiniInteractionModalHandler, MiniInteractionHandler, } from "./clients/MiniInteraction.js";
12
- export type { MessageComponentInteraction, ResolvedUserOption as ComponentResolvedUserOption, ResolvedMentionableOption as ComponentResolvedMentionableOption, } from "./utils/MessageComponentInteraction.js";
12
+ export type { MiniInteractionComponent, MiniInteractionButtonHandler, MiniInteractionStringSelectHandler, MiniInteractionRoleSelectHandler, MiniInteractionUserSelectHandler, MiniInteractionChannelSelectHandler, MiniInteractionMentionableSelectHandler, MiniInteractionComponentHandler, MiniInteractionModal, MiniInteractionModalHandler, MiniInteractionHandler, } from "./clients/MiniInteraction.js";
13
+ export type { MessageComponentInteraction, ButtonInteraction, StringSelectInteraction, RoleSelectInteraction, UserSelectInteraction, ChannelSelectInteraction, MentionableSelectInteraction, ResolvedUserOption as ComponentResolvedUserOption, ResolvedMentionableOption as ComponentResolvedMentionableOption, } from "./utils/MessageComponentInteraction.js";
13
14
  export type { ModalSubmitInteraction } from "./utils/ModalSubmitInteraction.js";
14
15
  export { RoleConnectionMetadataTypes } from "./types/RoleConnectionMetadataTypes.js";
15
16
  export { ChannelType } from "./types/ChannelType.js";
@@ -1,12 +1,13 @@
1
- import type { APIInteractionResponse, RESTPostAPIChatInputApplicationCommandsJSONBody, RESTPostAPIContextMenuApplicationCommandsJSONBody, APIUserApplicationCommandInteraction, APIMessageApplicationCommandInteraction } from "discord-api-types/v10";
1
+ import type { APIInteractionResponse, RESTPostAPIChatInputApplicationCommandsJSONBody, RESTPostAPIContextMenuApplicationCommandsJSONBody } from "discord-api-types/v10";
2
2
  import type { CommandInteraction } from "../utils/CommandInteractionOptions.js";
3
+ import type { UserContextMenuInteraction, MessageContextMenuInteraction } from "../utils/ContextMenuInteraction.js";
3
4
  import type { MiniInteractionComponent, MiniInteractionModal } from "../clients/MiniInteraction.js";
4
5
  /** Handler signature for slash command executions within MiniInteraction. */
5
6
  export type SlashCommandHandler = (interaction: CommandInteraction) => Promise<APIInteractionResponse | void> | APIInteractionResponse | void;
6
7
  /** Handler signature for user context menu command executions within MiniInteraction. */
7
- export type UserCommandHandler = (interaction: APIUserApplicationCommandInteraction) => Promise<APIInteractionResponse | void> | APIInteractionResponse | void;
8
+ export type UserCommandHandler = (interaction: UserContextMenuInteraction) => Promise<APIInteractionResponse | void> | APIInteractionResponse | void;
8
9
  /** Handler signature for message context menu command executions within MiniInteraction. */
9
- export type MessageCommandHandler = (interaction: APIMessageApplicationCommandInteraction) => Promise<APIInteractionResponse | void> | APIInteractionResponse | void;
10
+ export type MessageCommandHandler = (interaction: MessageContextMenuInteraction) => Promise<APIInteractionResponse | void> | APIInteractionResponse | void;
10
11
  /** Union of all command handler types. */
11
12
  export type CommandHandler = SlashCommandHandler | UserCommandHandler | MessageCommandHandler;
12
13
  /** Structure representing a slash command definition and its runtime handler. */
@@ -0,0 +1,36 @@
1
+ import { type APIInteractionResponse, type APIInteractionResponseChannelMessageWithSource, type APIInteractionResponseDeferredChannelMessageWithSource, type APIMessageApplicationCommandInteraction, type APIModalInteractionResponse, type APIModalInteractionResponseCallbackData, 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
+ type ContextMenuInteractionHelpers = {
7
+ getResponse: () => APIInteractionResponse | null;
8
+ reply: (data: InteractionMessageData) => APIInteractionResponseChannelMessageWithSource;
9
+ deferReply: (options?: DeferReplyOptions) => APIInteractionResponseDeferredChannelMessageWithSource;
10
+ showModal: (data: APIModalInteractionResponseCallbackData | {
11
+ toJSON(): APIModalInteractionResponseCallbackData;
12
+ }) => APIModalInteractionResponse;
13
+ };
14
+ /**
15
+ * User context menu interaction with helper methods.
16
+ */
17
+ export type UserContextMenuInteraction = APIUserApplicationCommandInteraction & ContextMenuInteractionHelpers;
18
+ /**
19
+ * Message context menu interaction with helper methods.
20
+ */
21
+ export type MessageContextMenuInteraction = APIMessageApplicationCommandInteraction & ContextMenuInteractionHelpers;
22
+ /**
23
+ * Wraps a raw user context menu interaction with helper methods.
24
+ *
25
+ * @param interaction - The raw user context menu interaction payload from Discord.
26
+ * @returns A helper-augmented interaction object.
27
+ */
28
+ export declare function createUserContextMenuInteraction(interaction: APIUserApplicationCommandInteraction): UserContextMenuInteraction;
29
+ /**
30
+ * Wraps a raw message context menu interaction with helper methods.
31
+ *
32
+ * @param interaction - The raw message context menu interaction payload from Discord.
33
+ * @returns A helper-augmented interaction object.
34
+ */
35
+ export declare function createMessageContextMenuInteraction(interaction: APIMessageApplicationCommandInteraction): MessageContextMenuInteraction;
36
+ export {};
@@ -0,0 +1,94 @@
1
+ import { InteractionResponseType, } from "discord-api-types/v10";
2
+ import { normaliseInteractionMessageData, normaliseMessageFlags, } from "./interactionMessageHelpers.js";
3
+ /**
4
+ * Wraps a raw user context menu interaction with helper methods.
5
+ *
6
+ * @param interaction - The raw user context menu interaction payload from Discord.
7
+ * @returns A helper-augmented interaction object.
8
+ */
9
+ export function createUserContextMenuInteraction(interaction) {
10
+ let capturedResponse = null;
11
+ const reply = (data) => {
12
+ const normalised = normaliseInteractionMessageData(data);
13
+ if (!normalised) {
14
+ throw new Error("[MiniInteraction] Channel message responses require data to be provided.");
15
+ }
16
+ const response = {
17
+ type: InteractionResponseType.ChannelMessageWithSource,
18
+ data: normalised,
19
+ };
20
+ capturedResponse = response;
21
+ return response;
22
+ };
23
+ const deferReply = (options = {}) => {
24
+ const flags = normaliseMessageFlags(options.flags);
25
+ const response = {
26
+ type: InteractionResponseType.DeferredChannelMessageWithSource,
27
+ data: flags ? { flags } : undefined,
28
+ };
29
+ capturedResponse = response;
30
+ return response;
31
+ };
32
+ const showModal = (data) => {
33
+ const modalData = typeof data === "object" && "toJSON" in data ? data.toJSON() : data;
34
+ const response = {
35
+ type: InteractionResponseType.Modal,
36
+ data: modalData,
37
+ };
38
+ capturedResponse = response;
39
+ return response;
40
+ };
41
+ const getResponse = () => capturedResponse;
42
+ return Object.assign(interaction, {
43
+ reply,
44
+ deferReply,
45
+ showModal,
46
+ getResponse,
47
+ });
48
+ }
49
+ /**
50
+ * Wraps a raw message context menu interaction with helper methods.
51
+ *
52
+ * @param interaction - The raw message context menu interaction payload from Discord.
53
+ * @returns A helper-augmented interaction object.
54
+ */
55
+ export function createMessageContextMenuInteraction(interaction) {
56
+ let capturedResponse = null;
57
+ const reply = (data) => {
58
+ const normalised = normaliseInteractionMessageData(data);
59
+ if (!normalised) {
60
+ throw new Error("[MiniInteraction] Channel message responses require data to be provided.");
61
+ }
62
+ const response = {
63
+ type: InteractionResponseType.ChannelMessageWithSource,
64
+ data: normalised,
65
+ };
66
+ capturedResponse = response;
67
+ return response;
68
+ };
69
+ const deferReply = (options = {}) => {
70
+ const flags = normaliseMessageFlags(options.flags);
71
+ const response = {
72
+ type: InteractionResponseType.DeferredChannelMessageWithSource,
73
+ data: flags ? { flags } : undefined,
74
+ };
75
+ capturedResponse = response;
76
+ return response;
77
+ };
78
+ const showModal = (data) => {
79
+ const modalData = typeof data === "object" && "toJSON" in data ? data.toJSON() : data;
80
+ const response = {
81
+ type: InteractionResponseType.Modal,
82
+ data: modalData,
83
+ };
84
+ capturedResponse = response;
85
+ return response;
86
+ };
87
+ const getResponse = () => capturedResponse;
88
+ return Object.assign(interaction, {
89
+ reply,
90
+ deferReply,
91
+ showModal,
92
+ getResponse,
93
+ });
94
+ }
@@ -1,4 +1,4 @@
1
- import { type APIInteractionDataResolvedChannel, type APIInteractionDataResolvedGuildMember, type APIInteractionResponse, type APIInteractionResponseChannelMessageWithSource, type APIInteractionResponseDeferredChannelMessageWithSource, type APIInteractionResponseDeferredMessageUpdate, type APIInteractionResponseUpdateMessage, type APIMessageComponentInteraction, type APIModalInteractionResponse, type APIModalInteractionResponseCallbackData, type APIRole, type APIUser } from "discord-api-types/v10";
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 } from "discord-api-types/v10";
2
2
  import { DeferReplyOptions, InteractionMessageData } from "./interactionMessageHelpers.js";
3
3
  /** Resolved user option including optional guild member data. */
4
4
  export type ResolvedUserOption = {
@@ -13,6 +13,66 @@ export type ResolvedMentionableOption = {
13
13
  type: "role";
14
14
  value: APIRole;
15
15
  };
16
+ /**
17
+ * Base helper methods available on all component interactions.
18
+ */
19
+ type BaseComponentInteractionHelpers = {
20
+ getResponse: () => APIInteractionResponse | null;
21
+ reply: (data: InteractionMessageData) => APIInteractionResponseChannelMessageWithSource;
22
+ deferReply: (options?: DeferReplyOptions) => APIInteractionResponseDeferredChannelMessageWithSource;
23
+ update: (data?: InteractionMessageData) => APIInteractionResponseUpdateMessage;
24
+ deferUpdate: () => APIInteractionResponseDeferredMessageUpdate;
25
+ showModal: (data: APIModalInteractionResponseCallbackData | {
26
+ toJSON(): APIModalInteractionResponseCallbackData;
27
+ }) => APIModalInteractionResponse;
28
+ };
29
+ /**
30
+ * Button interaction with helper methods.
31
+ * Buttons don't have values or resolved data.
32
+ */
33
+ export type ButtonInteraction = Omit<APIMessageComponentInteraction, "data"> & {
34
+ data: APIMessageButtonInteractionData;
35
+ } & BaseComponentInteractionHelpers;
36
+ /**
37
+ * String select menu interaction with helper methods.
38
+ */
39
+ export type StringSelectInteraction = Omit<APIMessageComponentInteraction, "data"> & {
40
+ data: APIMessageStringSelectInteractionData;
41
+ values: string[];
42
+ getStringValues: () => string[];
43
+ } & BaseComponentInteractionHelpers;
44
+ /**
45
+ * Role select menu interaction with helper methods.
46
+ */
47
+ export type RoleSelectInteraction = Omit<APIMessageComponentInteraction, "data"> & {
48
+ data: APIMessageRoleSelectInteractionData;
49
+ values: string[];
50
+ getRoles: () => APIRole[];
51
+ } & BaseComponentInteractionHelpers;
52
+ /**
53
+ * User select menu interaction with helper methods.
54
+ */
55
+ export type UserSelectInteraction = Omit<APIMessageComponentInteraction, "data"> & {
56
+ data: APIMessageUserSelectInteractionData;
57
+ values: string[];
58
+ getUsers: () => ResolvedUserOption[];
59
+ } & BaseComponentInteractionHelpers;
60
+ /**
61
+ * Channel select menu interaction with helper methods.
62
+ */
63
+ export type ChannelSelectInteraction = Omit<APIMessageComponentInteraction, "data"> & {
64
+ data: APIMessageChannelSelectInteractionData;
65
+ values: string[];
66
+ getChannels: () => APIInteractionDataResolvedChannel[];
67
+ } & BaseComponentInteractionHelpers;
68
+ /**
69
+ * Mentionable select menu interaction with helper methods.
70
+ */
71
+ export type MentionableSelectInteraction = Omit<APIMessageComponentInteraction, "data"> & {
72
+ data: APIMessageMentionableSelectInteractionData;
73
+ values: string[];
74
+ getMentionables: () => ResolvedMentionableOption[];
75
+ } & BaseComponentInteractionHelpers;
16
76
  /**
17
77
  * Represents a component interaction augmented with helper response methods.
18
78
  *
@@ -67,3 +127,4 @@ export type MessageComponentInteraction = APIMessageComponentInteraction & {
67
127
  * @returns A helper-augmented interaction object.
68
128
  */
69
129
  export declare function createMessageComponentInteraction(interaction: APIMessageComponentInteraction): MessageComponentInteraction;
130
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@minesa-org/mini-interaction",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "description": "Mini interaction, connecting your app with Discord via HTTP-interaction (Vercel support).",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",