Main commit

This commit is contained in:
2026-01-05 14:36:22 -03:00
commit ac90a9628d
9 changed files with 283 additions and 0 deletions

36
src/database.rb Normal file
View File

@@ -0,0 +1,36 @@
require 'pg'
class Database
def initialize
# Connect once when the bot starts
@conn = PG.connect(dbname: 'frugality_database')
init_tables
end
def init_tables
sql = <<~SQL
CREATE TABLE IF NOT EXISTS total_money (
user_id BIGINT PRIMARY KEY,
amount BIGINT DEFAULT 0
);
SQL
@conn.exec(sql)
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])
# 2. Check if the user exists
if result.num_tuples.zero?
return 0 # User has no money/row yet
else
# 3. Return the value (don't print it)
return result[0]['amount'].to_i
end
end
end