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, 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) { if (closeConnection) {
this.endConection(); this.endConection();
@ -147,6 +147,12 @@ export class Bot {
for (let index in results) { for (let index in results) {
let result = results[index]; let result = results[index];
fields.push({
name: 'content',
value: result.content,
inline: false,
});
fields.push({ fields.push({
name: 'id', name: 'id',
value: result.id.toString(), value: result.id.toString(),
@ -154,14 +160,14 @@ export class Bot {
}); });
fields.push({ fields.push({
name: 'content', name: 'active',
value: result.content, value: result.active ? 'True' : 'False',
inline: true, inline: true,
}); });
fields.push({ fields.push({
name: 'active', name: 'counter',
value: result.active ? 'True' : 'False', value: result.counter.toString(),
inline: true, inline: true,
}); });

View File

@ -10,9 +10,11 @@ export default {
.setDescription('Get a logtastic fact.'), .setDescription('Get a logtastic fact.'),
execute: async function (interaction: ChatInputCommandInteraction) { execute: async function (interaction: ChatInputCommandInteraction) {
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().execute('SELECT * FROM facts WHERE active = 1 ORDER BY RAND() LIMIT 1;');
let fact = results[0];
bot.endConection(); 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); console.log('Sent beaver fact with id: ' + fact.id);
return await interaction.reply({ return await interaction.reply({