Updated database content, new "add" command for earnings.
This commit is contained in:
38
src/commands/add.rb
Normal file
38
src/commands/add.rb
Normal file
@@ -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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
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
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user