summaryrefslogtreecommitdiff
path: root/economy.lua
blob: e27572b70f30ba6a281e08c7d9ef1b95f3547abc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
local http, env, storage
local C = minetest.get_color_escape_sequence

function furrybot.get_money(name)
	local key = name .. ".money"
	if storage:contains(key) then
		return storage:get_int(key)
	else
		return 100
	end
end

function furrybot.set_money(name, money)
	storage:set_int(name .. ".money", money)
end

function furrybot.add_money(name, add)
	local money = furrybot.get_money(name)
	furrybot.set_money(name, money + add)
end

function furrybot.take_money(name, remove)
	local money = furrybot.get_money(name)
	local new = money - remove
	if new < 0 then
		return false
	else
		furrybot.set_money(name, new)
		return true
	end
end

function furrybot.money(money, color)
	return furrybot.colors.money .. "$" .. money .. color
end

furrybot.commands.money = {
	func = function(name, target)
		target = target or name
		furrybot.ping_message(name, (target == name and "You have " or target .. " has ") .. furrybot.money(furrybot.get_money(target), furrybot.colors.system) .. ".", furrybot.colors.system)
	end,
}

furrybot.commands.pay = {
	unsafe = true,
	func = function(name, target, number)
		if furrybot.online_or_error(name, target) then
			local money = tonumber(number or "")
			if not money or money <= 0 or math.floor(money) ~= money then
				furrybot.error_message(name, "Invalid amount of money")
			else
				if furrybot.take_money(name, money) then
					furrybot.add_money(target, money)
					furrybot.ping_message(target, name .. " has payed you " .. furrybot.money(money, furrybot.colors.system) .. ".", furrybot.colors.system)
				else
					furrybot.error_message(name, "You don't have enough money")
				end
			end
		end
	end,
}

return function(_http, _env, _storage)
	http, env, storage = _http, _env, _storage
end