mirror of
https://github.com/sern-handler/handler
synced 2026-06-28 02:32:15 +00:00
feat: should be able to register other nodejs event emitters
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type { DiscordEvent } from '../types/handler';
|
||||
import type { DiscordEvent, EventEmitterRegister } from '../types/handler';
|
||||
|
||||
import { ApplicationCommandType, Client } from 'discord.js';
|
||||
|
||||
@@ -11,20 +11,25 @@ import { onInteractionCreate } from './events/interactionCreate';
|
||||
import { match, P } from 'ts-pattern';
|
||||
import { Err, Ok } from 'ts-results';
|
||||
import { CommandType } from './structures/enums';
|
||||
import { isDiscordEvent } from './utilities/predicates';
|
||||
|
||||
export function init(wrapper: Wrapper) {
|
||||
const { events, client } = wrapper;
|
||||
if (events !== undefined) eventObserver(client, events);
|
||||
if (events !== undefined) {
|
||||
eventObserver(client, events);
|
||||
}
|
||||
onReady(wrapper);
|
||||
onMessageCreate(wrapper);
|
||||
onInteractionCreate(wrapper);
|
||||
}
|
||||
|
||||
//TODO : Add event listener for any other generic node js event emitter
|
||||
function eventObserver(client: Client, events: DiscordEvent[]) {
|
||||
events.forEach(([event, cb]) => {
|
||||
if (event === 'ready') throw Error(SernError.ReservedEvent);
|
||||
fromEvent(client, event, cb).subscribe();
|
||||
function eventObserver(client: Client, events: (DiscordEvent | EventEmitterRegister)[]) {
|
||||
events.forEach((event) => {
|
||||
if(isDiscordEvent(event)) {
|
||||
fromEvent(client, event[0], event[1]).subscribe();
|
||||
} else {
|
||||
fromEvent(event[0], event[1], event[2]).subscribe();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { Client } from 'discord.js';
|
||||
import type { DiscordEvent } from '../../types/handler';
|
||||
import type { DiscordEvent, EventEmitterRegister } from '../../types/handler';
|
||||
|
||||
/**
|
||||
* An object to be passed into Sern.Handler constructor.
|
||||
@@ -14,7 +14,7 @@ interface Wrapper {
|
||||
readonly client: Client;
|
||||
readonly defaultPrefix?: string;
|
||||
readonly commands: string;
|
||||
readonly events?: DiscordEvent[];
|
||||
readonly events?: (DiscordEvent | EventEmitterRegister)[];
|
||||
}
|
||||
|
||||
export default Wrapper;
|
||||
|
||||
@@ -9,6 +9,7 @@ import type {
|
||||
SelectMenuInteraction,
|
||||
UserContextMenuCommandInteraction,
|
||||
} from 'discord.js';
|
||||
import type { DiscordEvent, EventEmitterRegister } from '../..';
|
||||
|
||||
|
||||
|
||||
@@ -40,4 +41,9 @@ export function isUserContextMenuCmd(i : CommandInteraction) : i is UserContextM
|
||||
export function isPromise<T>(promiseLike : Awaitable<T>) : promiseLike is Promise<T> {
|
||||
const keys = new Set(Object.keys(promiseLike));
|
||||
return keys.has('then') && keys.has('catch');
|
||||
}
|
||||
}
|
||||
|
||||
export function isDiscordEvent(el : DiscordEvent | EventEmitterRegister) : el is DiscordEvent {
|
||||
return el.length === 2;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import type {
|
||||
MessageOptions,
|
||||
MessagePayload,
|
||||
} from 'discord.js';
|
||||
|
||||
import type { EventEmitter } from 'events';
|
||||
// Anything that can be sent in a `<TextChannel>#send` or `<CommandInteraction>#reply`
|
||||
export type possibleOutput<T = string> = T | (MessagePayload & MessageOptions);
|
||||
export type Nullish<T> = T | undefined | null;
|
||||
@@ -17,6 +17,9 @@ export type ParseType<T> = {
|
||||
export type Args = ParseType<{ text: string[]; slash: SlashOptions }>;
|
||||
|
||||
export type DiscordEvent = ParseType<{ [K in keyof ClientEvents]: (...args: ClientEvents[K]) => Awaitable<void> }>;
|
||||
export type EventEmitterRegister = [ emitter: EventEmitter, k : string, cb : (...args: unknown[]) => Awaitable<void>];
|
||||
|
||||
|
||||
|
||||
export type SlashOptions = Omit<CommandInteractionOptionResolver, 'getMessage' | 'getFocused'>;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user