From a923fce12e7640b80544ec973120b0141bd51f90 Mon Sep 17 00:00:00 2001 From: csxkdv Date: Mon, 5 Jan 2026 19:27:23 -0300 Subject: [PATCH] Added a way to spend money. --- src/commands/subtract.rb | 52 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/commands/subtract.rb diff --git a/src/commands/subtract.rb b/src/commands/subtract.rb new file mode 100644 index 0000000..ca1ff51 --- /dev/null +++ b/src/commands/subtract.rb @@ -0,0 +1,52 @@ +# 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 Subtract + extend self + + def register(bot, db) + bot.register_application_command(:subtract, 'Take money from the wallet', server_id: ENV['TEST_SERVER_ID']) do |cmd| + cmd.integer('amount', "The amount you're spending.", required: true) + cmd.string('reason', "Reason you're spending money. Leave empty for default.", required: false) + end + + bot.application_command(:subtract) do |event| + amount = event.options['amount'].to_i + reason = event.options['reason'] ||= 'expense' + + if amount <= 0 + event.respond(content: "You either have 0, or you can't spend negative money.", ephemeral: true) + next + end + + user_id = event.user.id + + current_balance = db.get_currency(user_id) + + if current_balance < amount + event.respond(content: "You can't buy that, you have **#{current_balance}** coins.", ephemeral: true) + next + end + + db.update_balance(user_id, -amount, reason) + + new_balance = current_balance - amount + event.respond(content: "You spent **#{amount}** coins.\nReason: #{reason}\nYou now have: #{new_balance}") + end + end + end +end \ No newline at end of file