From 2a188a91f45444241f4a6ee8bda5a019c9bf5018 Mon Sep 17 00:00:00 2001 From: ShuriZma Date: Fri, 7 Jun 2024 08:09:00 +0200 Subject: [PATCH] fix db connection timeout fix embed field length --- src/bot.ts | 15 +++++++++++---- src/commands/addFact.ts | 2 +- src/commands/enableFact.ts | 2 +- src/commands/fact.ts | 1 + src/commands/removeFact.ts | 1 + 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index dc27752..a2a755c 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -27,7 +27,7 @@ export class Bot { public init() { const client = new Client({intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent]}); - this.connectDatabase() + this.connectDatabase(true); this.registerCommands(); const commands: Collection = new Collection(); @@ -105,7 +105,7 @@ export class Bot { })(); } - private connectDatabase() { + private connectDatabase(closeConnection: boolean) { this.connection = sql.createConnection({ host: config.dbHost, user: config.dbUser, @@ -115,9 +115,14 @@ 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(); + } } public getConnection() { + this.connectDatabase(false); return this.connection; } @@ -130,7 +135,9 @@ export class Bot { } public async sendEmbed(msg: Message | ChatInputCommandInteraction) { - const [results] = await this.connection.promise().execute('SELECT * FROM facts;'); + const [results] = await this.getConnection().promise().execute('SELECT * FROM facts;'); + this.connection.end(); + let fields = []; let embeds = []; for (let index in results) { @@ -154,7 +161,7 @@ export class Bot { inline: true, }); - if (fields.length < 27) { + if (fields.length < 24) { continue; } diff --git a/src/commands/addFact.ts b/src/commands/addFact.ts index 6a0305a..8bce8a2 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(); console.log('Fact has been saved!'); return await interaction.reply({ diff --git a/src/commands/enableFact.ts b/src/commands/enableFact.ts index 627c6c7..a1778f2 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(); console.log('Fact has been enabled!'); return await interaction.reply({ diff --git a/src/commands/fact.ts b/src/commands/fact.ts index fa26219..4845fe8 100644 --- a/src/commands/fact.ts +++ b/src/commands/fact.ts @@ -12,6 +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(); 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 c609541..3194133 100644 --- a/src/commands/removeFact.ts +++ b/src/commands/removeFact.ts @@ -41,6 +41,7 @@ export default { console.log('Fact has been disabled!'); } + bot.getConnection().end(); return await interaction.reply({ content: 'Done!',