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