client.on() not handling/accepting requests when awaiting response from SQLite when it is busy

This is how I register events in index.ts
for (const event of events) {
if (event.once) {
client.once(event.name, (...args) => event.execute(...args, commands, db));
} else {
client.on(event.name, (...args) => {
event.execute(...args, commands, db);
});
}
}
for (const event of events) {
if (event.once) {
client.once(event.name, (...args) => event.execute(...args, commands, db));
} else {
client.on(event.name, (...args) => {
event.execute(...args, commands, db);
});
}
}
This code is run on "InteractionCreate" event
await interaction.deferReply({ ephemeral: false });
await command.execute(interaction, db);
await interaction.deferReply({ ephemeral: false });
await command.execute(interaction, db);
The first interaction gets received and runs deferReply and then execute(interaction, db) But while the call to the database
await insertNewBanRecord(db, {
user_id: target_member.id,
guild_id: interaction.guild.id,
banner_id: banner_member.id,
ban_duration_minutes: duration,
reason: reason,
});
await insertNewBanRecord(db, {
user_id: target_member.id,
guild_id: interaction.guild.id,
banner_id: banner_member.id,
ban_duration_minutes: duration,
reason: reason,
});
is running inside the execute(interaction, db) Any more slash command requests are not going through the client.on(Events.InteractionCreate, ...) event handler. Only when the call to the database is finished it receives responses. I tried using sleep command to see if by stopping the interaction from executing, subsequent calls would be ignored or not, but they were not, all the InteractionCreate events were going through and deferred even though the previous one had not finished fully responding. I use better-sqlite3 and kysely query builder and orm. database.ts
const dialect = new SqliteDialect({
database: new SQLite("src/database/main.db"),
});

const db = new Kysely<Database>({
dialect,
});

export default db;
const dialect = new SqliteDialect({
database: new SQLite("src/database/main.db"),
});

const db = new Kysely<Database>({
dialect,
});

export default db;
index.ts
const client = new Client({
intents: [...],
});

for (const event of events) {
if (event.once) {
client.once(event.name, (...args) => event.execute(...args, commands, db));
} else {
client.on(event.name, (...args) => {
event.execute(...args, commands, db);
});
}
}
client.login(config.TOKEN);
const client = new Client({
intents: [...],
});

for (const event of events) {
if (event.once) {
client.once(event.name, (...args) => event.execute(...args, commands, db));
} else {
client.on(event.name, (...args) => {
event.execute(...args, commands, db);
});
}
}
client.login(config.TOKEN);
4 Replies
d.js toolkit
d.js toolkit6mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
luzede
luzedeOP6mo ago
I use DB Browser for SQLite, portable program to lock the database and make it busy, but I don't want that to stop event handlers from executing, I want the handlers to execute and report that there was an error with the database. It is inside try catch block and when the first event handler runs, it reports after 5 seconds
There was an error while banning the user.
SQLITE_BUSY
There was an error while banning the user.
SQLITE_BUSY
And during those 5 seconds, more calls are not handled by the handler await interaction.deferReply({ ephemeral: false }); This does not run during those 5 seconds when I send a new slash command request, and because 3 seconds already pass, it fails when it starts handling after 5 seconds because it got expired
Svitkona
Svitkona6mo ago
you can't, really better-sqlite3 is by design synchronous, and JS is single-threaded you can turn the timeout down to 0 (? i assume that will work. you'd need to test it) by passing the appropriate option https://github.com/WiseLibs/better-sqlite3/blob/HEAD/docs/api.md#new-databasepath-options so it'll immediately throw or you could run your queries in a worker thread, but that adds a lot of complexity anyway this isn't really an issue with the package itself, so you could try asking in #other-js-ts if those options don't work for you
luzede
luzedeOP6mo ago
Thank you, that really helped, for now setting timeout to something less than 1 second will work for me. In the future I will at least know to use an asynchronous database. ❤️
Want results from more Discord servers?
Add your server