summaryrefslogtreecommitdiff
path: root/bot.lua
blob: dc35b657804fbf290f969e3e29abd51bfa704b9f (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
furrybot.commands = {}
furrybot.requests = {}
furrybot.unsafe_commands = {}

local http, env, storage
local C = minetest.get_color_escape_sequence

furrybot.colors = {
	ping = C("#00DCFF"),
	system = C("#FFFA00"),
	error = C("#D70029"),
	detail = C("#FF6683"),
	roleplay = C("#FFD94E"),
	braces = C("#FFFAC0"),
	info = C("#00FFC3"),
	fun = C("#A0FF24"),
	random = C("#A300BE"),
	money = C("#A11600"),
}

-- helper functions

function furrybot.send(msg, color)
	minetest.send_chat_message("/me " .. furrybot.colors.braces .. "[" .. color .. msg .. furrybot.colors.braces .. "]")
end

function furrybot.ping(player, color)
	return furrybot.colors.ping .. "@" .. player .. color
end

function furrybot.ping_message(player, message, color)
	furrybot.send(furrybot.ping(player, color) .. ": " .. message, "")
end

function furrybot.error_message(player, error, detail)
	furrybot.ping_message(player, error .. (detail and furrybot.colors.detail .. " '" .. detail .. "'" .. furrybot.colors.error or "") .. ".", furrybot.colors.error)
end

function furrybot.parse_message(player, message, discord)
	if message:find("!") == 1 then
		local args = message:sub(2, #message):split(" ")
		local cmd = table.remove(args, 1)
		local func = furrybot.commands[cmd]
		if func then
			if furrybot.unsafe_commands[cmd] and discord then
				furrybot.error_message(player, "Sorry, you cannot run this command from discord: ", cmd)
			else
				func(player, unpack(args))
			end
		else
			furrybot.error_message(player, "Invalid command", cmd)
		end
	end
end

function furrybot.reload()
	local func, err = env.loadfile("clientmods/furrybot/bot.lua")
	if func then
		local old_fb = table.copy(furrybot)
		local status, init = pcall(func)
		if status then
			init(http, env, storage)
		else
			furrybot = old_fb
			return false, furrybot.colors.error .. "Error: " .. furrybot.colors.detail .. init
		end
	else
		return false, furrybot.colors.error .. "Syntax error: " .. furrybot.colors.detail .. err
	end
end

function furrybot.player_online(name)
	for _, n in ipairs(minetest.get_player_names()) do
		if name == n then
			return true
		end
	end
end

function furrybot.online_or_error(name, other, allow_self)
	if not other then
		furrybot.error_message(name, "You need to specify a player")
	elseif name == other and not allow_self then
		furrybot.error_message(name, "You need to specify a different player than yourself")
	elseif furrybot.player_online(other) then
		return true
	else
		furrybot.error_message(name, "Player not online", other)
	end
end

function furrybot.choose(list, color)
	return furrybot.colors.random .. list[math.random(#list)] .. color
end

function furrybot.random(min, max, color)
	return furrybot.colors.random .. math.random(min, max) .. color
end

function furrybot.http_request(url, name, callback)
	http.fetch({url = url}, function(res)
		if res.succeeded then
			callback(res.data)
		else
			furrybot.error_message(name, "Request failed with code", res.code)
		end
	end)
end

function furrybot.json_http_request(url, name, callback)
	furrybot.http_request(url, name, function(raw)
		local data = minetest.parse_json(raw)
		callback(data[1] or data)
	end)
end

function furrybot.strrandom(str, seed, ...)
	local v = 0
	local pr = PseudoRandom(seed)
	for i = 1, #str do
		v = v + str:byte(i) * pr:next()
	end
	return PseudoRandom(v):next(...)
end

function furrybot.repeat_string(str, times)
	local msg = ""
	for i = 1, times do
		msg = msg .. str
	end
	return msg
end

function furrybot.interactive_roleplay_command(action)
	return function(name, target)
		if furrybot.online_or_error(name, target) then
			furrybot.send(name .. " " .. action .. " " .. target .. ".", furrybot.colors.roleplay)
		end
	end
end

function furrybot.solo_roleplay_command(action)
	return function(name)
		furrybot.send(name .. " " .. action .. ".", furrybot.colors.roleplay)
	end
end

function furrybot.request_command(on_request, on_accept)
	return function(name, target)
		if furrybot.online_or_error(name, target) and on_request(name, target) ~= false then
			furrybot.requests[target] = {
				origin = name,
				func = on_accept,
			}
		end
	end
end

-- General purpose commands

function furrybot.commands.help()
	local keys = {}
	for k in pairs(furrybot.commands) do
		table.insert(keys, k)
	end
	furrybot.send("Available commands: " .. table.concat(keys, ", "), furrybot.colors.system)
end

function furrybot.commands.accept(name)
	local tbl = furrybot.requests[name]
	if tbl then
		furrybot.requests[name] = nil
		tbl.func(tbl.origin, name)
	else
		furrybot.error_message(name, "Nothing to accept")
	end
end
furrybot.unsafe_commands.accept = true

function furrybot.commands.deny(name)
	local tbl = furrybot.requests[name]
	if tbl then
		furrybot.requests[name] = nil
		furrybot.ping_message(name, "Denied request", furrybot.colors.system)
	else
		furrybot.error_message(name, "Nothing to deny")
	end
end
furrybot.unsafe_commands.deny = true

-- don't bug players that are running ClamityBot commands from discord
function furrybot.commands.status()
end

function furrybot.commands.cmd()
end

-- send load message
furrybot.send("FurryBot - " .. C("#170089") .. "https://github.com/EliasFleckenstein03/furrybot", furrybot.colors.system)

if furrybot.loaded then
	furrybot.send("Reloaded", furrybot.colors.system)
else
	furrybot.loaded = true
end

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

	for _, f in ipairs {"nsfw", "roleplay", "death", "economy", "random", "http"} do
		print(f)
		env.loadfile("clientmods/furrybot/" .. f .. ".lua")()(http, env, storage)
	end
end