]> git.lizzy.rs Git - furrybot.git/blob - bot.lua
2a1943f7034ebddda597f85be4f83d94a62c9cb1
[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.parse_message(player, message, discord)
40         if message:find("!") == 1 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                 if status then
62                         init(http, env, storage)
63                 else
64                         furrybot = old_fb
65                         return false, furrybot.colors.error .. "Error: " .. furrybot.colors.detail .. init
66                 end
67         else
68                 return false, furrybot.colors.error .. "Syntax error: " .. furrybot.colors.detail .. err
69         end
70 end
71
72 function furrybot.player_online(name)
73         for _, n in ipairs(minetest.get_player_names()) do
74                 if name == n then
75                         return true
76                 end
77         end
78 end
79
80 function furrybot.online_or_error(name, other, allow_self)
81         if not other then
82                 furrybot.error_message(name, "You need to specify a player")
83         elseif name == other and not allow_self then
84                 furrybot.error_message(name, "You need to specify a different player than yourself")
85         elseif furrybot.player_online(other) then
86                 return true
87         else
88                 furrybot.error_message(name, "Player not online", other)
89         end
90 end
91
92 function furrybot.choose(list, color)
93         return furrybot.colors.random .. list[math.random(#list)] .. color
94 end
95
96 function furrybot.random(min, max, color)
97         return furrybot.colors.random .. math.random(min, max) .. color
98 end
99
100 function furrybot.http_request(url, name, callback)
101         http.fetch({url = url}, function(res)
102                 if res.succeeded then
103                         callback(res.data)
104                 else
105                         furrybot.error_message(name, "Request failed with code", res.code)
106                 end
107         end)
108 end
109
110 function furrybot.json_http_request(url, name, callback)
111         furrybot.http_request(url, name, function(raw)
112                 local data = minetest.parse_json(raw)
113                 callback(data[1] or data)
114         end)
115 end
116
117 function furrybot.strrandom(str, seed, ...)
118         local v = 0
119         local pr = PseudoRandom(seed)
120         for i = 1, #str do
121                 v = v + str:byte(i) * pr:next()
122         end
123         return PseudoRandom(v):next(...)
124 end
125
126 function furrybot.repeat_string(str, times)
127         local msg = ""
128         for i = 1, times do
129                 msg = msg .. str
130         end
131         return msg
132 end
133
134 function furrybot.interactive_rpg_command(action)
135         return function(name, target)
136                 if furrybot.online_or_error(name, target) then
137                         furrybot.send(name .. " " .. action .. " " .. target .. ".", furrybot.colors.rpg)
138                 end
139         end
140 end
141
142 function furrybot.solo_rpg_command(action)
143         return function(name)
144                 furrybot.send(name .. " " .. action .. ".", furrybot.colors.rpg)
145         end
146 end
147
148 function furrybot.request_command(on_request, on_accept)
149         return function(name, target)
150                 if furrybot.online_or_error(name, target) and on_request(name, target) ~= false then
151                         furrybot.requests[target] = {
152                                 origin = name,
153                                 func = on_accept,
154                         }
155                 end
156         end
157 end
158
159 function furrybot.get_money(name)
160         local key = name .. ".money"
161         if storage:contains(key) then
162                 return storage:get_int(key)
163         else
164                 return 100
165         end
166 end
167
168 function furrybot.set_money(name, money)
169         storage:set_int(name .. ".money", money)
170 end
171
172 function furrybot.add_money(name, add)
173         local money = furrybot.get_money(name)
174         furrybot.set_money(name, money + add)
175 end
176
177 function furrybot.take_money(name, remove)
178         local money = furrybot.get_money(name)
179         local new = money - remove
180         if new < 0 then
181                 return false
182         else
183                 furrybot.set_money(name, new)
184                 return true
185         end
186 end
187
188 function furrybot.money(money, color)
189         return furrybot.colors.money .. "$" .. money .. color
190 end
191
192 function furrybot.get_ascii_dick(name)
193         return minetest.rainbow(furrybot.repeat_string("=", furrybot.strrandom(name, 31242, 2, 10)) .. "D")
194 end
195
196 -- Commands
197
198 -- system
199 function furrybot.commands.help()
200         local keys = {}
201         for k in pairs(furrybot.commands) do
202                 table.insert(keys, k)
203         end
204         furrybot.send("Available commands: " .. table.concat(keys, ", "), furrybot.colors.system)
205 end
206
207 function furrybot.commands.accept(name)
208         local tbl = furrybot.requests[name]
209         if tbl then
210                 furrybot.requests[name] = nil
211                 tbl.func(tbl.origin, name)
212         else
213                 furrybot.error_message(name, "Nothing to accept")
214         end
215 end
216 furrybot.unsafe_commands.accept = true
217
218 function furrybot.commands.deny(name)
219         local tbl = furrybot.requests[name]
220         if tbl then
221                 furrybot.requests[name] = nil
222                 furrybot.ping_message(name, "Denied request", furrybot.colors.system)
223         else
224                 furrybot.error_message(name, "Nothing to deny")
225         end
226 end
227 furrybot.unsafe_commands.deny = true
228
229 -- don't bug players that are running ClamityBot commands from discord
230 function furrybot.commands.status()
231 end
232
233 function furrybot.commands.cmd()
234 end
235
236 -- rpg
237 furrybot.commands.cry = furrybot.solo_rpg_command("cries")
238 furrybot.commands.laugh = furrybot.solo_rpg_command("laughs")
239 furrybot.commands.confused = furrybot.solo_rpg_command("is confused")
240 furrybot.commands.smile = furrybot.solo_rpg_command("smiles")
241 furrybot.commands.hug = furrybot.interactive_rpg_command("hugs")
242 furrybot.commands.cuddle = furrybot.interactive_rpg_command("cuddles")
243 furrybot.commands.kiss = furrybot.interactive_rpg_command("kisses")
244 furrybot.commands.hit = furrybot.interactive_rpg_command("hits")
245 furrybot.commands.slap = furrybot.interactive_rpg_command("slaps")
246 furrybot.commands.beat = furrybot.interactive_rpg_command("beats")
247 furrybot.commands.lick = furrybot.interactive_rpg_command("licks")
248
249 furrybot.commands.smellfeet = furrybot.request_command(function(name, target)
250         furrybot.ping_message(target, name .. " wants to smell your feet. Type !accept to accept or !deny to deny.", furrybot.colors.system)
251 end, function(name, target)
252         furrybot.ping_message(name, " you are smelling " .. target .. "'s feet. They are kinda stinky!", furrybot.colors.rpg)
253 end)
254
255 furrybot.commands.blowjob = furrybot.request_command(function(name, target)
256         furrybot.ping_message(target, name .. " wants to suck your dick. Type !accept to accept or !deny to deny.", furrybot.colors.system)
257 end, function(name, target)
258         furrybot.send(name .. " is sucking " .. target .. "'s cock. ˣoˣ IT'S SO HUGE", furrybot.colors.rpg)
259 end)
260
261 furrybot.commands.sex = furrybot.request_command(function(name, target)
262         furrybot.ping_message(target, name .. " wants to have sex with you. Type !accept to accept or !deny to deny.", furrybot.colors.system)
263 end, function(name, target)
264         furrybot.send(name .. " and " .. target .. " are having sex! OwO", furrybot.colors.rpg)
265 end)
266 furrybot.commands.bang = furrybot.commands.sex
267 furrybot.commands.fuck = furrybot.commands.sex
268
269 furrybot.commands.cum = function(name)
270         furrybot.send(name .. " is cumming: " .. furrybot.get_ascii_dick(name) .. C("#FFFFFF") .. furrybot.repeat_string("~", math.random(1, 10)), furrybot.colors.rpg)
271 end
272
273 furrybot.commands.marry = furrybot.request_command(function(name, target)
274         if storage:contains(name .. ".partner", target) then
275                 furrybot.error_message(name, "You are already married to", storage:get_string(name .. ".partner"))
276                 return false
277         elseif storage:contains(target .. ".partner", name) then
278                 furrybot.error_message(name, target .. " is already married to", storage:get_string(target .. ".partner"))
279                 return false
280         else
281                 furrybot.ping_message(target, name .. " proposes to you. Type !accept to accept or !deny to deny.", furrybot.colors.system)
282         end
283 end, function(name, target)
284         storage:set_string(name .. ".partner", target)
285         storage:set_string(target .. ".partner", name)
286         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)
287 end)
288 furrybot.commands.propose = furrybot.commands.marry
289 furrybot.unsafe_commands.marry = true
290 furrybot.unsafe_commands.propose = true
291
292 function furrybot.commands.divorce(name)
293         if storage:contains(name .. ".partner") then
294                 local partner = storage:get_string(name .. ".partner")
295                 storage:set_string(name .. ".partner", "")
296                 storage:set_string(partner .. ".partner", "")
297                 furrybot.ping_message(name, "divorces from " .. partner .. " :(", furrybot.colors.rpg)
298         else
299                 furrybot.error_message(name, "You are not married")
300         end
301 end
302 furrybot.unsafe_commands.divorce = true
303
304 function furrybot.commands.partner(name, target)
305         target = target or name
306         if storage:contains(target .. ".partner") then
307                 furrybot.ping_message(name, (target == name and "You are" or target .. " is") .. " married to " .. storage:get_string(target .. ".partner"), furrybot.colors.system)
308         else
309                 furrybot.error_message(name, (target == name and "You are" or target .. " is") .. " not married")
310         end
311 end
312 furrybot.commands.married = furrybot.commands.partner
313
314 furrybot.kill_deathmessages = {
315         "%s walked into fire whilst fighting %s",
316         "%s was struck by lightning whilst fighting %s",
317         "%s was burnt to a crisp whilst fighting %s",
318         "%s tried to swim in lava to escape %s",
319         "%s walked into danger zone due to %s",
320         "%s suffocated in a wall whilst fighting %s",
321         "%s drowned whilst trying to escape %s",
322         "%s starved to death whilst fighting %s",
323         "%s walked into a cactus whilst trying to escape %s",
324         "%s hit the ground too hard whilst trying to escape %s",
325         "%s experienced kinetic energy whilst trying to escape %s",
326         "%s didn't want to live in the same world as %s",
327         "%s died because of %s",
328         "%s was killed by magic whilst trying to escape %s",
329         "%s was killed by %s using magic",
330         "%s was roasted in dragon breath by %s",
331         "%s withered away whilst fighting %s",
332         "%s was shot by a skull from %s",
333         "%s was squashed by a falling anvil whilst fighting %s",
334         "%s was slain by %s",
335         "%s was shot by %s",
336         "%s was fireballed by %s",
337         "%s was killed trying to hurt %s",
338         "%s was blown up by %s",
339         "%s was squashed by %s",
340 }
341
342 furrybot.deathmessages = {
343         "%s went up in flames",
344         "%s was struck by lightning",
345         "%s burned to death",
346         "%s tried to swim in lava",
347         "%s discovered the floor was lava",
348         "%s suffocated in a wall",
349         "%s drowned",
350         "%s starved to death",
351         "%s was pricked to death",
352         "%s hit the ground too hard",
353         "%s experienced kinetic energy",
354         "%s fell out of the world",
355         "%s died",
356         "%s was killed by magic",
357         "%s was roasted in dragon breath",
358         "%s withered away",
359         "%s was squashed by a falling anvil",
360         "%s blew up",
361         "%s was squished too much",
362         "%s went off with a bang",
363 }
364
365 function furrybot.commands.kill(name, target)
366         if furrybot.online_or_error(name, target, true) then
367                 if name == target then
368                         furrybot.send(string.format("%s died due to lack of friends", target), furrybot.colors.rpg)
369                 else
370                         furrybot.send(string.format(furrybot.kill_deathmessages[math.random(#furrybot.kill_deathmessages)], target, name), furrybot.colors.rpg)
371                 end
372         end
373 end
374
375 function furrybot.commands.die(name)
376         furrybot.send(string.format(furrybot.deathmessages[math.random(#furrybot.deathmessages)], name), furrybot.colors.rpg)
377 end
378
379 -- misc
380 function furrybot.commands.rolldice(name)
381         furrybot.ping_message(name, "rolled a dice and got a " .. furrybot.random(1, 6, furrybot.colors.system) .. ".", furrybot.colors.system)
382 end
383
384 function furrybot.commands.coinflip(name)
385         furrybot.ping_message(name, "flipped a coin and got " .. furrybot.choose({"Heads", "Tails"}, furrybot.colors.system) .. ".", furrybot.colors.system)
386 end
387
388 function furrybot.commands.choose(name, ...)
389         local options = {...}
390         if #options > 1 then
391                 furrybot.ping_message(name, "I choose " .. furrybot.choose(options, "", furrybot.colors.system) .. ".", furrybot.colors.system)
392         else
393                 furrybot.error_message(name, "Not enough options")
394         end
395 end
396
397 function furrybot.commands.dicksize(name, target)
398         target = target or name
399         furrybot.send(furrybot.get_ascii_dick(target) .. furrybot.colors.system .. "   ← " .. furrybot.ping(target, furrybot.colors.system) .. "'s Dick", C("#FF4DE1"))
400 end
401 furrybot.commands.cocksize = furrybot.commands.dicksize
402
403 -- fun
404 function furrybot.commands.amogus(name)
405         furrybot.ping_message(name, "YOU KINDA SUS MAN", furrybot.colors.fun)
406 end
407
408 function furrybot.commands.verse(name)
409         furrybot.json_http_request("https://labs.bible.org/api/?type=json&passage=random", name, function(data)
410                 furrybot.send(data.text .. furrybot.colors.info .. "[" .. data.bookname .. " " .. data.chapter .. "," .. data.verse .. "]", furrybot.colors.fun)
411         end)
412 end
413
414 function furrybot.commands.define(name, word)
415         if word then
416                 furrybot.json_http_request("https://api.dictionaryapi.dev/api/v1/entries/en_US/" .. word:gsub("computer", "person"), name, function(data)
417                         local meaning = data.meaning
418                         local selected = meaning.abbreviation or meaning["cardinal number"] or meaning.exclamation or meaning.noun or meaning.verb or meaning.adjective or meaning["transitive verb"] or meaning.adverb or meaning["relative adverb"] or meaning.preposition
419                         if not selected then
420                                 print(dump(meaning))
421                                 furrybot.error_message(name, "Error in parsing response")
422                         else
423                                 furrybot.send(word:sub(1, 1):upper() .. word:sub(2, #word):lower() .. ": " .. furrybot.colors.fun .. selected[1].definition, furrybot.colors.info)
424                         end
425                 end)
426         else
427                 furrybot.error_message(name, "You need to specify a word")
428         end
429 end
430
431 function furrybot.commands.insult(name, target)
432         if furrybot.online_or_error(name, target, true) then
433                 furrybot.http_request("https://insult.mattbas.org/api/insult", name, function(data)
434                         furrybot.ping_message(target, data, furrybot.colors.fun)
435                 end)
436         end
437 end
438
439 function furrybot.commands.joke(name, first, last)
440         if not first then
441                 first = "Chuck"
442                 last = "Norris"
443         elseif not last then
444                 last = ""
445         end
446         furrybot.json_http_request("http://api.icndb.com/jokes/random?firstName=" .. first .. "&lastName=" .. last, name, function(data)
447                 local joke = data.value.joke:gsub("&quot;", "\""):gsub("  ", " ")
448                 furrybot.send(joke, furrybot.colors.fun)
449         end)
450 end
451
452 function furrybot.commands.question(name)
453         furrybot.json_http_request("https://8ball.delegator.com/magic/JSON/anything", name, function(data)
454                 furrybot.ping_message(name, data.magic.answer, furrybot.colors.fun)
455         end)
456 end
457 furrybot.commands["8ball"] = furrybot.commands.question
458
459 -- economy
460 function furrybot.commands.money(name, target)
461         target = target or name
462         furrybot.ping_message(name, (target == name and "You have " or target .. " has ") .. furrybot.money(furrybot.get_money(target), furrybot.colors.system) .. ".", furrybot.colors.system)
463 end
464 furrybot.commands.balance = furrybot.commands.money
465
466 function furrybot.commands.pay(name, target, number)
467         if furrybot.online_or_error(name, target) then
468                 local money = tonumber(number or "")
469                 if not money or money <= 0 or math.floor(money) ~= money then
470                         furrybot.error_message(name, "Invalid amount of money")
471                 else
472                         if furrybot.take_money(name, money) then
473                                 furrybot.add_money(target, money)
474                                 furrybot.ping_message(target, name .. " has payed you " .. furrybot.money(money, furrybot.colors.system) .. ".", furrybot.colors.system)
475                         else
476                                 furrybot.error_message(name, "You don't have enough money")
477                         end
478                 end
479         end
480 end
481 furrybot.unsafe_commands.pay = true
482
483 -- send load message
484 furrybot.send("FurryBot - " .. C("#170089") .. "https://github.com/EliasFleckenstein03/furrybot", furrybot.colors.system)
485
486 if furrybot.loaded then
487         furrybot.send("Reloaded", furrybot.colors.system)
488 else
489         furrybot.loaded = true
490 end
491
492 return function(_http, _env, _storage)
493         http, env, storage = _http, _env, _storage
494 end