]> git.lizzy.rs Git - furrybot.git/blob - bot.lua
Add book command and sort help command output alphabethically
[furrybot.git] / bot.lua
1 furrybot.commands = {}
2 furrybot.requests = {}
3 furrybot.unsafe_commands = {}
4
5 local http, env, storage
6 local C = minetest.get_color_escape_sequence
7
8 furrybot.colors = {
9         ping = C("#00DCFF"),
10         system = C("#FFFA00"),
11         error = C("#D70029"),
12         detail = C("#FF6683"),
13         roleplay = C("#FFD94E"),
14         braces = C("#FFFAC0"),
15         info = C("#00FFC3"),
16         fun = C("#A0FF24"),
17         random = C("#A300BE"),
18         money = C("#A11600"),
19 }
20
21 -- helper functions
22
23 function furrybot.send(msg, color)
24         minetest.send_chat_message("/me " .. furrybot.colors.braces .. "[" .. color .. msg .. furrybot.colors.braces .. "]")
25 end
26
27 function furrybot.ping(player, color)
28         return furrybot.colors.ping .. "@" .. player .. color
29 end
30
31 function furrybot.ping_message(player, message, color)
32         furrybot.send(furrybot.ping(player, color) .. ": " .. message, "")
33 end
34
35 function furrybot.error_message(player, error, detail)
36         furrybot.ping_message(player, error .. (detail and furrybot.colors.detail .. " '" .. detail .. "'" .. furrybot.colors.error or "") .. ".", furrybot.colors.error)
37 end
38
39 function furrybot.parse_message(player, message, discord)
40         if message:find("!") == 1 and not furrybot.ignored[player] then
41                 local args = message:sub(2, #message):split(" ")
42                 local cmd = table.remove(args, 1)
43                 local func = furrybot.commands[cmd]
44                 if func then
45                         if furrybot.unsafe_commands[cmd] and discord then
46                                 furrybot.error_message(player, "Sorry, you cannot run this command from discord: ", cmd)
47                         else
48                                 func(player, unpack(args))
49                         end
50                 else
51                         furrybot.error_message(player, "Invalid command", cmd)
52                 end
53         end
54 end
55
56 function furrybot.reload()
57         local func, err = env.loadfile("clientmods/furrybot/bot.lua")
58         if func then
59                 local old_fb = table.copy(furrybot)
60                 local status, init = pcall(func)
61
62                 if status then
63                         status, init = init(http, env, storage)
64                 end
65
66                 if not status then
67                         furrybot = old_fb
68                         furrybot.send("Error: " .. furrybot.colors.detail .. init, furrybot.colors.error)
69                 end
70         else
71                 furrybot.send("Syntax error: " .. furrybot.colors.detail .. err, furrybot.colors.error)
72         end
73 end
74
75 function furrybot.player_online(name)
76         for _, n in ipairs(minetest.get_player_names()) do
77                 if name == n then
78                         return true
79                 end
80         end
81 end
82
83 function furrybot.online_or_error(name, other, allow_self)
84         if not other then
85                 furrybot.error_message(name, "You need to specify a player")
86         elseif name == other and not allow_self then
87                 furrybot.error_message(name, "You need to specify a different player than yourself")
88         elseif furrybot.player_online(other) then
89                 return true
90         else
91                 furrybot.error_message(name, "Player not online", other)
92         end
93 end
94
95 function furrybot.choose(list, color)
96         return furrybot.colors.random .. list[math.random(#list)] .. color
97 end
98
99 function furrybot.random(min, max, color)
100         return furrybot.colors.random .. math.random(min, max) .. color
101 end
102
103 function furrybot.http_request(url, name, callback)
104         http.fetch({url = url}, function(res)
105                 if res.succeeded then
106                         callback(res.data)
107                 else
108                         furrybot.error_message(name, "Request failed with code", res.code)
109                 end
110         end)
111 end
112
113 function furrybot.json_http_request(url, name, callback)
114         furrybot.http_request(url, name, function(raw)
115                 local data = minetest.parse_json(raw)
116                 callback(data[1] or data)
117         end)
118 end
119
120 function furrybot.strrandom(str, seed, ...)
121         local v = 0
122         local pr = PseudoRandom(seed)
123         for i = 1, #str do
124                 v = v + str:byte(i) * pr:next()
125         end
126         return PseudoRandom(v):next(...)
127 end
128
129 function furrybot.repeat_string(str, times)
130         local msg = ""
131         for i = 1, times do
132                 msg = msg .. str
133         end
134         return msg
135 end
136
137 function furrybot.interactive_roleplay_command(action)
138         return function(name, target)
139                 if furrybot.online_or_error(name, target) then
140                         furrybot.send(name .. " " .. action .. " " .. target .. ".", furrybot.colors.roleplay)
141                 end
142         end
143 end
144
145 function furrybot.solo_roleplay_command(action)
146         return function(name)
147                 furrybot.send(name .. " " .. action .. ".", furrybot.colors.roleplay)
148         end
149 end
150
151 function furrybot.request_command(on_request, on_accept)
152         return function(name, target)
153                 if furrybot.online_or_error(name, target) and on_request(name, target) ~= false then
154                         furrybot.requests[target] = {
155                                 origin = name,
156                                 func = on_accept,
157                         }
158                 end
159         end
160 end
161
162 -- General purpose commands
163
164 function furrybot.commands.help()
165         local commands = {}
166
167         for cmd in pairs(furrybot.commands) do
168                 table.insert(commands, cmd)
169         end
170
171         table.sort(commands)
172
173         furrybot.send("Available commands: " .. table.concat(commands, ", "), furrybot.colors.system)
174 end
175
176 function furrybot.commands.accept(name)
177         local tbl = furrybot.requests[name]
178         if tbl then
179                 furrybot.requests[name] = nil
180                 tbl.func(tbl.origin, name)
181         else
182                 furrybot.error_message(name, "Nothing to accept")
183         end
184 end
185 furrybot.unsafe_commands.accept = true
186
187 function furrybot.commands.deny(name)
188         local tbl = furrybot.requests[name]
189         if tbl then
190                 furrybot.requests[name] = nil
191                 furrybot.ping_message(name, "Denied request", furrybot.colors.system)
192         else
193                 furrybot.error_message(name, "Nothing to deny")
194         end
195 end
196 furrybot.unsafe_commands.deny = true
197
198 -- don't bug players that are running ClamityBot commands from discord
199 function furrybot.commands.status()
200 end
201
202 function furrybot.commands.cmd()
203 end
204
205 return function(_http, _env, _storage)
206         http, env, storage = _http, _env, _storage
207
208         for _, f in ipairs {"nsfw", "roleplay", "death", "economy", "random", "http", "operator", "bullshit"} do
209                 local func, err = env.loadfile("clientmods/furrybot/" .. f .. ".lua")
210
211                 if not func then
212                         return false, err
213                 end
214
215                 func()(http, env, storage)
216         end
217
218         furrybot.send("FurryBot - " .. C("#170089") .. "https://github.com/EliasFleckenstein03/furrybot", furrybot.colors.system)
219
220         if furrybot.loaded then
221                 furrybot.send("Reloaded", furrybot.colors.system)
222         else
223                 furrybot.loaded = true
224         end
225
226         return true
227 end