A powerful and extendable Discord bot, with it's own module system :3
thevoid.cafe/projects/voidy
1import type { ResourceStep } from "@voidy/framework";
2import type { Resource, VoidyClient } from "@voidy/framework";
3import type { ResourceCache } from "@voidy/framework";
4import type { NotificationResource } from "./types";
5import { createNotificationWorker } from "../queue";
6
7export const notificationStep: ResourceStep = {
8 name: "notification",
9
10 match(resource: Resource): boolean {
11 return resource.type === "notification";
12 },
13
14 process(resource: Resource, { cache }): Resource | null {
15 const notif = resource as NotificationResource;
16
17 if (!notif.id || !notif.execute) {
18 console.warn(`[NotificationStep] Skipping invalid notification: missing id or execute`);
19 return null;
20 }
21
22 cache.set("notification", notif.id, notif);
23 return notif;
24 },
25
26 finalize(client: VoidyClient, cache: ResourceCache): void {
27 const notifications = cache.getAll<NotificationResource>("notification");
28 if (notifications.size === 0) return;
29
30 createNotificationWorker(async (job) => {
31 const { notificationId, ...data } = job.data as { notificationId: string } & Record<string, unknown>;
32 const resource = notifications.get(notificationId);
33
34 if (!resource) {
35 console.warn(`[NotificationWorker] Unknown notification: ${notificationId}`);
36 return;
37 }
38
39 try {
40 await resource.execute({ client, resource, data });
41 } catch (err) {
42 console.error(`[NotificationWorker] Error in notification "${notificationId}":`, err);
43 }
44 });
45
46 console.log(`[Notifications] Worker started, ${notifications.size} notification type(s) registered`);
47 },
48};