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