Skip to content

Commit d3977f9

Browse files
committed
feat: new events
1 parent f7c7e3c commit d3977f9

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed

src/lib/managers/CommandManager.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Collection } from 'discord.js';
22
import { Command } from '../structures/Command';
33
import type { GClient } from '../GClient';
4-
import { Logger } from '../structures/Logger';
4+
import { Logger, LoggerEvents } from '../structures/Logger';
55
import { Plugins } from './PluginManager';
66

77
export class CommandManager extends Collection<string, Command> {
@@ -13,6 +13,7 @@ export class CommandManager extends Collection<string, Command> {
1313
if (this.client) command.initialize(this.client);
1414
if (Plugins.currentlyLoading) command.owner = Plugins.currentlyLoading;
1515
this.set(command.name, command);
16+
Logger.emit(LoggerEvents.COMMAND_REGISTERED, command);
1617
Logger.debug('Registered command', command.name, command.owner ? `(by plugin ${command.owner})` : '');
1718
} else Logger.warn('Command must be a instance of Command');
1819

@@ -23,6 +24,7 @@ export class CommandManager extends Collection<string, Command> {
2324
const command = this.get(commandName);
2425
if (command) {
2526
this.delete(commandName);
27+
Logger.emit(LoggerEvents.COMMAND_UNREGISTERED, command);
2628
Logger.debug('Unregistered command', command.name);
2729
}
2830

src/lib/managers/ComponentManager.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Collection } from 'discord.js';
22
import { Component } from '../structures/Component';
33
import type { GClient } from '../GClient';
4-
import { Logger } from '../structures/Logger';
4+
import { Logger, LoggerEvents } from '../structures/Logger';
55
import { Plugins } from './PluginManager';
66

77
export class ComponentManager extends Collection<string, Component> {
@@ -14,6 +14,7 @@ export class ComponentManager extends Collection<string, Component> {
1414
if (this.client) component.initialize(this.client);
1515
if (Plugins.currentlyLoading) component.owner = Plugins.currentlyLoading;
1616
this.set(component.name, component);
17+
Logger.emit(LoggerEvents.COMPONENT_REGISTERED, component);
1718
Logger.debug('Registered component', component.name, component.owner ? `(by plugin ${component.owner})` : '');
1819
} else Logger.warn('Component must be a instance of Component');
1920

@@ -24,6 +25,7 @@ export class ComponentManager extends Collection<string, Component> {
2425
const component = this.get(componentName);
2526
if (component) {
2627
this.delete(componentName);
28+
Logger.emit(LoggerEvents.COMPONENT_UNREGISTERED, component);
2729
Logger.debug('Unregistered component', component.name);
2830
}
2931

src/lib/managers/ListenerManager.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { GClient } from '../GClient';
22
import { Listener } from '../structures/Listener';
33
import { ClientEvents, Collection, WSEventType } from 'discord.js';
4-
import { Logger } from '../structures/Logger';
4+
import { Logger, LoggerEvents } from '../structures/Logger';
55
import { Plugins } from './PluginManager';
66

77
export class ListenerManager extends Collection<string, Listener> {
@@ -17,6 +17,7 @@ export class ListenerManager extends Collection<string, Listener> {
1717
if (this.client) this.initialize(listener);
1818
if (Plugins.currentlyLoading) listener.owner = Plugins.currentlyLoading;
1919
this.set(listener.name, listener);
20+
Logger.emit(LoggerEvents.LISTENER_REGISTERED, listener);
2021
Logger.debug(
2122
'Registered listener',
2223
listener.name,
@@ -43,6 +44,7 @@ export class ListenerManager extends Collection<string, Listener> {
4344
: this.client.off(listener.event as keyof ClientEvents, listener._run);
4445
}
4546

47+
Logger.emit(LoggerEvents.LISTENER_UNREGISTERED, listener);
4648
Logger.debug('Unregistered listener', listener.name, 'listening to', listener.event);
4749
}
4850

src/lib/managers/PluginManager.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Collection } from 'discord.js';
22
import { Plugin } from '../structures/Plugin';
33
import type { GClient } from '../GClient';
44
import { pluginFinder } from '../loaders/pluginFinder';
5-
import { Logger } from '../structures/Logger';
5+
import { Logger, LoggerEvents } from '../structures/Logger';
66

77
export class PluginManager extends Collection<string, Plugin> {
88
private client: GClient;
@@ -13,6 +13,7 @@ export class PluginManager extends Collection<string, Plugin> {
1313
if (this.has(plugin.name)) Logger.warn('Overwriting plugin', plugin.name);
1414
if (!Plugin.validate(plugin)) return;
1515
this.set(plugin.name, plugin);
16+
Logger.emit(LoggerEvents.PLUGIN_REGISTERED, plugin);
1617
Logger.debug('Registered plugin', plugin.name);
1718
} else Logger.warn('Plugin must be a instance of plugin');
1819

src/lib/structures/Logger.ts

+18
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,33 @@ import type { GlobalLogger } from 'js-logger';
66
import type { AutocompleteContext } from './contexts/AutocompleteContext';
77
import type { CommandContext } from './contexts/CommandContext';
88
import type { ComponentContext } from './contexts/ComponentContext';
9+
import type { Command } from './Command';
10+
import type { Component } from './Component';
11+
import type { Listener } from './Listener';
12+
import type { Plugin } from './Plugin';
913

1014
export enum LoggerEvents {
1115
'HANDLER_RUN' = 'handlerRun',
1216
'HANDLER_ERROR' = 'handlerError',
17+
'COMMAND_REGISTERED' = 'commandRegistered',
18+
'COMMAND_UNREGISTERED' = 'commandUnregistered',
19+
'COMPONENT_REGISTERED' = 'componentRegistered',
20+
'COMPONENT_UNREGISTERED' = 'componentUnregistered',
21+
'LISTENER_REGISTERED' = 'listenerRegistered',
22+
'LISTENER_UNREGISTERED' = 'listenerUnregistered',
23+
'PLUGIN_REGISTERED' = 'pluginRegistered',
1324
}
1425

1526
export interface LoggerEventsInterface {
1627
'handlerRun': (ctx: AutocompleteContext | CommandContext | ComponentContext) => void;
1728
'handlerError': (ctx: AutocompleteContext | CommandContext | ComponentContext, error: any) => void;
29+
'commandRegistered': (command: Command) => void;
30+
'commandUnregistered': (command: Command) => void;
31+
'componentRegistered': (component: Component) => void;
32+
'componentUnregistered': (component: Component) => void;
33+
'listenerRegistered': (listener: Listener) => void;
34+
'listenerUnregistered': (listener: Listener) => void;
35+
'pluginRegistered': (plugin: Plugin) => void;
1836
}
1937

2038
export declare interface LoggerClass {

0 commit comments

Comments
 (0)