Skip to content

Commit

Permalink
feat(#103): custom event and event manager
Browse files Browse the repository at this point in the history
  • Loading branch information
barthofu committed Jan 2, 2023
1 parent 9be9fc2 commit 8c77a18
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 10 deletions.
13 changes: 7 additions & 6 deletions src/events/custom/guildAdmin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import { Collection, GuildMember, PermissionFlagsBits, Role } from "discord.js"
import { ArgsOf, Client } from "discordx"
import { injectable } from "tsyringe"

import { Discord, Guard, On } from "@decorators"
import { Discord, Guard, On, OnCustom } from "@decorators"
import { Maintenance } from "@guards"
import { Logger } from "@services"
import { EventManager, Logger } from "@services"

@Discord()
@injectable()
export default class GuildAdminAddEvent {

constructor(
private logger: Logger
private logger: Logger,
private eventManager: EventManager
) {}

// =============================
Expand All @@ -31,7 +32,7 @@ export default class GuildAdminAddEvent {
this.logger.log(`${member.nickname} has been added as an admin`)
}

@On('guildAdminDelete')
@OnCustom('guildAdminDelete')
@Guard(
Maintenance
)
Expand Down Expand Up @@ -67,7 +68,7 @@ export default class GuildAdminAddEvent {
* @param {GuildMember} member
* @param {Collection<String, Role>} newAdminRoles
*/
client.emit('guildAdminAdd', newMember, newAdminRoles)
this.eventManager.emit('guildAdminAdd', newMember, newAdminRoles)
}
else if (oldMember.roles.cache.size > newMember.roles.cache.size) {

Expand All @@ -82,7 +83,7 @@ export default class GuildAdminAddEvent {
* @param {GuildMember} member
* @param {Collection<String, Role>} oldAdminRoles
*/
client.emit('guildAdminRemove', newMember, oldAdminRoles)
this.eventManager.emit('guildAdminRemove', newMember, oldAdminRoles)
}
}
}
7 changes: 4 additions & 3 deletions src/events/custom/simpleCommandCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { injectable } from "tsyringe"
import { Discord, On } from "@decorators"
import { Guild, User } from "@entities"
import { Maintenance } from "@guards"
import { Database, Logger, Stats } from "@services"
import { Database, EventManager, Logger, Stats } from "@services"
import { getPrefixFromMessage, syncUser } from "@utils/functions"

@Discord()
Expand All @@ -14,7 +14,8 @@ export default class SimpleCommandCreateEvent {
constructor(
private stats: Stats,
private logger: Logger,
private db: Database
private db: Database,
private eventManager: EventManager
) {}

// =============================
Expand Down Expand Up @@ -56,7 +57,7 @@ export default class SimpleCommandCreateEvent {
/**
* @param {SimpleCommandMessage} command
*/
client.emit('simpleCommandCreate', command)
this.eventManager.emit('simpleCommandCreate', command)
}
}
}
37 changes: 37 additions & 0 deletions src/services/EventManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { singleton } from 'tsyringe'

import { Logger } from '@services'

@singleton()
export class EventManager {

private _events: Map<string, Function[]> = new Map()

constructor(
private logger: Logger
) {
}

register(event: string, callback: Function): void {

this._events.set(event, [...this._events.get(event) || [], callback])
}

async emit(event: string, ...args: any[]): Promise<void> {

const callbacks = this._events.get(event)

if (!callbacks) return

for (const callback of callbacks) {

try {
await callback(...args)
} catch (error) {
if (error instanceof Error) {
this.logger.log(error.toString(), 'error', true)
}
}
}
}
}
1 change: 1 addition & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './Scheduler'
export * from './WebSocket'
export * from './Pastebin'
export * from './PluginsManager'
export * from './EventManager'
18 changes: 18 additions & 0 deletions src/utils/decorators/OnCustom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { resolveDependency } from '@utils/functions'

export const OnCustom = (event: string) => {

return function (
target: any,
propertyKey: string,
descriptor: PropertyDescriptor
) {

import('@services').then(async ({ EventManager }) => {

const eventManager = await resolveDependency(EventManager)

eventManager.register(event, descriptor.value)
})
}
}
3 changes: 2 additions & 1 deletion src/utils/decorators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export * from './SlashChoice'
export * from './SlashGroup'
export * from './ContextMenu'
export * from './Schedule'
export * from './WSOn'
export * from './WSOn'
export * from './OnCustom'

0 comments on commit 8c77a18

Please sign in to comment.