Skip to content

Commit 8a68af8

Browse files
committed
Make logging work with multiple loggers
1 parent 67c33e7 commit 8a68af8

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

backend/src/plugin/logging/helper/logging.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,25 @@ import { logViaWebhook } from "#plugin/logging/helper/webhooks.ts";
66
import { loggingConfigStore } from "#plugin/logging/index.ts";
77
import type { Guild } from "oceanic.js";
88

9-
type EventConfig = ValuesOf<LoggerConfig["events"]>;
10-
type EventConfigView<T extends EventConfig> = Parameters<
9+
type EventConfigs = LoggerConfig["events"];
10+
type EventConfigView<T extends ValuesOf<EventConfigs>> = Parameters<
1111
Exclude<T, false>["render"]
1212
>[0];
1313

14-
// FIXME: the generics are very broken!
15-
export async function logEvent<T extends EventConfig>(
14+
export async function logEvent<T extends keyof EventConfigs>(
1615
ctx: SquirrelDiscordContext,
1716
guild: Guild,
1817
channel: string | null,
19-
key: keyof LoggerConfig["events"],
20-
supply: () => Awaitable<EventConfigView<T> | null>,
18+
key: T,
19+
supply: () => Awaitable<EventConfigView<EventConfigs[T]> | null>,
2120
): Promise<void> {
2221
const config = loggingConfigStore.get(guild.id);
2322

2423
if (config === undefined) {
2524
return;
2625
}
2726

28-
let lazyParams: EventConfigView<T> | undefined;
27+
let view: EventConfigView<EventConfigs[T]> | null = null;
2928

3029
const tasks: (() => Promise<void>)[] = [];
3130

@@ -40,31 +39,31 @@ export async function logEvent<T extends EventConfig>(
4039
continue;
4140
}
4241

43-
if (lazyParams === undefined) {
44-
const view = await supply();
42+
if (view === null) {
43+
view = await supply();
4544

4645
if (view === null) {
4746
return;
4847
}
48+
}
4949

50-
tasks.push(async () => {
51-
const channel = await fetchTextableGuildChannelCached(
52-
ctx.bot,
53-
guild,
54-
logger.channel,
55-
);
50+
tasks.push(async () => {
51+
const channel = await fetchTextableGuildChannelCached(
52+
ctx.bot,
53+
guild,
54+
logger.channel,
55+
);
5656

57-
if (channel === null) {
58-
return;
59-
}
57+
if (channel === null) {
58+
return;
59+
}
6060

61-
await logViaWebhook(ctx, channel, {
62-
...event.render(view),
63-
username: logger.displayName,
64-
avatarURL: logger.avatar ?? guild.clientMember.avatarURL(),
65-
});
61+
await logViaWebhook(ctx, channel, {
62+
...event.render(view!),
63+
username: logger.displayName,
64+
avatarURL: logger.avatar ?? guild.clientMember.avatarURL(),
6665
});
67-
}
66+
});
6867
}
6968

7069
await Promise.all(tasks.map((task) => task()));

0 commit comments

Comments
 (0)