]> git.lizzy.rs Git - furrybot.git/blob - bot.lua
Proper badge support
[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         rpg = 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.recieve(rawmsg)
40         local msg = minetest.strip_colors(rawmsg)
41         local nameidx = msg:find("<")
42         local first_byte = rawmsg:byte(1)
43         if nameidx and (first_byte == 60 or first_byte == 27) then
44                 local idx = msg:find(">")
45                 local player = msg:sub(nameidx + 1, idx - 1)
46                 local sidx = idx + 2
47                 if msg:sub(idx + 1, idx + 1) == ":" then
48                         sidx = sidx + 1
49                 end
50                 local message = msg:sub(sidx, #msg)
51                 if message:find("!") == 1 then
52                         local args = message:sub(2, #message):split(" ")
53                         local cmd = table.remove(args, 1)
54                         local func = furrybot.commands[cmd]
55                         if func then
56                                 if furrybot.unsafe_commands[cmd] and first_byte == 27 and rawmsg:sub(2, 12) == "(c@#63d269)" and nameidx == 1 then
57                                         furrybot.error_message(player, "Sorry, you cannot run this command from discord", cmd)
58                                 else
59                                         func(player, unpack(args))
60                                 end
61                         else
62                                 furrybot.error_message(player, "Invalid command", cmd)
63                         end
64                 end
65         end
66 end
67
68 function furrybot.reload()
69         local func, err = env.loadfile("clientmods/furrybot/bot.lua")
70         if func then
71                 local old_fb = table.copy(furrybot)
72                 local status, init = pcall(func)
73                 if status then
74                         init(http, env, storage)
75                 else
76                         furrybot = old_fb
77                         return false, furrybot.colors.error .. "Error: " .. furrybot.colors.detail .. init
78                 end
79         else
80                 return false, furrybot.colors.error .. "Syntax error: " .. furrybot.colors.detail .. err
81         end
82 end
83
84 function furrybot.player_online(name)
85         for _, n in ipairs(minetest.get_player_names()) do
86                 if name == n then
87                         return true
88                 end
89         end
90 end
91
92 function furrybot.online_or_error(name, other, allow_self)
93         if not other then
94                 furrybot.error_message(name, "You need to specify a player")
95         elseif name == other and not allow_self then
96                 furrybot.error_message(name, "You need to specify a different player than yourself")
97         elseif furrybot.player_online(other) then
98                 return true
99         else
100                 furrybot.error_message(name, "Player not online", other)
101         end
102 end
103
104 function furrybot.choose(list, color)
105         return furrybot.colors.random .. list[math.random(#list)] .. color
106 end
107
108 function furrybot.random(min, max, color)
109         return furrybot.colors.random .. math.random(min, max) .. color
110 end
111
112 function furrybot.http_request(url, name, callback)
113         http.fetch({url = url}, function(res)
114                 if res.succeeded then
115                         callback(res.data)
116                 else
117                         furrybot.error_message(name, "Request failed with code", res.code)
118                 end
119         end)
120 end
121
122 function furrybot.json_http_request(url, name, callback)
123         furrybot.http_request(url, name, function(raw)
124                 local data = minetest.parse_json(raw)
125                 callback(data[1] or data)
126         end)
127 end
128
129 function furrybot.strrandom(str, seed, ...)
130         local v = 0
131         local pr = PseudoRandom(seed)
132         for i = 1, #str do
133                 v = v + str:byte(i) * pr:next()
134         end
135         return PseudoRandom(v):next(...)
136 end
137
138 function furrybot.repeat_string(str, times)
139         local msg = ""
140         for i = 1, times do
141                 msg = msg .. str
142         end
143         return msg
144 end
145
146 function furrybot.interactive_rpg_command(action)
147         return function(name, target)
148                 if furrybot.online_or_error(name, target) then
149                         furrybot.send(name .. " " .. action .. " " .. target .. ".", furrybot.colors.rpg)
150                 end
151         end
152 end
153
154 function furrybot.solo_rpg_command(action)
155         return function(name)
156                 furrybot.send(name .. " " .. action .. ".", furrybot.colors.rpg)
157         end
158 end
159
160 function furrybot.request_command(on_request, on_accept)
161         return function(name, target)
162                 if furrybot.online_or_error(name, target) and on_request(name, target) ~= false then
163                         furrybot.requests[target] = {
164                                 origin = name,
165                                 func = on_accept,
166                         }
167                 end
168         end
169 end
170
171 function furrybot.get_money(name)
172         local key = name .. ".money"
173         if storage:contains(key) then
174                 return storage:get_int(key)
175         else
176                 return 100
177         end
178 end
179
180 function furrybot.set_money(name, money)
181         storage:set_int(name .. ".money", money)
182 end
183
184 function furrybot.add_money(name, add)
185         local money = furrybot.get_money(name)
186         furrybot.set_money(name, money + add)
187 end
188
189 function furrybot.take_money(name, remove)
190         local money = furrybot.get_money(name)
191         local new = money - remove
192         if new < 0 then
193                 return false
194         else
195                 furrybot.set_money(name, new)
196                 return true
197         end
198 end
199
200 function furrybot.money(money, color)
201         return furrybot.colors.money .. "$" .. money .. color
202 end
203
204 -- Commands
205
206 -- system
207 function furrybot.commands.help()
208         local keys = {}
209         for k in pairs(furrybot.commands) do
210                 table.insert(keys, k)
211         end
212         furrybot.send("Available commands: " .. table.concat(keys, ", "), furrybot.colors.system)
213 end
214
215 function furrybot.commands.accept(name)
216         local tbl = furrybot.requests[name]
217         if tbl then
218                 furrybot.requests[name] = nil
219                 tbl.func(tbl.origin, name)
220         else
221                 furrybot.error_message(name, "Nothing to accept")
222         end
223 end
224 furrybot.unsafe_commands.accept = true
225
226 function furrybot.commands.deny(name)
227         local tbl = furrybot.requests[name]
228         if tbl then
229                 furrybot.requests[name] = nil
230                 furrybot.ping_message(name, "Denied request", furrybot.colors.system)
231         else
232                 furrybot.error_message(name, "Nothing to deny")
233         end
234 end
235 furrybot.unsafe_commands.deny = true
236
237 -- don't bug players that are running ClamityBot commands from discord
238 function furrybot.commands.status()
239 end
240
241 function furrybot.commands.cmd()
242 end
243
244 -- rpg
245 furrybot.commands.cry = furrybot.solo_rpg_command("cries")
246 furrybot.commands.laugh = furrybot.solo_rpg_command("laughs")
247 furrybot.commands.confused = furrybot.solo_rpg_command("is confused")
248 furrybot.commands.smile = furrybot.solo_rpg_command("smiles")
249 furrybot.commands.hug = furrybot.interactive_rpg_command("hugs")
250 furrybot.commands.cuddle = furrybot.interactive_rpg_command("cuddles")
251 furrybot.commands.kiss = furrybot.interactive_rpg_command("kisses")
252 furrybot.commands.hit = furrybot.interactive_rpg_command("hits")
253 furrybot.commands.slap = furrybot.interactive_rpg_command("slaps")
254 furrybot.commands.beat = furrybot.interactive_rpg_command("beats")
255 furrybot.commands.lick = furrybot.interactive_rpg_command("licks")
256
257 furrybot.commands.sex = furrybot.request_command(function(name, target)
258         furrybot.ping_message(target, name .. " wants to have sex with you. Type !accept to accept or !deny to deny.", furrybot.colors.system)
259 end, function(name, target)
260         furrybot.send(name .. " and " .. target .. " are having sex! OwO", furrybot.colors.rpg)
261 end)
262 furrybot.commands.bang = furrybot.commands.sex
263 furrybot.commands.fuck = furrybot.commands.sex
264
265 furrybot.commands.marry = furrybot.request_command(function(name, target)
266         if storage:contains(name .. ".partner", target) then
267                 furrybot.error_message(name, "You are already married to", storage:get_string(name .. ".partner"))
268                 return false
269         elseif storage:contains(target .. ".partner", name) then
270                 furrybot.error_message(name, target .. " is already married to", storage:get_string(name .. ".partner"))
271                 return false
272         else
273                 furrybot.ping_message(target, name .. " proposes to you. Type !accept to accept or !deny to deny.", furrybot.colors.system)
274         end
275 end, function(name, target)
276         storage:set_string(name .. ".partner", target)
277         storage:set_string(target .. ".partner", name)
278         furrybot.send("Congratulations, " .. furrybot.ping(name, furrybot.colors.rpg) .. "&" .. furrybot.ping(target, furrybot.colors.rpg) .. ", you are married. You may now kiss :).", furrybot.colors.rpg)
279 end)
280 furrybot.commands.propose = furrybot.commands.marry
281 furrybot.unsafe_commands.marry = true
282 furrybot.unsafe_commands.propose = true
283
284 function furrybot.commands.divorce(name)
285         if storage:contains(name .. ".partner") then
286                 local partner = storage:get_string(name .. ".partner")
287                 storage:set_string(name .. ".partner", "")
288                 storage:set_string(partner .. ".partner", "")
289                 furrybot.ping_message(name, "divorces from " .. partner .. " :(", furrybot.colors.rpg)
290         else
291                 furrybot.error_message(name, "You are not married")
292         end
293 end
294 furrybot.unsafe_commands.divorce = true
295
296 function furrybot.commands.partner(name, target)
297         target = target or name
298         if storage:contains(target .. ".partner") then
299                 furrybot.ping_message(name, (target == name and "You are" or target .. " is") .. " married to " .. storage:get_string(target .. ".partner"), furrybot.colors.system)
300         else
301                 furrybot.error_message(name, (target == name and "You are" or target .. " is") .. " not married")
302         end
303 end
304 furrybot.commands.married = furrybot.commands.partner
305
306 -- misc
307 function furrybot.commands.rolldice(name)
308         furrybot.ping_message(name, "rolled a dice and got a " .. furrybot.random(1, 6, furrybot.colors.system) .. ".", furrybot.colors.system)
309 end
310
311 function furrybot.commands.coinflip(name)
312         furrybot.ping_message(name, "flipped a coin and got " .. furrybot.choose({"Heads", "Tails"}, furrybot.colors.system) .. ".", furrybot.colors.system)
313 end
314
315 function furrybot.commands.choose(name, ...)
316         local options = {...}
317         if #options > 1 then
318                 furrybot.ping_message(name, "I choose " .. furrybot.choose(options, "", furrybot.colors.system) .. ".", furrybot.colors.system)
319         else
320                 furrybot.error_message(name, "Not enough options")
321         end
322 end
323
324 function furrybot.commands.dicksize(name, target)
325         target = target or name
326         local size = furrybot.strrandom(target, 31242, 2, 10)
327         local dick = furrybot.repeat_string("=", size) .. "D"
328         furrybot.send(dick .. furrybot.colors.system .. "   <= " .. furrybot.ping(target, furrybot.colors.system) .. "'s Dick", C("#FF4DE1"))
329 end
330 furrybot.commands.cocksize = furrybot.commands.dicksize
331
332 -- fun
333 function furrybot.commands.verse(name)
334         furrybot.json_http_request("https://labs.bible.org/api/?type=json&passage=random", name, function(data)
335                 furrybot.send(data.text .. furrybot.colors.info .. "[" .. data.bookname .. " " .. data.chapter .. "," .. data.verse .. "]", furrybot.colors.fun)
336         end)
337 end
338
339 function furrybot.commands.define(name, word)
340         if word then
341                 furrybot.json_http_request("https://api.dictionaryapi.dev/api/v1/entries/en_US/" .. word, name, function(data)
342                         local meaning = data.meaning
343                         local selected = meaning.exclamation or meaning.noun or meaning.verb or meaning.adjective or meaning["transitive verb"] or meaning.adverb or meaning["relative adverb"]
344                         if not selected then
345                                 print(dump(meaning))
346                                 furrybot.error_message(name, "Error in parsing response")
347                         else
348                                 furrybot.send(word:sub(1, 1):upper() .. word:sub(2, #word):lower() .. ": " .. furrybot.colors.fun .. selected[1].definition, furrybot.colors.info)
349                         end
350                 end)
351         else
352                 furrybot.error_message(name, "You need to specify a word")
353         end
354 end
355
356 function furrybot.commands.insult(name, target)
357         if furrybot.online_or_error(name, target, true) then
358                 furrybot.http_request("https://insult.mattbas.org/api/insult", name, function(data)
359                         furrybot.ping_message(target, data, furrybot.colors.fun)
360                 end)
361         end
362 end
363
364 function furrybot.commands.joke(name, first, last)
365         if not first then
366                 first = "Chuck"
367                 last = "Norris"
368         elseif not last then
369                 last = ""
370         end
371         furrybot.json_http_request("http://api.icndb.com/jokes/random?firstName=" .. first .. "&lastName=" .. last, name, function(data)
372                 local joke = data.value.joke:gsub("&quot;", "\""):gsub("  ", " ")
373                 furrybot.send(joke, furrybot.colors.fun)
374         end)
375 end
376
377 function furrybot.commands.question(name)
378         furrybot.json_http_request("https://8ball.delegator.com/magic/JSON/anything", name, function(data)
379                 furrybot.ping_message(name, data.magic.answer, furrybot.colors.fun)
380         end)
381 end
382 furrybot.commands["8ball"] = furrybot.commands.question
383
384 -- economy
385 function furrybot.commands.money(name, target)
386         target = target or name
387         furrybot.ping_message(name, (target == name and "You have " or target .. " has ") .. furrybot.money(furrybot.get_money(target), furrybot.colors.system) .. ".", furrybot.colors.system)
388 end
389 furrybot.commands.balance = furrybot.commands.money
390
391 function furrybot.commands.pay(name, target, number)
392         if furrybot.online_or_error(name, target) then
393                 local money = tonumber(number or "")
394                 if not money or money <= 0 or math.floor(money) ~= money then
395                         furrybot.error_message(name, "Invalid amount of money")
396                 else
397                         if furrybot.take_money(name, money) then
398                                 furrybot.add_money(target, money)
399                                 furrybot.ping_message(target, name .. " has payed you " .. furrybot.money(money, furrybot.colors.system) .. ".", furrybot.colors.system)
400                         else
401                                 furrybot.error_message(name, "You don't have enough money")
402                         end
403                 end
404         end
405 end
406 furrybot.unsafe_commands.pay = true
407
408 -- send load message
409 furrybot.send("FurryBot - " .. C("#170089") .. "https://github.com/EliasFleckenstein03/furrybot", furrybot.colors.system)
410
411 if furrybot.loaded then
412         furrybot.send("Reloaded", furrybot.colors.system)
413 else
414         furrybot.loaded = true
415 end
416
417 return function(_http, _env, _storage)
418         http, env, storage = _http, _env, _storage
419 end