@minesa-org/mini-interaction 0.3.9 → 0.3.13

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.
@@ -74,7 +74,8 @@ export class ModalBuilder {
74
74
  componentType === ComponentType.RoleSelect ||
75
75
  componentType === ComponentType.MentionableSelect ||
76
76
  componentType === ComponentType.ChannelSelect ||
77
- componentType === ComponentType.FileUpload) {
77
+ componentType === ComponentType.FileUpload ||
78
+ componentType === ComponentType.Label) {
78
79
  return {
79
80
  type: ComponentType.ActionRow,
80
81
  components: [resolved],
@@ -1,4 +1,4 @@
1
- import { SelectMenuDefaultValueType, type APIChannelSelectComponent, type APISelectMenuDefaultValue } from "discord-api-types/v10";
1
+ import { SelectMenuDefaultValueType, type APIChannelSelectComponent, type APISelectMenuDefaultValue, ChannelType } from "discord-api-types/v10";
2
2
  import type { JSONEncodable } from "./shared.js";
3
3
  /** Shape describing initial modal channel select menu data accepted by the builder. */
4
4
  export type ModalChannelSelectMenuBuilderData = {
@@ -8,6 +8,7 @@ export type ModalChannelSelectMenuBuilderData = {
8
8
  maxValues?: number;
9
9
  disabled?: boolean;
10
10
  required?: boolean;
11
+ channelTypes?: ChannelType[];
11
12
  defaultValues?: APISelectMenuDefaultValue<SelectMenuDefaultValueType.Channel>[];
12
13
  };
13
14
  /** Builder for Discord channel select menu components in modals. */
@@ -45,6 +46,14 @@ export declare class ModalChannelSelectMenuBuilder implements JSONEncodable<APIC
45
46
  * Replaces the default channel selections displayed when the menu renders.
46
47
  */
47
48
  setDefaultValues(defaultValues: Iterable<APISelectMenuDefaultValue<SelectMenuDefaultValueType.Channel>>): this;
49
+ /**
50
+ * Sets the channel types allowed in the select menu.
51
+ */
52
+ setChannelTypes(...channelTypes: ChannelType[]): this;
53
+ /**
54
+ * Adds channel types allowed in the select menu.
55
+ */
56
+ addChannelTypes(...channelTypes: ChannelType[]): this;
48
57
  /**
49
58
  * Serialises the builder into an API compatible channel select menu payload.
50
59
  */
@@ -13,6 +13,7 @@ export class ModalChannelSelectMenuBuilder {
13
13
  maxValues: data.maxValues,
14
14
  disabled: data.disabled,
15
15
  required: data.required,
16
+ channelTypes: data.channelTypes,
16
17
  defaultValues: data.defaultValues
17
18
  ? data.defaultValues.map((value) => ({
18
19
  ...value,
@@ -73,6 +74,21 @@ export class ModalChannelSelectMenuBuilder {
73
74
  }));
74
75
  return this;
75
76
  }
77
+ /**
78
+ * Sets the channel types allowed in the select menu.
79
+ */
80
+ setChannelTypes(...channelTypes) {
81
+ this.data.channelTypes = channelTypes.flat();
82
+ return this;
83
+ }
84
+ /**
85
+ * Adds channel types allowed in the select menu.
86
+ */
87
+ addChannelTypes(...channelTypes) {
88
+ this.data.channelTypes ??= [];
89
+ this.data.channelTypes.push(...channelTypes.flat());
90
+ return this;
91
+ }
76
92
  /**
77
93
  * Serialises the builder into an API compatible channel select menu payload.
78
94
  */
@@ -89,6 +105,7 @@ export class ModalChannelSelectMenuBuilder {
89
105
  max_values: this.data.maxValues,
90
106
  disabled: this.data.disabled,
91
107
  required: this.data.required,
108
+ channel_types: this.data.channelTypes,
92
109
  default_values: this.data.defaultValues?.map((value) => ({
93
110
  ...value,
94
111
  type: SelectMenuDefaultValueType.Channel,
@@ -12,6 +12,7 @@ export type InteractionClientOptions = {
12
12
  commandsDirectory?: string | false;
13
13
  componentsDirectory?: string | false;
14
14
  utilsDirectory?: string | false;
15
+ debug?: boolean;
15
16
  fetchImplementation?: typeof fetch;
16
17
  verifyKeyImplementation?: VerifyKeyFunction;
17
18
  timeoutConfig?: InteractionTimeoutConfig;
@@ -178,6 +179,7 @@ export declare class MiniInteraction {
178
179
  private readonly commandsDirectory;
179
180
  private readonly componentsDirectory;
180
181
  readonly utilsDirectory: string | null;
182
+ private readonly debug;
181
183
  private readonly timeoutConfig;
182
184
  private readonly commands;
183
185
  private readonly componentHandlers;
@@ -196,6 +198,7 @@ export declare class MiniInteraction {
196
198
  * Creates a new MiniInteraction client with optional command auto-loading and custom runtime hooks.
197
199
  */
198
200
  constructor(options?: InteractionClientOptions);
201
+ private log;
199
202
  private trackInteractionState;
200
203
  /**
201
204
  * Checks if an interaction can still respond (not expired and not already responded).
@@ -31,6 +31,7 @@ export class MiniInteraction {
31
31
  commandsDirectory;
32
32
  componentsDirectory;
33
33
  utilsDirectory;
34
+ debug;
34
35
  timeoutConfig;
35
36
  commands = new Map();
36
37
  componentHandlers = new Map();
@@ -54,7 +55,8 @@ export class MiniInteraction {
54
55
  // @ts-ignore - Optional dependency, may not have types available during build
55
56
  import("dotenv/config").catch(() => { });
56
57
  }
57
- const { applicationId, publicKey, commandsDirectory, componentsDirectory, utilsDirectory, fetchImplementation, verifyKeyImplementation, timeoutConfig, } = options;
58
+ const { applicationId, publicKey, commandsDirectory, componentsDirectory, utilsDirectory, debug, fetchImplementation, verifyKeyImplementation, timeoutConfig, } = options;
59
+ this.debug = debug ?? (typeof process !== "undefined" ? process.env.DEBUG === "true" || process.env.MINI_DEBUG === "true" : false);
58
60
  const resolvedAppId = applicationId ?? (typeof process !== "undefined" ? process.env.DISCORD_APPLICATION_ID : undefined);
59
61
  const resolvedPublicKey = publicKey ?? (typeof process !== "undefined" ? (process.env.DISCORD_PUBLIC_KEY ?? process.env.DISCORD_APP_PUBLIC_KEY) : undefined);
60
62
  const fetchImpl = fetchImplementation ?? globalThis.fetch;
@@ -85,6 +87,11 @@ export class MiniInteraction {
85
87
  ...timeoutConfig,
86
88
  };
87
89
  }
90
+ log(message, ...args) {
91
+ if (this.debug) {
92
+ console.log(`[MiniInteraction] ${message}`, ...args);
93
+ }
94
+ }
88
95
  trackInteractionState(interactionId, token, state) {
89
96
  const now = Date.now();
90
97
  this.interactionStates.set(interactionId, {
@@ -856,6 +863,9 @@ export class MiniInteraction {
856
863
  * Recursively collects all command module file paths from the target directory.
857
864
  */
858
865
  async collectModuleFiles(directory) {
866
+ if (this.debug) {
867
+ this.log(`Collecting module files from: ${directory}`);
868
+ }
859
869
  const entries = await readdir(directory, { withFileTypes: true });
860
870
  const files = [];
861
871
  for (const entry of entries) {
@@ -891,6 +901,7 @@ export class MiniInteraction {
891
901
  async importCommandModule(absolutePath) {
892
902
  try {
893
903
  const moduleUrl = pathToFileURL(absolutePath).href;
904
+ this.log(`Dynamically importing command: ${moduleUrl}`);
894
905
  const imported = await import(moduleUrl);
895
906
  // Try to find a command object from various export patterns
896
907
  let candidate = imported.default ??
@@ -954,6 +965,7 @@ export class MiniInteraction {
954
965
  async importComponentModule(absolutePath) {
955
966
  try {
956
967
  const moduleUrl = pathToFileURL(absolutePath).href;
968
+ this.log(`Dynamically importing component: ${moduleUrl}`);
957
969
  const imported = await import(moduleUrl);
958
970
  // Collect all potential component candidates
959
971
  const candidates = [];
@@ -1139,10 +1151,17 @@ export class MiniInteraction {
1139
1151
  pushCandidate(path.resolve(root, defaultFolder));
1140
1152
  }
1141
1153
  for (const candidate of candidates) {
1142
- if (existsSync(candidate)) {
1154
+ const exists = existsSync(candidate);
1155
+ if (this.debug) {
1156
+ this.log(`Checking directory: ${candidate} (exists: ${exists})`);
1157
+ }
1158
+ if (exists) {
1143
1159
  return candidate;
1144
1160
  }
1145
1161
  }
1162
+ if (this.debug) {
1163
+ this.log(`No existing directory found for ${defaultFolder}, using default candidate: ${candidates[0]}`);
1164
+ }
1146
1165
  return candidates[0];
1147
1166
  }
1148
1167
  /**
@@ -1,4 +1,4 @@
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";
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
2
  import { DeferReplyOptions, InteractionMessageData } from "./interactionMessageHelpers.js";
3
3
  /** Resolved user option including optional guild member data. */
4
4
  export type ResolvedUserOption = {
@@ -183,21 +183,29 @@ export type MessageComponentInteraction = APIMessageComponentInteraction & {
183
183
  * @returns Array of resolved role objects, or empty array if not a role select menu
184
184
  */
185
185
  getRoles: () => APIRole[];
186
+ getRole: () => APIRole | undefined;
186
187
  /**
187
188
  * Helper method to get selected channels from a channel select menu.
188
189
  * @returns Array of resolved channel objects, or empty array if not a channel select menu
189
190
  */
190
191
  getChannels: () => APIInteractionDataResolvedChannel[];
192
+ getChannel: () => APIInteractionDataResolvedChannel | undefined;
191
193
  /**
192
194
  * Helper method to get selected users from a user select menu.
193
195
  * @returns Array of resolved user objects (with optional member data), or empty array if not a user select menu
194
196
  */
195
197
  getUsers: () => ResolvedUserOption[];
198
+ getUser: () => ResolvedUserOption | undefined;
196
199
  /**
197
200
  * Helper method to get selected mentionables from a mentionable select menu.
198
201
  * @returns Array of resolved mentionable objects (users or roles), or empty array if not a mentionable select menu
199
202
  */
200
203
  getMentionables: () => ResolvedMentionableOption[];
204
+ getMentionable: () => ResolvedMentionableOption | undefined;
205
+ /**
206
+ * Helper method to get an attachment value (if applicable).
207
+ */
208
+ getAttachment: () => APIAttachment | undefined;
201
209
  };
202
210
  export declare const MessageComponentInteraction: {};
203
211
  /**
@@ -160,6 +160,7 @@ export function createMessageComponentInteraction(interaction, helpers) {
160
160
  }
161
161
  return roles;
162
162
  };
163
+ const getRole = () => getRoles()[0];
163
164
  const getChannels = () => {
164
165
  if (!values) {
165
166
  return [];
@@ -180,6 +181,7 @@ export function createMessageComponentInteraction(interaction, helpers) {
180
181
  }
181
182
  return channels;
182
183
  };
184
+ const getChannel = () => getChannels()[0];
183
185
  const getUsers = () => {
184
186
  if (!values) {
185
187
  return [];
@@ -201,6 +203,7 @@ export function createMessageComponentInteraction(interaction, helpers) {
201
203
  }
202
204
  return users;
203
205
  };
206
+ const getUser = () => getUsers()[0];
204
207
  const getMentionables = () => {
205
208
  if (!values) {
206
209
  return [];
@@ -229,6 +232,15 @@ export function createMessageComponentInteraction(interaction, helpers) {
229
232
  }
230
233
  return mentionables;
231
234
  };
235
+ const getMentionable = () => getMentionables()[0];
236
+ const getAttachment = () => {
237
+ if (!values || values.length === 0)
238
+ return undefined;
239
+ const resolved = "resolved" in interaction.data
240
+ ? interaction.data.resolved
241
+ : undefined;
242
+ return resolved?.attachments?.[values[0]];
243
+ };
232
244
  return Object.assign(interaction, {
233
245
  reply,
234
246
  deferReply,
@@ -240,9 +252,14 @@ export function createMessageComponentInteraction(interaction, helpers) {
240
252
  values,
241
253
  getStringValues,
242
254
  getRoles,
255
+ getRole,
243
256
  getChannels,
257
+ getChannel,
244
258
  getUsers,
259
+ getUser,
245
260
  getMentionables,
261
+ getMentionable,
262
+ getAttachment,
246
263
  onAck: helpers?.onAck,
247
264
  sendFollowUp: helpers?.sendFollowUp,
248
265
  canRespond: helpers?.canRespond,
@@ -1,5 +1,6 @@
1
- import { type APIInteractionResponse, type APIInteractionResponseChannelMessageWithSource, type APIInteractionResponseDeferredChannelMessageWithSource, type APIModalSubmitInteraction } from "discord-api-types/v10";
1
+ import { type APIInteractionResponse, type APIInteractionResponseChannelMessageWithSource, type APIInteractionResponseDeferredChannelMessageWithSource, type APIModalSubmitInteraction, type APIRole, type APIInteractionDataResolvedChannel, type APIAttachment } from "discord-api-types/v10";
2
2
  import { DeferReplyOptions, InteractionMessageData } from "./interactionMessageHelpers.js";
3
+ import type { ResolvedUserOption } from "./MessageComponentInteraction.js";
3
4
  /**
4
5
  * Represents a modal submit interaction augmented with helper response methods.
5
6
  */
@@ -7,9 +8,6 @@ export type ModalSubmitInteraction = APIModalSubmitInteraction & {
7
8
  getResponse: () => APIInteractionResponse | null;
8
9
  reply: (data: InteractionMessageData) => Promise<APIInteractionResponseChannelMessageWithSource>;
9
10
  deferReply: (options?: DeferReplyOptions) => APIInteractionResponseDeferredChannelMessageWithSource;
10
- /**
11
- * Helper method to get the value of a text input component by its custom ID.
12
- */
13
11
  /**
14
12
  * Helper method to get the value of a text input component by its custom ID.
15
13
  */
@@ -23,6 +21,25 @@ export type ModalSubmitInteraction = APIModalSubmitInteraction & {
23
21
  * Returns string for text inputs, string[] for select menus, or undefined.
24
22
  */
25
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 an attachment value (e.g. from FileUpload component).
41
+ */
42
+ getAttachment: (customId: string) => APIAttachment | undefined;
26
43
  /**
27
44
  * Finalise the interaction response via a webhook follow-up.
28
45
  * This is automatically called by reply() if the interaction is deferred.
@@ -137,6 +137,56 @@ export function createModalSubmitInteraction(interaction, helpers) {
137
137
  }
138
138
  return getSelectMenuValues(customId);
139
139
  };
140
+ const getRoles = (customId) => {
141
+ const values = getSelectMenuValues(customId);
142
+ const resolved = interaction.data.resolved;
143
+ if (!values || !resolved?.roles)
144
+ return [];
145
+ const roles = [];
146
+ for (const id of values) {
147
+ const role = resolved.roles[id];
148
+ if (role)
149
+ roles.push(role);
150
+ }
151
+ return roles;
152
+ };
153
+ const getRole = (customId) => getRoles(customId)[0];
154
+ const getUsers = (customId) => {
155
+ const values = getSelectMenuValues(customId);
156
+ const resolved = interaction.data.resolved;
157
+ if (!values || !resolved?.users)
158
+ return [];
159
+ const users = [];
160
+ for (const id of values) {
161
+ const user = resolved.users[id];
162
+ if (user) {
163
+ const member = resolved.members?.[id];
164
+ users.push({ user, member });
165
+ }
166
+ }
167
+ return users;
168
+ };
169
+ const getUser = (customId) => getUsers(customId)[0];
170
+ const getChannels = (customId) => {
171
+ const values = getSelectMenuValues(customId);
172
+ const resolved = interaction.data.resolved;
173
+ if (!values || !resolved?.channels)
174
+ return [];
175
+ const channels = [];
176
+ for (const id of values) {
177
+ const channel = resolved.channels[id];
178
+ if (channel)
179
+ channels.push(channel);
180
+ }
181
+ return channels;
182
+ };
183
+ const getChannel = (customId) => getChannels(customId)[0];
184
+ const getAttachment = (customId) => {
185
+ const value = getComponentValue(customId);
186
+ if (!value || Array.isArray(value))
187
+ return undefined;
188
+ return interaction.data.resolved?.attachments?.[value];
189
+ };
140
190
  return Object.assign(interaction, {
141
191
  reply,
142
192
  deferReply,
@@ -145,6 +195,13 @@ export function createModalSubmitInteraction(interaction, helpers) {
145
195
  getTextFieldValue,
146
196
  getSelectMenuValues,
147
197
  getComponentValue,
198
+ getRoles,
199
+ getRole,
200
+ getUsers,
201
+ getUser,
202
+ getChannels,
203
+ getChannel,
204
+ getAttachment,
148
205
  sendFollowUp: helpers?.sendFollowUp,
149
206
  canRespond: helpers?.canRespond,
150
207
  trackResponse: helpers?.trackResponse,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@minesa-org/mini-interaction",
3
- "version": "0.3.9",
3
+ "version": "0.3.13",
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",