@minesa-org/mini-interaction 0.2.15 → 0.2.17

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.
@@ -1584,9 +1584,21 @@ function createTimeoutWrapper(handler, timeoutMs, handlerName, enableWarnings =
1584
1584
  reject(new Error(`Handler timeout: ${handlerName} exceeded ${timeoutMs}ms limit`));
1585
1585
  }, timeoutMs);
1586
1586
  });
1587
+ // Create the handler promise and handle its potential background errors
1588
+ const handlerPromise = (async () => {
1589
+ try {
1590
+ return await handler(...args);
1591
+ }
1592
+ catch (error) {
1593
+ // If this error happens AFTER an ACK was already sent (via ackPromise)
1594
+ // or after a timeout, we MUST still log it because nobody else will catch it.
1595
+ console.error(`[MiniInteraction] ${handlerName} background error:`, error);
1596
+ throw error;
1597
+ }
1598
+ })();
1587
1599
  try {
1588
1600
  const promises = [
1589
- Promise.resolve(handler(...args)),
1601
+ handlerPromise,
1590
1602
  timeoutPromise,
1591
1603
  ];
1592
1604
  if (ackPromise) {
@@ -1606,12 +1618,13 @@ function createTimeoutWrapper(handler, timeoutMs, handlerName, enableWarnings =
1606
1618
  if (timeoutId) {
1607
1619
  clearTimeout(timeoutId);
1608
1620
  }
1609
- // Re-throw the error with additional context
1621
+ // Background errors are already logged above
1610
1622
  if (error instanceof Error &&
1611
1623
  error.message.includes("Handler timeout")) {
1612
1624
  throw error;
1613
1625
  }
1614
- console.error(`[MiniInteraction] ${handlerName} failed:`, error);
1626
+ // Only log here if it wasn't a background error (which we already caught)
1627
+ // But since we catch all in handlerPromise, this is mostly for the timeout/race itself.
1615
1628
  throw error;
1616
1629
  }
1617
1630
  };
@@ -45,10 +45,10 @@ export interface CommandInteraction extends Omit<APIChatInputApplicationCommandI
45
45
  };
46
46
  options: CommandInteractionOptionResolver;
47
47
  getResponse(): APIInteractionResponse | null;
48
- reply(data: InteractionMessageData): APIInteractionResponseChannelMessageWithSource;
48
+ reply(data: InteractionMessageData): Promise<APIInteractionResponseChannelMessageWithSource>;
49
49
  edit(data?: InteractionMessageData): APIInteractionResponseUpdateMessage;
50
50
  followUp(data: InteractionMessageData): Promise<void>;
51
- editReply(data?: InteractionMessageData): Promise<void>;
51
+ editReply(data?: InteractionMessageData): Promise<APIInteractionResponseChannelMessageWithSource | APIInteractionResponseUpdateMessage>;
52
52
  deferReply(options?: DeferReplyOptions): APIInteractionResponseDeferredChannelMessageWithSource;
53
53
  showModal(data: APIModalInteractionResponseCallbackData | {
54
54
  toJSON(): APIModalInteractionResponseCallbackData;
@@ -251,13 +251,13 @@ export function createCommandInteraction(interaction, helpers) {
251
251
  getResponse() {
252
252
  return capturedResponse;
253
253
  },
254
- reply(data) {
254
+ async reply(data) {
255
255
  if (this.canRespond && !this.canRespond(this.id)) {
256
256
  throw new Error('Interaction cannot respond: already responded or expired');
257
257
  }
258
258
  const response = createMessageResponse(InteractionResponseType.ChannelMessageWithSource, data);
259
259
  if (isDeferred && this.sendFollowUp) {
260
- this.sendFollowUp(this.token, response, '@original');
260
+ await this.sendFollowUp(this.token, response, '@original');
261
261
  }
262
262
  else {
263
263
  this.onAck?.(response);
@@ -305,6 +305,7 @@ export function createCommandInteraction(interaction, helpers) {
305
305
  }
306
306
  this.trackResponse?.(this.id, this.token, 'responded');
307
307
  hasResponded = true;
308
+ return response;
308
309
  },
309
310
  deferReply(options) {
310
311
  if (this.canRespond && !this.canRespond(this.id)) {
@@ -5,7 +5,7 @@ import { DeferReplyOptions, InteractionMessageData } from "./interactionMessageH
5
5
  */
6
6
  export type ContextMenuInteractionHelpers = {
7
7
  getResponse: () => APIInteractionResponse | null;
8
- reply: (data: InteractionMessageData) => APIInteractionResponseChannelMessageWithSource;
8
+ reply: (data: InteractionMessageData) => Promise<APIInteractionResponseChannelMessageWithSource>;
9
9
  followUp: (data: InteractionMessageData) => Promise<APIInteractionResponseChannelMessageWithSource>;
10
10
  editReply: (data?: InteractionMessageData) => Promise<APIInteractionResponseUpdateMessage>;
11
11
  deferReply: (options?: DeferReplyOptions) => APIInteractionResponseDeferredChannelMessageWithSource;
@@ -27,13 +27,13 @@ function createContextMenuInteractionHelpers(interaction, helpers) {
27
27
  }
28
28
  return captureResponse({ type });
29
29
  }
30
- const reply = (data) => {
30
+ const reply = async (data) => {
31
31
  if (helpers?.canRespond && !helpers.canRespond(interaction.id)) {
32
32
  throw new Error("[MiniInteraction] Interaction cannot respond: already responded or expired");
33
33
  }
34
34
  const response = createMessageResponse(InteractionResponseType.ChannelMessageWithSource, data);
35
35
  if (isDeferred && helpers?.sendFollowUp) {
36
- helpers.sendFollowUp(interaction.token, response, '@original');
36
+ await helpers.sendFollowUp(interaction.token, response, '@original');
37
37
  }
38
38
  else {
39
39
  helpers?.onAck?.(response);
@@ -153,6 +153,10 @@ export type MessageComponentInteraction = APIMessageComponentInteraction & {
153
153
  showModal: (data: APIModalInteractionResponseCallbackData | {
154
154
  toJSON(): APIModalInteractionResponseCallbackData;
155
155
  }) => APIModalInteractionResponse;
156
+ /**
157
+ * Edit the initial interaction response.
158
+ */
159
+ editReply: (data?: InteractionMessageData) => Promise<APIInteractionResponseUpdateMessage | APIInteractionResponseChannelMessageWithSource>;
156
160
  /**
157
161
  * Finalise the interaction response via a webhook follow-up.
158
162
  * This is automatically called by reply() and update() if the interaction is deferred.
@@ -108,6 +108,29 @@ export function createMessageComponentInteraction(interaction, helpers) {
108
108
  data: resolvedData,
109
109
  });
110
110
  };
111
+ const editReply = async (data) => {
112
+ if (helpers?.canRespond && !helpers.canRespond(interaction.id)) {
113
+ throw new Error("[MiniInteraction] Interaction cannot edit reply: expired");
114
+ }
115
+ const normalisedData = normaliseInteractionMessageData(data);
116
+ const response = captureResponse(normalisedData
117
+ ? {
118
+ type: InteractionResponseType.ChannelMessageWithSource,
119
+ data: normalisedData,
120
+ }
121
+ : {
122
+ type: InteractionResponseType.ChannelMessageWithSource,
123
+ data: { content: "" }
124
+ });
125
+ if (helpers?.sendFollowUp) {
126
+ await helpers.sendFollowUp(interaction.token, response, "@original");
127
+ }
128
+ else {
129
+ helpers?.onAck?.(response);
130
+ }
131
+ helpers?.trackResponse?.(interaction.id, interaction.token, "responded");
132
+ return response;
133
+ };
111
134
  const getResponse = () => capturedResponse;
112
135
  // Extract values from select menu interactions
113
136
  const values = "values" in interaction.data ? interaction.data.values : undefined;
@@ -210,6 +233,7 @@ export function createMessageComponentInteraction(interaction, helpers) {
210
233
  update,
211
234
  deferUpdate,
212
235
  showModal,
236
+ editReply,
213
237
  getResponse,
214
238
  values,
215
239
  getStringValues,
@@ -21,6 +21,10 @@ export type ModalSubmitInteraction = APIModalSubmitInteraction & {
21
21
  */
22
22
  canRespond?: (interactionId: string) => boolean;
23
23
  trackResponse?: (interactionId: string, token: string, state: 'responded' | 'deferred') => void;
24
+ /**
25
+ * Edit the initial interaction response.
26
+ */
27
+ editReply: (data?: InteractionMessageData) => Promise<APIInteractionResponseChannelMessageWithSource>;
24
28
  };
25
29
  export declare const ModalSubmitInteraction: {};
26
30
  /**
@@ -25,7 +25,21 @@ export function createModalSubmitInteraction(interaction, helpers) {
25
25
  data: normalisedData,
26
26
  });
27
27
  if (isDeferred && helpers?.sendFollowUp) {
28
- await helpers.sendFollowUp(interaction.token, response, '');
28
+ await helpers.sendFollowUp(interaction.token, response, '@original');
29
+ }
30
+ else {
31
+ helpers?.onAck?.(response);
32
+ }
33
+ return response;
34
+ };
35
+ const editReply = async (data) => {
36
+ const normalisedData = normaliseInteractionMessageData(data);
37
+ const response = captureResponse({
38
+ type: InteractionResponseType.ChannelMessageWithSource,
39
+ data: normalisedData ?? { content: "" },
40
+ });
41
+ if (helpers?.sendFollowUp) {
42
+ await helpers.sendFollowUp(interaction.token, response, '@original');
29
43
  }
30
44
  else {
31
45
  helpers?.onAck?.(response);
@@ -64,6 +78,7 @@ export function createModalSubmitInteraction(interaction, helpers) {
64
78
  return Object.assign(interaction, {
65
79
  reply,
66
80
  deferReply,
81
+ editReply,
67
82
  getResponse,
68
83
  getTextFieldValue,
69
84
  sendFollowUp: helpers?.sendFollowUp,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@minesa-org/mini-interaction",
3
- "version": "0.2.15",
3
+ "version": "0.2.17",
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",