From 86e1af1a6669ba88d43c1ce3fa46c1ba6b23567b Mon Sep 17 00:00:00 2001 From: csxkdv Date: Mon, 5 Jan 2026 19:16:04 -0300 Subject: [PATCH] Updated database content, new "add" command for earnings. --- src/commands/add.rb | 38 ++++++++++++++++++++++++++++++++++++ src/commands/echo.rb | 1 + src/database.rb | 46 +++++++++++++++++++++++++++++++++++++++----- 3 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 src/commands/add.rb diff --git a/src/commands/add.rb b/src/commands/add.rb new file mode 100644 index 0000000..56aa3dc --- /dev/null +++ b/src/commands/add.rb @@ -0,0 +1,38 @@ +# FrugalityBot +# Copyright (C) 2026 Eri (csxkdv/nxkdv) nxkdv@thenight.club +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +module Commands + module Add + extend self + + def register(bot, db) + bot.register_application_command(:add, 'Adds money to the wallet.', server_id: ENV['TEST_SERVER_ID']) do |cmd| + cmd.integer('amount', 'The amount you want to add.', required: true) + cmd.string('reason', "Reason you're adding money to the wallet. Leave empty for default.", required: false) + end + + bot.application_command(:add) do |event| + user_id = event.user.id + amount = event.options['amount'] + reason = event.options['reason'] ||= 'transaction' + + db.update_balance(user_id, amount, reason) + + event.respond(content: "Added: #{amount} to the wallet.\nReason: #{reason}") + end + end + end +end \ No newline at end of file diff --git a/src/commands/echo.rb b/src/commands/echo.rb index 2bc25f5..91aa6ff 100644 --- a/src/commands/echo.rb +++ b/src/commands/echo.rb @@ -17,6 +17,7 @@ module Commands module Echo extend self + def register(bot, _db) bot.register_application_command(:echo, 'Repeats what you say', server_id: ENV['TEST_SERVER_ID']) do |cmd| cmd.string('message', 'The text you want the bot to repeat', required: true) diff --git a/src/database.rb b/src/database.rb index 28e495e..8676614 100644 --- a/src/database.rb +++ b/src/database.rb @@ -19,27 +19,44 @@ require 'pg' class Database def initialize # Connect once when the bot starts - @conn = PG.connect(dbname: 'frugality_database') + @conn = PG.connect(dbname: 'fgbot_db') init_tables end def init_tables - sql = <<~SQL - CREATE TABLE IF NOT EXISTS total_money ( + sql_wallet = <<~SQL + CREATE TABLE IF NOT EXISTS wallets ( user_id BIGINT PRIMARY KEY, amount BIGINT DEFAULT 0 ); SQL - @conn.exec(sql) + sql_ledger = <<~SQL + CREATE TABLE IF NOT EXISTS transactions ( + id SERIAL PRIMARY KEY, + user_id BIGINT NOT NULL, + amount BIGINT NOT NULL, + reason VARCHAR(50), + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ); + SQL + + sql_index = <<~SQL + CREATE INDEX IF NOT EXISTS idx_user_history + ON transactions(user_id, created_at); + SQL + + @conn.exec(sql_wallet) + @conn.exec(sql_ledger) + @conn.exec(sql_index) puts "Database tables have been initialized." end # We pass the user_id def get_currency(user_id) # 1. Run the query using parameters ($1) to prevent SQL injection - result = @conn.exec_params("SELECT amount FROM total_money WHERE user_id = $1", [user_id]) + result = @conn.exec_params("SELECT amount FROM wallets WHERE user_id = $1", [user_id]) # 2. Check if the user exists if result.num_tuples.zero? @@ -49,4 +66,23 @@ class Database return result[0]['amount'].to_i end end + + def update_balance(user_id, amount, reason = "transaction") + @conn.transaction do + @conn.exec_params( + "INSERT INTO transactions (user_id, amount, reason) VALUES ($1, $2, $3)", + [user_id, amount, reason] + ) + + # We update the user's wallet + sql_update = <<~SQL + INSERT INTO wallets (user_id, amount) + VALUES ($1, $2) + ON CONFLICT (user_id) + DO UPDATE SET amount = wallets.amount + $2 + SQL + + @conn.exec_params(sql_update, [user_id, amount]) + end + end end \ No newline at end of file