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