Added a way to spend money.

This commit is contained in:
2026-01-05 19:27:23 -03:00
parent 6588d68cc4
commit a923fce12e

52
src/commands/subtract.rb Normal file
View File

@@ -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 <https://www.gnu.org/licenses/>.
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