smogon / Porygon-Z

The Bot for the Official Smogon Discord (WIP)
https://discord.gg/smogon
MIT License
13 stars 6 forks source link

Startup Hook #21

Closed HoeenCoder closed 4 years ago

HoeenCoder commented 4 years ago

Create a way to hook methods from command/monitor files the ready event for the bot. I want this to be done so that events that need to be done when the bot first starts but also need the bot to be connected to discord to work can be fired at the appropriate time.

Alcremie commented 4 years ago

Similar to the pagination class, you could have an initialize/setup method on your command and monitor classes and take advantage of Collection#map returning an array with Promise.all:

await Promise.all([
    ...commands.map(command => command.initialize()),
    ...monitors.map(monitor => monitor.initialize()),
]);

That way, all of your initializers should be able to fire in parallel. You'd just have to make your ready listener asynchronous and include your initialize function on your abstract classes; they should probably return Promise<void>. Otherwise, you might want a more fine-tuned approach if your initializers deal with any rate-limited endpoint.

HoeenCoder commented 4 years ago

@Alcremie that sounds good yeah. The method would need to be static since we store commands/monitors as classes not built monitors but thats not an issue at all, the default on BaseCommand just needs to be no operation.

Concerning rate limits, discord.js itself handles rate limits freeing me from having to think about them bar "If we flood too much info out at once, the bot may reply slowly for a short time". This has not been an issue yet though, so I think were ok regardless. Do you want to take a whack at this or should I do it later?

Alcremie commented 4 years ago

Sure, I'll get to it tomorrow.