fix db connection timeout

fix embed field length
This commit is contained in:
ShuriZma 2024-06-07 08:09:00 +02:00
parent f2d39cce0f
commit 2a188a91f4
Signed by: ShuriZma
GPG Key ID: 8D289758EE9B8074
5 changed files with 15 additions and 6 deletions

View File

@ -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<any, any> = 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<boolean> | 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;
}

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();
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();
console.log('Fact has been enabled!');
return await interaction.reply({

View File

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

View File

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