From c777b4c3585154e30208e656d19c0e3468f7739f Mon Sep 17 00:00:00 2001 From: ShuriZma Date: Fri, 7 Jun 2024 08:53:12 +0200 Subject: [PATCH] add fact counter --- src/bot.ts | 16 +++++++++++----- src/commands/fact.ts | 6 ++++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index aec7c57..bf33cb7 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -114,7 +114,7 @@ export class Bot { port: config.dbPort, }) - 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.execute('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, counter INT UNSIGNED NOT NULL DEFAULT 0);'); if (closeConnection) { this.endConection(); @@ -147,6 +147,12 @@ export class Bot { for (let index in results) { let result = results[index]; + fields.push({ + name: 'content', + value: result.content, + inline: false, + }); + fields.push({ name: 'id', value: result.id.toString(), @@ -154,14 +160,14 @@ export class Bot { }); fields.push({ - name: 'content', - value: result.content, + name: 'active', + value: result.active ? 'True' : 'False', inline: true, }); fields.push({ - name: 'active', - value: result.active ? 'True' : 'False', + name: 'counter', + value: result.counter.toString(), inline: true, }); diff --git a/src/commands/fact.ts b/src/commands/fact.ts index 155a7ea..44a7da8 100644 --- a/src/commands/fact.ts +++ b/src/commands/fact.ts @@ -10,9 +10,11 @@ export default { .setDescription('Get a logtastic fact.'), execute: async function (interaction: ChatInputCommandInteraction) { 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]; + const [results] = await bot.getConnection().promise().execute('SELECT * FROM facts WHERE active = 1 ORDER BY RAND() LIMIT 1;'); bot.endConection(); + + let fact = results[0]; + await bot.getConnection().promise().execute('UPDATE facts SET counter = counter + 1 WHERE id = ?', [fact.id]) console.log('Sent beaver fact with id: ' + fact.id); return await interaction.reply({