]> git.lizzy.rs Git - furrybot.git/blob - bot.lua
Add solo RPG commands
[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.interactive_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.solo_rpg_command(action)
134         return function(name)
135                 furrybot.send(name .. " " .. action .. ".", furrybot.colors.rpg)
136         end
137 end
138
139 function furrybot.request_command(on_request, on_accept)
140         return function(name, target)
141                 if furrybot.online_or_error(name, target) and on_request(name, target) ~= false then
142                         furrybot.requests[target] = {
143                                 origin = name,
144                                 func = on_accept,
145                         }
146                 end
147         end
148 end
149
150 function furrybot.get_money(name)
151         local key = name .. ".money"
152         if furrybot.storage:contains(key) then
153                 return furrybot.storage:get_int(key)
154         else
155                 return 100
156         end
157 end
158
159 function furrybot.set_money(name, money)
160         furrybot.storage:set_int(name .. ".money", money)
161 end
162
163 function furrybot.add_money(name, add)
164         local money = furrybot.get_money(name)
165         furrybot.set_money(name, money + add)
166 end
167
168 function furrybot.take_money(name, remove)
169         local money = furrybot.get_money(name)
170         local new = money - remove
171         if new < 0 then
172                 return false
173         else
174                 furrybot.set_money(name, new)
175                 return true
176         end
177 end
178
179 function furrybot.money(money, color)
180         return furrybot.colors.money .. "$" .. money .. color
181 end
182
183 -- Commands
184
185 -- system
186 function furrybot.commands.help()
187         local keys = {}
188         for k in pairs(furrybot.commands) do
189                 table.insert(keys, k)
190         end
191         furrybot.send("Available commands: " .. table.concat(keys, ", "), furrybot.colors.system)
192 end
193
194 function furrybot.commands.accept(name)
195         local tbl = furrybot.requests[name]
196         if tbl then
197                 furrybot.requests[name] = nil
198                 tbl.func(tbl.origin, name)
199         else
200                 furrybot.error_message(name, "Nothing to accept")
201         end
202 end
203 furrybot.unsafe_commands.accept = true
204
205 function furrybot.commands.deny(name)
206         local tbl = furrybot.requests[name]
207         if tbl then
208                 furrybot.requests[name] = nil
209                 furrybot.ping_message(name, "Denied request", furrybot.colors.system)
210         else
211                 furrybot.error_message(name, "Nothing to deny")
212         end
213 end
214 furrybot.unsafe_commands.deny = true
215
216 -- don't bug players that are running ClamityBot commands from discord
217 function furrybot.commands.status()
218 end
219
220 function furrybot.commands.cmd()
221 end
222
223 -- rpg
224 furrybot.commands.cry = furrybot.solo_rpg_command("cries")
225 furrybot.commands.laugh = furrybot.solo_rpg_command("laughs")
226 furrybot.commands.confused = furrybot.solo_rpg_command("is confused")
227 furrybot.commands.hug = furrybot.interactive_rpg_command("hugs")
228 furrybot.commands.cuddle = furrybot.interactive_rpg_command("cuddles")
229 furrybot.commands.kiss = furrybot.interactive_rpg_command("kisses")
230 furrybot.commands.hit = furrybot.interactive_rpg_command("hits")
231 furrybot.commands.slap = furrybot.interactive_rpg_command("slaps")
232 furrybot.commands.beat = furrybot.interactive_rpg_command("beats")
233 furrybot.commands.lick = furrybot.interactive_rpg_command("licks")
234
235 furrybot.commands.sex = furrybot.request_command(function(name, target)
236         furrybot.ping_message(target, name .. " wants to have sex with you. Type !accept to accept or !deny to deny.", furrybot.colors.system)
237 end, function(name, target)
238         furrybot.send(name .. " and " .. target .. " are having sex! OwO", furrybot.colors.rpg)
239 end)
240 furrybot.commands.bang = furrybot.commands.sex
241 furrybot.commands.fuck = furrybot.commands.sex
242
243 furrybot.commands.marry = furrybot.request_command(function(name, target)
244         if furrybot.storage:contains(name .. ".partner", target) then
245                 furrybot.error_message(name, "You are already married to", furrybot.storage:get_string(name .. ".partner"))
246                 return false
247         elseif furrybot.storage:contains(target .. ".partner", name) then
248                 furrybot.error_message(name, target .. " is already married to", furrybot.storage:get_string(name .. ".partner"))
249                 return false
250         else
251                 furrybot.ping_message(target, name .. " proposes to you. Type !accept to accept or !deny to deny.", furrybot.colors.system)
252         end
253 end, function(name, target)
254         furrybot.storage:set_string(name .. ".partner", target)
255         furrybot.storage:set_string(target .. ".partner", name)
256         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)
257 end)
258 furrybot.commands.propose = furrybot.commands.marry
259 furrybot.unsafe_commands.marry = true
260 furrybot.unsafe_commands.propose = true
261
262 function furrybot.commands.divorce(name)
263         if furrybot.storage:contains(name .. ".partner") then
264                 local partner = furrybot.storage:get_string(name .. ".partner")
265                 furrybot.storage:set_string(name .. ".partner", "")
266                 furrybot.storage:set_string(partner .. ".partner", "")
267                 furrybot.ping_message(name, "divorces from " .. partner .. " :(", furrybot.colors.rpg)
268         else
269                 furrybot.error_message(name, "You are not married")
270         end
271 end
272 furrybot.unsafe_commands.divorce = true
273
274 function furrybot.commands.partner(name, target)
275         target = target or name
276         if furrybot.storage:contains(target .. ".partner") then
277                 furrybot.ping_message(name, (target == name and "You are" or target .. " is") .. " married to " .. furrybot.storage:get_string(target .. ".partner"), furrybot.colors.system)
278         else
279                 furrybot.error_message(name, (target == name and "You are" or target .. " is") .. " not married")
280         end
281 end
282 furrybot.commands.married = furrybot.commands.partner
283
284 -- misc
285 function furrybot.commands.rolldice(name)
286         furrybot.ping_message(name, "rolled a dice and got a " .. furrybot.random(1, 6, furrybot.colors.system) .. ".", furrybot.colors.system)
287 end
288
289 function furrybot.commands.coinflip(name)
290         furrybot.ping_message(name, "flipped a coin and got " .. furrybot.choose({"Heads", "Tails"}, furrybot.colors.system) .. ".", furrybot.colors.system)
291 end
292
293 function furrybot.commands.choose(name, ...)
294         local options = {...}
295         if #options > 1 then
296                 furrybot.ping_message(name, "I choose " .. furrybot.choose(options, "", furrybot.colors.system) .. ".", furrybot.colors.system)
297         else
298                 furrybot.error_message(name, "Not enough options")
299         end
300 end
301
302 function furrybot.commands.dicksize(name, target)
303         target = target or name
304         local size = furrybot.strrandom(target, 31242, 2, 10)
305         local dick = furrybot.repeat_string("=", size) .. "D"
306         furrybot.send(dick .. furrybot.colors.system .. "   <= " .. furrybot.ping(target, furrybot.colors.system) .. "'s Dick", C("#FF4DE1"))
307 end
308 furrybot.commands.cocksize = furrybot.commands.dicksize
309
310 -- fun
311 function furrybot.commands.verse(name)
312         furrybot.json_http_request("https://labs.bible.org/api/?type=json&passage=random", name, function(data)
313                 furrybot.send(data.text .. furrybot.colors.info .. "[" .. data.bookname .. " " .. data.chapter .. "," .. data.verse .. "]", furrybot.colors.fun)
314         end)
315 end
316
317 function furrybot.commands.define(name, word)
318         if word then
319                 furrybot.json_http_request("https://api.dictionaryapi.dev/api/v1/entries/en_US/" .. word, name, function(data)
320                         local meaning = data.meaning
321                         local selected = meaning.exclamation or meaning.noun or meaning.verb or meaning.adjective or meaning["transitive verb"] or meaning.adverb or meaning["relative adverb"]
322                         if not selected then
323                                 print(dump(meaning))
324                                 furrybot.error_message(name, "Error in parsing response")
325                         else
326                                 furrybot.send(word:sub(1, 1):upper() .. word:sub(2, #word):lower() .. ": " .. furrybot.colors.fun .. selected[1].definition, furrybot.colors.info)
327                         end
328                 end)
329         else
330                 furrybot.error_message(name, "You need to specify a word")
331         end
332 end
333
334 function furrybot.commands.insult(name, target)
335         if furrybot.online_or_error(name, target, true) then
336                 furrybot.http_request("https://insult.mattbas.org/api/insult", name, function(data)
337                         furrybot.ping_message(target, data, furrybot.colors.fun)
338                 end)
339         end
340 end
341
342 function furrybot.commands.joke(name, first, last)
343         if not first then
344                 first = "Chuck"
345                 last = "Norris"
346         elseif not last then
347                 last = ""
348         end
349         furrybot.json_http_request("http://api.icndb.com/jokes/random?firstName=" .. first .. "&lastName=" .. last, name, function(data)
350                 local joke = data.value.joke:gsub("&quot;", "\""):gsub("  ", " ")
351                 furrybot.send(joke, furrybot.colors.fun)
352         end)
353 end
354
355 function furrybot.commands.question(name)
356         furrybot.json_http_request("https://8ball.delegator.com/magic/JSON/anything", name, function(data)
357                 furrybot.ping_message(name, data.magic.answer, furrybot.colors.fun)
358         end)
359 end
360 furrybot.commands["8ball"] = furrybot.commands.question
361
362 -- economy
363 function furrybot.commands.money(name, target)
364         target = target or name
365         furrybot.ping_message(name, (target == name and "You have " or target .. " has ") .. furrybot.money(furrybot.get_money(target), furrybot.colors.system) .. ".", furrybot.colors.system)
366 end
367 furrybot.commands.balance = furrybot.commands.money
368
369 function furrybot.commands.pay(name, target, number)
370         if furrybot.online_or_error(name, target) then
371                 local money = tonumber(number or "")
372                 if not money or money <= 0 or math.floor(money) ~= money then
373                         furrybot.error_message(name, "Invalid amount of money")
374                 else
375                         if furrybot.take_money(name, money) then
376                                 furrybot.add_money(target, money)
377                                 furrybot.ping_message(target, name .. " has payed you " .. furrybot.money(money, furrybot.colors.system) .. ".", furrybot.colors.system)
378                         else
379                                 furrybot.error_message(name, "You don't have enough money")
380                         end
381                 end
382         end
383 end
384 furrybot.unsafe_commands.pay = true
385
386 -- send load message
387 furrybot.send("FurryBot - " .. C("#170089") .. "https://github.com/EliasFleckenstein03/furrybot", furrybot.colors.system)
388
389 if furrybot.loaded then
390         furrybot.send("Reloaded", furrybot.colors.system)
391 else
392         furrybot.loaded = true
393 end