feat: add events.ts for more customizable event handling

This commit is contained in:
Jacob Nguyen
2022-06-14 14:16:55 -05:00
parent cccfecc325
commit 812974ebb6
3 changed files with 55 additions and 9 deletions

View File

@@ -2,14 +2,17 @@
* @enum { number };
*/
enum CommandType {
Text = 0b00000001,
Slash = 0b00000010,
MenuUser = 0b00000100,
MenuMsg = 0b00001000,
Button = 0b00010000,
MenuSelect = 0b00100000,
Modal = 0b01000000,
Autocomplete = 0b10000000,
Text = 0b00000000001,
Slash = 0b00000000010,
MenuUser = 0b00000000100,
MenuMsg = 0b0000001000,
Button = 0b00000010000,
MenuSelect = 0b00000100000,
Modal = 0b00001000000,
Autocomplete = 0b00010000000,
Discord = 0b00100000000,
External = 0b01000000000,
Sern = 0b10000000000,
Both = 0b0000011,
}

View File

@@ -0,0 +1,38 @@
import type { Override } from '../../types/handler';
import type { BaseModule } from './module';
import type { CommandPlugin, EventPlugin } from '../plugins/plugin';
import type { CommandType } from './enums';
import type { SernEventsMapping } from '../sernEmitter';
import type { Awaitable, ClientEvents } from 'discord.js';
export type SernEventCommand<T extends keyof SernEventsMapping = keyof SernEventsMapping> =
Override<
BaseModule,
{
name?: T;
type: CommandType.Sern;
onEvent: EventPlugin<CommandType.Sern>[];
plugins: CommandPlugin[];
execute(...args: SernEventsMapping[T]): Awaitable<void | unknown>;
}
>;
export type DiscordEventCommand<T extends keyof ClientEvents = keyof ClientEvents> = Override<
BaseModule,
{
name?: T;
type: CommandType.Discord;
onEvent: EventPlugin<CommandType.Discord>[];
plugins: CommandPlugin[];
execute(...args: ClientEvents[T]): Awaitable<void | unknown>;
}
>;
export type ExternalEventCommand = Override<
BaseModule,
{
type: CommandType.External;
onEvent: EventPlugin<CommandType.External>[];
plugins: CommandPlugin[];
execute(...args: unknown[]): Awaitable<void | unknown>;
}
>;

View File

@@ -1,4 +1,4 @@
import type { Module, ModuleDefs } from '../structures/module';
import type { EventModule, Module, ModuleDefs } from '../structures/module';
import type {
Awaitable,
ButtonInteraction,
@@ -10,6 +10,7 @@ import type {
UserContextMenuCommandInteraction,
} from 'discord.js';
import type { DiscordEvent, EventEmitterRegister, SernEvent } from '../..';
import { CommandType } from '../..';
export function correctModuleType<T extends keyof ModuleDefs>(
plug: Module | undefined,
@@ -57,3 +58,7 @@ export function isDiscordEvent(
export function isSernEvent(el: DiscordEvent | EventEmitterRegister | SernEvent): el is SernEvent {
return !isDiscordEvent(el);
}
export function isEventModule(module: Module): module is EventModule {
return [CommandType.Discord, CommandType.Sern, CommandType.External].includes(module.type);
}