add fact counter

This commit is contained in:
ShuriZma 2024-06-07 08:53:12 +02:00
parent a0003871c2
commit c777b4c358
Signed by: ShuriZma
GPG Key ID: 8D289758EE9B8074
2 changed files with 15 additions and 7 deletions

View File

@ -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,
});

View File

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