Skip to content
This repository has been archived by the owner on Oct 12, 2024. It is now read-only.

Commit

Permalink
Events: Initial code
Browse files Browse the repository at this point in the history
  • Loading branch information
RadiatedExodus committed Feb 12, 2024
1 parent 04e75a4 commit a5ad2e8
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/classes/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { HolodexApiClient } from "holodex.js";

import Logging from "./logging.js";
import MeteoriumInteractionManager from "../interactions/index.js";
import MeteoriumEventManager from "../events/index.js";

function parseDotEnvConfig() {
if (!process.env.METEORIUM_BOT_TOKEN) config();
Expand All @@ -23,6 +24,7 @@ export default class MeteoriumClient extends Client<true> {
public config = parseDotEnvConfig();
public db = new PrismaClient();
public interactions = new MeteoriumInteractionManager(this);
public events = new MeteoriumEventManager(this);
public holodex = new HolodexApiClient({ apiKey: this.config.HolodexApiKey });

public async login() {
Expand Down
12 changes: 12 additions & 0 deletions src/events/eventsEntry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { Awaitable, ClientEvents } from "discord.js";
import type MeteoriumClient from "../classes/client.js";

export * as ReadyInit from "./readyInit.js";
export * as InteractionHandler from "./interactionHandler.js";
export * as PresenceResumption from "./presenceResumption.js";

export type MeteoriumEvent<EventName extends keyof ClientEvents> = {
event: EventName;
callback(client: MeteoriumClient, ...args: ClientEvents[EventName]): Awaitable<any>;
once: boolean;
};
42 changes: 42 additions & 0 deletions src/events/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ClientEvents, Collection } from "discord.js";
import type MeteoriumClient from "../classes/client.js";
import type { LoggingNamespace } from "../classes/logging.js";

import * as events from "./eventsEntry.js";

export default class MeteoriumEventManager {
public events: Collection<string, events.MeteoriumEvent<any>>;
public client: MeteoriumClient;
public logging: LoggingNamespace;

public constructor(client: MeteoriumClient) {
this.events = new Collection();
this.client = client;
this.logging = client.logging.registerNamespace("EventManager");
}

public register() {
const regNS = this.logging.getNamespace("Registration");

regNS.info("Registering events");
for (const [Name, { Event }] of Object.entries(events)) {
regNS.verbose(`Registering -> ${Name}${Event.once ? " (Once)" : ""}`);
this.events.set(Name, Event);
}

return;
}

public hook() {
const hookNS = this.logging.registerNamespace("Hooking");

hookNS.info("Hooking events to client");
for (const [Name, Event] of this.events) {
hookNS.verbose(`Hooking -> ${Name}`);
if (Event.once) this.client.once(Event.event, Event.callback);
else this.client.on(Event.event, Event.callback);
}

return;
}
}
7 changes: 7 additions & 0 deletions src/events/interactionHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { MeteoriumEvent } from "./eventsEntry.js";

export const Event: MeteoriumEvent<"interactionCreate"> = {
event: "interactionCreate",
async callback(client, interaction) {},
once: false,
};
7 changes: 7 additions & 0 deletions src/events/presenceResumption.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { MeteoriumEvent } from "./eventsEntry.js";

export const Event: MeteoriumEvent<"shardResume"> = {
event: "shardResume",
async callback(client) {},
once: false,
};
7 changes: 7 additions & 0 deletions src/events/readyInit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { MeteoriumEvent } from "./eventsEntry.js";

export const Event: MeteoriumEvent<"ready"> = {
event: "ready",
async callback(client) {},
once: true,
};

0 comments on commit a5ad2e8

Please sign in to comment.