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