fix db connection timeout

This commit is contained in:
ShuriZma 2024-06-07 08:42:03 +02:00
parent 2a188a91f4
commit 72c519a794
Signed by: ShuriZma
GPG Key ID: 8D289758EE9B8074
5 changed files with 12 additions and 6 deletions

View File

@ -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<boolean> | ChatInputCommandInteraction) {
const [results] = await this.getConnection().promise().execute('SELECT * FROM facts;');
this.connection.end();
this.endConection();
let fields = [];
let embeds = [];

View File

@ -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({

View File

@ -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({

View File

@ -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({

View File

@ -41,7 +41,7 @@ export default {
console.log('Fact has been disabled!');
}
bot.getConnection().end();
bot.endConection();
return await interaction.reply({
content: 'Done!',