- I'm writing an event handler 'manager' such that I can group event handlers by bot features.
- For example, I want to have ExperienceEventHandler which contains functions for the MessageCreate and MessageReactionAdd events.
export const ExperienceEventHandler = new Collection<Events, (...parameters: any) => Promise<void>>([
[Events.MessageCreate, async (message: Message) => {
console.log(message)
}],
[Events.MessageReactionAdd, async (messageReaction: MessageReaction, user: User) => {
console.log(messageReaction, user)
}]
])
- Using the client.on() method, I want to call a method like EventHandlerManager.handleEvent() which will take the event and the parameters.
client.on(Events.MessageCreate, async (message) => {
eventHandlerManager.handleEvent(Events.MessageCreate, message)
})
client.on(Events.MessageReactionAdd, async (messageReaction, user) => {
eventHandlerManager.handleEvent(Events.MessageReactionAdd, messageReaction, user)
})
- This will then loop through the registered event handlers and execute the relevant functions.
export class EventHandlerManager {
private eventHandlers = [ExperienceEventHandler]
public async handleEvent(event: Events, ...parameters: any) {
for (const eventHandler of this.eventHandlers) {
await eventHandler.get(event)?.(...parameters)
}
}
}
The good thing is that, in its current state, it works. My problem is with the types and more specifically, using any for the parameters. The difficult part is that each event requires a unique set of parameters and the parameters chosen depend on the event that is passed. I've fought with ChatGPT over the problem and tried my own ideas but couldn't figure out how to execute them correctly: