Skip to content

Commit 32a8870

Browse files
chore(debug): add multiple debug logs
1 parent f4edd22 commit 32a8870

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

src/interactionRouter/internal.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ class InteractionRouterManager {
190190
ReturnType<(typeof SlashCommandBuilder)["prototype"]["toJSON"]>
191191
> = [];
192192

193+
constructor(public isDebug = false) {
194+
this.isDebug = isDebug;
195+
}
196+
193197
register(...routes: Array<InteractionRouter | InteractionRouterCollector>) {
194198
routes.forEach((route) => {
195199
if (route instanceof InteractionRouter) {
@@ -202,6 +206,23 @@ class InteractionRouterManager {
202206
});
203207
}
204208

209+
protected debug(...message: string[]) {
210+
if (this.isDebug) {
211+
const datePrefix = new Date().toLocaleString("en-US", {
212+
year: "numeric",
213+
month: "2-digit",
214+
day: "2-digit",
215+
hour: "2-digit",
216+
minute: "2-digit",
217+
second: "2-digit",
218+
});
219+
return console.debug(
220+
`${datePrefix} [class:InteractionRouter - discord.https]`,
221+
...message
222+
);
223+
}
224+
}
225+
205226
mergeRoute(route: InteractionRouter) {
206227
// Merge unknown handlers
207228
for (const [routeKey, routeMap] of Object.entries(
@@ -218,6 +239,7 @@ class InteractionRouterManager {
218239
...(route.__internal_middlewares as any),
219240
...(middlewares as any)
220241
);
242+
this.debug(`Registered interaction route ${routeKey}${key}`);
221243
}
222244
}
223245
// Merge command definitions
@@ -345,6 +367,7 @@ class InteractionRouterManager {
345367
incomingInteraction: DiscordHttpsAPIInteraction,
346368
client: REST
347369
) {
370+
this.debug("The internal dispatcher has been invoked");
348371
const routeData = this.mapInteractionToRoute(incomingInteraction);
349372
if (routeData.routeKey === "unknown") {
350373
const ctx: Context = {
@@ -356,19 +379,21 @@ class InteractionRouterManager {
356379
...this.middlewares,
357380
...this._unknownInteraction,
358381
];
359-
``;
360382
await this._runMiddlewareStack(ctx, middlewareStack, res);
361383
} else {
362384
const route = this.routeStack[routeData.routeKey];
363385
const globalMiddlewares = this.middlewares;
364386
const unknownMiddlewares = route.get(routeData.routeHandlerKey);
365387
if (!unknownMiddlewares)
366-
return this._afterMiddleware(incomingInteraction, client, res);
388+
return this._autoFlusher(incomingInteraction, client, res);
367389
const middlewareStack = [...globalMiddlewares, ...unknownMiddlewares];
368390
const ctx: Context = {
369391
client,
370392
resolvedInteraction: routeData.resolvedInteraction,
371393
};
394+
this.debug(
395+
`Middleware stack has been created with length: ${middlewareStack.length}`
396+
);
372397
await this._runMiddlewareStack(ctx, middlewareStack, res);
373398
}
374399
}
@@ -390,6 +415,7 @@ class InteractionRouterManager {
390415
const flush = () => {
391416
throw new Error("FLUSH_MIDDLEWARE");
392417
};
418+
this.debug(`Running middleware stack with ${stack.length} middleware(s)`);
393419

394420
try {
395421
for (const middleware of stack) {
@@ -403,18 +429,20 @@ class InteractionRouterManager {
403429
}
404430
} catch (err) {
405431
if ((err as Error).message !== "FLUSH_MIDDLEWARE") {
432+
this.debug("Middleware stack threw an unexpected error");
406433
throw err; // propagate real errors
407434
}
408435
}
409-
this._afterMiddleware(ctx.resolvedInteraction, ctx.client, res);
436+
this._autoFlusher(ctx.resolvedInteraction, ctx.client, res);
410437
}
411438

412-
_afterMiddleware(
439+
_autoFlusher(
413440
interaction: Context["resolvedInteraction"],
414441
client: REST,
415442
res: HttpAdapterSererResponse
416443
) {
417444
if (res.headersSent) return;
445+
this.debug("AutoFlusher flushed response with 204 No Content");
418446
res.writeHead(204);
419447
res.end();
420448
}

0 commit comments

Comments
 (0)