Describe the Issue
I'm aware by design waitForGuilds flag is only supposed to wait for all guilds to load before the ready event fires, however allowing other events to fire before ready can cause problems. Basically this design can lead to unsafe execution order and runtime errors.
In my experience, I have a countMessage.js event that listens for any messages in a specified channel. The channel is queried from an entry in my database, however the database connection is established in ready.js file, which is only fired after all the guilds load. When I launched the bot, it would listen for messages in all channels, the event fires and a query is sent the database to read the channel ID, using client.db.query . However, client.db was initialized in ready.js, which has not been fired, causing the bot to throw an error at every message listened and either crash or pass it to the main event error handler which spams the chat or logs with an "Error occured" message.
To Reproduce
main.js:-
require("dotenv").config()
const { Client } = require("@fluxerjs/core")
const loadEvents = require("./handlers/loadEvents")
const loadCommands = require("./handlers/loadCommands")
const client = new Client({
intents: 0,
waitForGuilds: true
})
client.commands = new Map()
loadCommands(client)
loadEvents(client)
client.login(process.env.FLUXER_BOT_TOKEN)
This code will load all the commands and events.
ready.js:-
require("dotenv").config();
const { Events } = require("@fluxerjs/core");
const mysql2 = require("mysql2/promise");
module.exports = {
name: Events.Ready,
async execute(client) {
try {
client.db = mysql2.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
});
} catch (err) {
console.log(err)
}
}
};
This code would initialize client.db.
messageEvent.js:-
require("dotenv").config();
module.exports = {
name: Events.MessageCreate,
async execute(client, message) {
const [rowsSettings] = await client.db.query(
"SELECT * FROM community_settings WHERE community_id = ?",
[message.guild.id]
);
const settings = rowsSettings[0];
if (settings.channel_id === message.channel.id) {
await message.send("testing");
}
}
};
This code will send a message in the channel_id channel.
Expected behavior
Upon running, you shall notice while the bot waits for all guilds to loads, it fires the messageEvent.js before ready.js, that means client.db is not initialized, causing the bot to crash.
Making sure all events including ready fire after the bot has loaded all guilds would avoid unexpected runtime errors.
Environment
- @fluxerjs/core version: ^1.2.3
- Node.js version: v24.13.0
- OS: Windows 11
Acknowledgements
@f3tchcodes
@ravener
Describe the Issue
I'm aware by design
waitForGuildsflag is only supposed to wait for all guilds to load before the ready event fires, however allowing other events to fire before ready can cause problems. Basically this design can lead to unsafe execution order and runtime errors.In my experience, I have a
countMessage.jsevent that listens for any messages in a specified channel. The channel is queried from an entry in my database, however the database connection is established inready.jsfile, which is only fired after all the guilds load. When I launched the bot, it would listen for messages in all channels, the event fires and a query is sent the database to read the channel ID, usingclient.db.query. However,client.dbwas initialized inready.js, which has not been fired, causing the bot to throw an error at every message listened and either crash or pass it to the main event error handler which spams the chat or logs with an "Error occured" message.To Reproduce
main.js:-
This code will load all the commands and events.
ready.js:-
This code would initialize
client.db.messageEvent.js:-
This code will send a message in the
channel_idchannel.Expected behavior
Upon running, you shall notice while the bot waits for all guilds to loads, it fires the
messageEvent.jsbeforeready.js, that meansclient.dbis not initialized, causing the bot to crash.Making sure all events including ready fire after the bot has loaded all guilds would avoid unexpected runtime errors.
Environment
Acknowledgements
@f3tchcodes
@ravener