From 72c519a794efc1ac00a2934a80d136da292b6a59 Mon Sep 17 00:00:00 2001 From: ShuriZma Date: Fri, 7 Jun 2024 08:42:03 +0200 Subject: [PATCH] fix db connection timeout --- src/bot.ts | 10 ++++++++-- src/commands/addFact.ts | 2 +- src/commands/enableFact.ts | 2 +- src/commands/fact.ts | 2 +- src/commands/removeFact.ts | 2 +- 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index a2a755c..bd15cff 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -117,7 +117,7 @@ export class Bot { this.connection.query('CREATE TABLE if NOT EXISTS facts (id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, content TEXT NOT NULL, active TINYINT(1) NOT NULL DEFAULT 1)'); if (closeConnection) { - this.connection.end(); + this.endConection(); } } @@ -126,6 +126,12 @@ export class Bot { return this.connection; } + public endConection() { + this.connection.end(function (err) { + console.log(err); + }); + } + public static getInstance(): Bot { if (!Bot.instance) { Bot.instance = new Bot(); @@ -136,7 +142,7 @@ export class Bot { public async sendEmbed(msg: Message | ChatInputCommandInteraction) { const [results] = await this.getConnection().promise().execute('SELECT * FROM facts;'); - this.connection.end(); + this.endConection(); let fields = []; let embeds = []; diff --git a/src/commands/addFact.ts b/src/commands/addFact.ts index 8bce8a2..935584a 100644 --- a/src/commands/addFact.ts +++ b/src/commands/addFact.ts @@ -30,7 +30,7 @@ export default { 'INSERT INTO facts SET content = ?, active = ?;', [interaction.options.data.at(0).value, interaction.options.data.at(1)?.value ?? true] ); - bot.getConnection().end(); + bot.endConection(); console.log('Fact has been saved!'); return await interaction.reply({ diff --git a/src/commands/enableFact.ts b/src/commands/enableFact.ts index a1778f2..5e65cd9 100644 --- a/src/commands/enableFact.ts +++ b/src/commands/enableFact.ts @@ -24,7 +24,7 @@ export default { 'UPDATE facts SET active = 1 WHERE id = ?;', [interaction.options.data.at(0).value] ); - bot.getConnection().end(); + bot.endConection(); console.log('Fact has been enabled!'); return await interaction.reply({ diff --git a/src/commands/fact.ts b/src/commands/fact.ts index 4845fe8..155a7ea 100644 --- a/src/commands/fact.ts +++ b/src/commands/fact.ts @@ -12,7 +12,7 @@ export default { const bot = Bot.getInstance(); const [results] = await bot.getConnection().promise().query('SELECT * FROM facts WHERE active = 1 ORDER BY RAND() LIMIT 1;'); let fact = results[0]; - bot.getConnection().end(); + bot.endConection(); console.log('Sent beaver fact with id: ' + fact.id); return await interaction.reply({ diff --git a/src/commands/removeFact.ts b/src/commands/removeFact.ts index 3194133..fa6e63c 100644 --- a/src/commands/removeFact.ts +++ b/src/commands/removeFact.ts @@ -41,7 +41,7 @@ export default { console.log('Fact has been disabled!'); } - bot.getConnection().end(); + bot.endConection(); return await interaction.reply({ content: 'Done!',