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)'); 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) { if (closeConnection) {
this.connection.end(); this.endConection();
} }
} }
@ -126,6 +126,12 @@ export class Bot {
return this.connection; return this.connection;
} }
public endConection() {
this.connection.end(function (err) {
console.log(err);
});
}
public static getInstance(): Bot { public static getInstance(): Bot {
if (!Bot.instance) { if (!Bot.instance) {
Bot.instance = new Bot(); Bot.instance = new Bot();
@ -136,7 +142,7 @@ export class Bot {
public async sendEmbed(msg: Message<boolean> | ChatInputCommandInteraction) { public async sendEmbed(msg: Message<boolean> | ChatInputCommandInteraction) {
const [results] = await this.getConnection().promise().execute('SELECT * FROM facts;'); const [results] = await this.getConnection().promise().execute('SELECT * FROM facts;');
this.connection.end(); this.endConection();
let fields = []; let fields = [];
let embeds = []; let embeds = [];

View File

@ -30,7 +30,7 @@ export default {
'INSERT INTO facts SET content = ?, active = ?;', 'INSERT INTO facts SET content = ?, active = ?;',
[interaction.options.data.at(0).value, interaction.options.data.at(1)?.value ?? true] [interaction.options.data.at(0).value, interaction.options.data.at(1)?.value ?? true]
); );
bot.getConnection().end(); bot.endConection();
console.log('Fact has been saved!'); console.log('Fact has been saved!');
return await interaction.reply({ return await interaction.reply({

View File

@ -24,7 +24,7 @@ export default {
'UPDATE facts SET active = 1 WHERE id = ?;', 'UPDATE facts SET active = 1 WHERE id = ?;',
[interaction.options.data.at(0).value] [interaction.options.data.at(0).value]
); );
bot.getConnection().end(); bot.endConection();
console.log('Fact has been enabled!'); console.log('Fact has been enabled!');
return await interaction.reply({ return await interaction.reply({

View File

@ -12,7 +12,7 @@ export default {
const bot = Bot.getInstance(); const bot = Bot.getInstance();
const [results] = await bot.getConnection().promise().query('SELECT * FROM facts WHERE active = 1 ORDER BY RAND() LIMIT 1;'); const [results] = await bot.getConnection().promise().query('SELECT * FROM facts WHERE active = 1 ORDER BY RAND() LIMIT 1;');
let fact = results[0]; let fact = results[0];
bot.getConnection().end(); bot.endConection();
console.log('Sent beaver fact with id: ' + fact.id); console.log('Sent beaver fact with id: ' + fact.id);
return await interaction.reply({ return await interaction.reply({

View File

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