Premature execution of ready.js
Closed this issue · 5 comments
Version of Komada
v0.18.1
Are core files modified?
No
Describe the problem
The ready.js event is executed before komada is even finished loading all of the other components, such as the functions.
Expected Behaviour
Komada loads all available components, such as events, functions, commands, monitors, and so on, and THEN executes ready.js event if exists once everything is loaded and Discord is connected to. That way, a required database.js function is loaded, which ready.js uses to prune out banned users who snuck in the guild when the bot was offline.
Actual Behaviour
Komada loads all event handlers, then executes ready.js, then loads the rest of the components like commands, functions, and so on. This causes the bot to error because my ready.js needs the database.js function to prune banned users.
Dirty workaround
For anyone else who may have this issue, I set my user pruning functions inside a 15 second setTimeout to give the bot 15 seconds to load the rest of the components before pruning. This is not supposed to be a production fix though (sometimes the bot may take more than 15 seconds).
Steps to Reproduce.
My explanations above should answer this question easily.
In fact, you should do the database stuff in a function init. Create a function and write the code:
exports.init = (client) => { //code }
. Whatever you write inside this will be executed at startup. You should have all the stuff from Discord ready and loaded for when you run it.
Are you sure? Komada console output indicates that only the events are loaded in when ready.js firesl it hasn't even touched the functions yet.
I'm sure, right now I have launched my bot loading 3 big databases in which involves guild and user objects inside function init.
Inside my ready.js event I have just my clientUser.setGame().
Okay, I see what I did wrong. I had tried your method before but realised it wasn't inside exports.init, which passes the client object necessary to execute the tasks. No bug here.
Working without problems now?