mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-16 14:11:50 +00:00
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
![]() |
import { BaseRepository } from "./BaseRepository";
|
||
|
import { QueuedEventEmitter } from "../QueuedEventEmitter";
|
||
|
|
||
|
export class GuildEvents extends BaseRepository {
|
||
|
private queuedEventEmitter: QueuedEventEmitter;
|
||
|
private pluginListeners: Map<string, Map<string, any[]>>;
|
||
|
|
||
|
constructor(guildId) {
|
||
|
super(guildId);
|
||
|
this.queuedEventEmitter = new QueuedEventEmitter();
|
||
|
}
|
||
|
|
||
|
public on(pluginName: string, eventName: string, fn) {
|
||
|
this.queuedEventEmitter.on(eventName, fn);
|
||
|
|
||
|
if (!this.pluginListeners.has(pluginName)) {
|
||
|
this.pluginListeners.set(pluginName, new Map());
|
||
|
}
|
||
|
|
||
|
const pluginListeners = this.pluginListeners.get(pluginName);
|
||
|
if (!pluginListeners.has(eventName)) {
|
||
|
pluginListeners.set(eventName, []);
|
||
|
}
|
||
|
|
||
|
const pluginEventListeners = pluginListeners.get(eventName);
|
||
|
pluginEventListeners.push(fn);
|
||
|
}
|
||
|
|
||
|
public offPlugin(pluginName: string) {
|
||
|
const pluginListeners = this.pluginListeners.get(pluginName) || new Map();
|
||
|
for (const [eventName, listeners] of Array.from(pluginListeners.entries())) {
|
||
|
for (const listener of listeners) {
|
||
|
this.queuedEventEmitter.off(eventName, listener);
|
||
|
}
|
||
|
}
|
||
|
this.pluginListeners.delete(pluginName);
|
||
|
}
|
||
|
|
||
|
public emit(eventName: string, args: any[] = []) {
|
||
|
return this.queuedEventEmitter.emit(eventName, args);
|
||
|
}
|
||
|
}
|