@minesa-org/mini-interaction 0.2.6 → 0.2.7

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.
@@ -1273,9 +1273,9 @@ export class MiniInteraction {
1273
1273
  }
1274
1274
  else if (commandInteraction.data.type ===
1275
1275
  ApplicationCommandType.User) {
1276
- // User context menu command
1277
1276
  const interactionWithHelpers = createUserContextMenuInteraction(commandInteraction, {
1278
1277
  onAck: (response) => ackResolver?.(response),
1278
+ sendFollowUp,
1279
1279
  });
1280
1280
  response = await command.handler(interactionWithHelpers);
1281
1281
  resolvedResponse =
@@ -1285,6 +1285,7 @@ export class MiniInteraction {
1285
1285
  ApplicationCommandType.PrimaryEntryPoint) {
1286
1286
  const interactionWithHelpers = createAppCommandInteraction(commandInteraction, {
1287
1287
  onAck: (response) => ackResolver?.(response),
1288
+ sendFollowUp,
1288
1289
  });
1289
1290
  response = await command.handler(interactionWithHelpers);
1290
1291
  resolvedResponse =
@@ -1295,6 +1296,7 @@ export class MiniInteraction {
1295
1296
  // Message context menu command
1296
1297
  const interactionWithHelpers = createMessageContextMenuInteraction(commandInteraction, {
1297
1298
  onAck: (response) => ackResolver?.(response),
1299
+ sendFollowUp,
1298
1300
  });
1299
1301
  response = await command.handler(interactionWithHelpers);
1300
1302
  resolvedResponse =
@@ -1305,9 +1307,15 @@ export class MiniInteraction {
1305
1307
  response = await command.handler(commandInteraction);
1306
1308
  resolvedResponse = response ?? null;
1307
1309
  }
1310
+ if (this.timeoutConfig.enableResponseDebugLogging) {
1311
+ console.log(`[MiniInteraction] Command handler finished: "${commandName}"`);
1312
+ }
1308
1313
  return resolvedResponse;
1309
1314
  }, this.timeoutConfig.initialResponseTimeout, `Command "${commandName}"`, this.timeoutConfig.enableTimeoutWarnings, ackPromise);
1310
1315
  const finalResponse = await timeoutWrapper();
1316
+ if (this.timeoutConfig.enableResponseDebugLogging) {
1317
+ console.log(`[MiniInteraction] handleApplicationCommand: initial response determined (type=${finalResponse?.type})`);
1318
+ }
1311
1319
  if (!finalResponse) {
1312
1320
  console.error(`[MiniInteraction] Command "${commandName}" did not return a response. ` +
1313
1321
  "This indicates the handler completed but no response was generated. " +
@@ -1352,8 +1360,14 @@ export class MiniInteraction {
1352
1360
  const url = isEdit
1353
1361
  ? `${DISCORD_BASE_URL}/webhooks/${this.applicationId}/${token}/messages/${messageId}`
1354
1362
  : `${DISCORD_BASE_URL}/webhooks/${this.applicationId}/${token}`;
1363
+ if (this.timeoutConfig.enableResponseDebugLogging) {
1364
+ console.log(`[MiniInteraction] sendFollowUp: id=${messageId || 'new'}, edit=${isEdit}, url=${url}`);
1365
+ }
1355
1366
  // Only send follow-up if there is data to send
1356
1367
  if (!('data' in response) || !response.data) {
1368
+ if (this.timeoutConfig.enableResponseDebugLogging) {
1369
+ console.warn(`[MiniInteraction] sendFollowUp cancelled: no data in response object`);
1370
+ }
1357
1371
  return;
1358
1372
  }
1359
1373
  try {
@@ -1364,6 +1378,9 @@ export class MiniInteraction {
1364
1378
  },
1365
1379
  body: JSON.stringify(response.data),
1366
1380
  });
1381
+ if (this.timeoutConfig.enableResponseDebugLogging) {
1382
+ console.log(`[MiniInteraction] sendFollowUp response: [${fetchResponse.status}] ${fetchResponse.statusText}`);
1383
+ }
1367
1384
  if (!fetchResponse.ok) {
1368
1385
  const errorBody = await fetchResponse.text();
1369
1386
  console.error(`[MiniInteraction] Failed to send follow-up response: [${fetchResponse.status}] ${errorBody}`);
@@ -3,16 +3,17 @@ import { DeferReplyOptions, InteractionMessageData } from "./interactionMessageH
3
3
  /**
4
4
  * Base helper methods for context menu interactions.
5
5
  */
6
- type ContextMenuInteractionHelpers = {
6
+ export type ContextMenuInteractionHelpers = {
7
7
  getResponse: () => APIInteractionResponse | null;
8
8
  reply: (data: InteractionMessageData) => APIInteractionResponseChannelMessageWithSource;
9
- followUp: (data: InteractionMessageData) => APIInteractionResponseChannelMessageWithSource;
10
- editReply: (data?: InteractionMessageData) => APIInteractionResponseUpdateMessage;
9
+ followUp: (data: InteractionMessageData) => Promise<APIInteractionResponseChannelMessageWithSource>;
10
+ editReply: (data?: InteractionMessageData) => Promise<APIInteractionResponseUpdateMessage>;
11
11
  deferReply: (options?: DeferReplyOptions) => APIInteractionResponseDeferredChannelMessageWithSource;
12
12
  showModal: (data: APIModalInteractionResponseCallbackData | {
13
13
  toJSON(): APIModalInteractionResponseCallbackData;
14
14
  }) => APIModalInteractionResponse;
15
15
  onAck?: (response: APIInteractionResponse) => void;
16
+ sendFollowUp?: (token: string, response: APIInteractionResponse, messageId?: string) => Promise<void>;
16
17
  };
17
18
  /**
18
19
  * User context menu interaction with helper methods.
@@ -39,27 +40,32 @@ export declare const AppCommandInteraction: {};
39
40
  * Wraps a raw user context menu interaction with helper methods.
40
41
  *
41
42
  * @param interaction - The raw user context menu interaction payload from Discord.
43
+ * @param helpers - Optional callback helpers.
42
44
  * @returns A helper-augmented interaction object.
43
45
  */
44
46
  export declare function createUserContextMenuInteraction(interaction: APIUserApplicationCommandInteraction, helpers?: {
45
47
  onAck?: (response: APIInteractionResponse) => void;
48
+ sendFollowUp?: (token: string, response: APIInteractionResponse, messageId?: string) => Promise<void>;
46
49
  }): UserContextMenuInteraction;
47
50
  /**
48
51
  * Wraps a raw message context menu interaction with helper methods.
49
52
  *
50
53
  * @param interaction - The raw message context menu interaction payload from Discord.
54
+ * @param helpers - Optional callback helpers.
51
55
  * @returns A helper-augmented interaction object.
52
56
  */
53
57
  export declare function createMessageContextMenuInteraction(interaction: APIMessageApplicationCommandInteraction, helpers?: {
54
58
  onAck?: (response: APIInteractionResponse) => void;
59
+ sendFollowUp?: (token: string, response: APIInteractionResponse, messageId?: string) => Promise<void>;
55
60
  }): MessageContextMenuInteraction;
56
61
  /**
57
62
  * Wraps a raw primary entry point interaction with helper methods.
58
63
  *
59
64
  * @param interaction - The raw primary entry point interaction payload from Discord.
65
+ * @param helpers - Optional callback helpers.
60
66
  * @returns A helper-augmented interaction object.
61
67
  */
62
68
  export declare function createAppCommandInteraction(interaction: APIPrimaryEntryPointCommandInteraction, helpers?: {
63
69
  onAck?: (response: APIInteractionResponse) => void;
70
+ sendFollowUp?: (token: string, response: APIInteractionResponse, messageId?: string) => Promise<void>;
64
71
  }): AppCommandInteraction;
65
- export {};
@@ -3,8 +3,10 @@ import { normaliseInteractionMessageData, normaliseMessageFlags, } from "./inter
3
3
  export const UserContextMenuInteraction = {};
4
4
  export const MessageContextMenuInteraction = {};
5
5
  export const AppCommandInteraction = {};
6
- function createContextMenuInteractionHelpers(helpers) {
6
+ function createContextMenuInteractionHelpers(interaction, helpers) {
7
7
  let capturedResponse = null;
8
+ let isDeferred = false;
9
+ let hasResponded = false;
8
10
  const captureResponse = (response) => {
9
11
  capturedResponse = response;
10
12
  return response;
@@ -27,17 +29,32 @@ function createContextMenuInteractionHelpers(helpers) {
27
29
  }
28
30
  const reply = (data) => {
29
31
  const response = createMessageResponse(InteractionResponseType.ChannelMessageWithSource, data);
32
+ hasResponded = true;
30
33
  helpers?.onAck?.(response);
31
34
  return response;
32
35
  };
33
- const followUp = (data) => createMessageResponse(InteractionResponseType.ChannelMessageWithSource, data);
34
- const editReply = (data) => createMessageResponse(InteractionResponseType.UpdateMessage, data);
36
+ const followUp = async (data) => {
37
+ const response = createMessageResponse(InteractionResponseType.ChannelMessageWithSource, data);
38
+ if (helpers?.sendFollowUp) {
39
+ await helpers.sendFollowUp(interaction.token, response, '');
40
+ }
41
+ return response;
42
+ };
43
+ const editReply = async (data) => {
44
+ const response = createMessageResponse(InteractionResponseType.UpdateMessage, data);
45
+ if (helpers?.sendFollowUp && (isDeferred || hasResponded)) {
46
+ await helpers.sendFollowUp(interaction.token, response, '@original');
47
+ }
48
+ hasResponded = true;
49
+ return response;
50
+ };
35
51
  const deferReply = (options = {}) => {
36
52
  const flags = normaliseMessageFlags(options.flags);
37
53
  const response = captureResponse({
38
54
  type: InteractionResponseType.DeferredChannelMessageWithSource,
39
55
  data: flags ? { flags } : undefined,
40
56
  });
57
+ isDeferred = true;
41
58
  helpers?.onAck?.(response);
42
59
  return response;
43
60
  };
@@ -56,17 +73,18 @@ function createContextMenuInteractionHelpers(helpers) {
56
73
  editReply,
57
74
  deferReply,
58
75
  showModal,
59
- onAck: helpers?.onAck,
76
+ sendFollowUp: helpers?.sendFollowUp,
60
77
  };
61
78
  }
62
79
  /**
63
80
  * Wraps a raw user context menu interaction with helper methods.
64
81
  *
65
82
  * @param interaction - The raw user context menu interaction payload from Discord.
83
+ * @param helpers - Optional callback helpers.
66
84
  * @returns A helper-augmented interaction object.
67
85
  */
68
86
  export function createUserContextMenuInteraction(interaction, helpers) {
69
- return Object.assign(interaction, createContextMenuInteractionHelpers(helpers), {
87
+ return Object.assign(interaction, createContextMenuInteractionHelpers(interaction, helpers), {
70
88
  targetUser: resolveTargetUser(interaction),
71
89
  });
72
90
  }
@@ -74,10 +92,11 @@ export function createUserContextMenuInteraction(interaction, helpers) {
74
92
  * Wraps a raw message context menu interaction with helper methods.
75
93
  *
76
94
  * @param interaction - The raw message context menu interaction payload from Discord.
95
+ * @param helpers - Optional callback helpers.
77
96
  * @returns A helper-augmented interaction object.
78
97
  */
79
98
  export function createMessageContextMenuInteraction(interaction, helpers) {
80
- return Object.assign(interaction, createContextMenuInteractionHelpers(helpers), {
99
+ return Object.assign(interaction, createContextMenuInteractionHelpers(interaction, helpers), {
81
100
  targetMessage: resolveTargetMessage(interaction),
82
101
  });
83
102
  }
@@ -85,10 +104,11 @@ export function createMessageContextMenuInteraction(interaction, helpers) {
85
104
  * Wraps a raw primary entry point interaction with helper methods.
86
105
  *
87
106
  * @param interaction - The raw primary entry point interaction payload from Discord.
107
+ * @param helpers - Optional callback helpers.
88
108
  * @returns A helper-augmented interaction object.
89
109
  */
90
110
  export function createAppCommandInteraction(interaction, helpers) {
91
- return Object.assign(interaction, createContextMenuInteractionHelpers(helpers));
111
+ return Object.assign(interaction, createContextMenuInteractionHelpers(interaction, helpers));
92
112
  }
93
113
  function resolveTargetMessage(interaction) {
94
114
  const targetId = interaction.data?.target_id;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@minesa-org/mini-interaction",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
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",