]> git.lizzy.rs Git - furrybot.git/blob - bot.lua
Async HTTP fetching
[furrybot.git] / bot.lua
1 furrybot.commands = {}
2
3 local C = minetest.get_color_escape_sequence
4
5 function furrybot.send(msg, color)
6         minetest.send_chat_message("/me " .. C("#00FF3C") .. "[" .. C(color or "#FFFA00") .. msg .. C("#00FF3C") .. "]")
7 end
8
9 function furrybot.ping(player)
10         return C("#00DCFF") .. "@" .. player .. C("#FFFA00")
11 end
12
13 function furrybot.ping_player(player, message)
14         furrybot.send(furrybot.ping(player) .. ": " .. message)
15 end
16
17 function furrybot.ping_player_error(player, err, detail)
18         furrybot.ping_player(player, C("#D70029") .. " " .. err ..  " " .. (detail and C("#FF6683") .. "'" .. detail .. "'" .. C("#D70029") or "") .. ".")
19 end
20
21 function furrybot.player_online(name)
22         for _, n in ipairs(minetest.get_player_names()) do
23                 if name == n then
24                         return true
25                 end
26         end
27 end
28
29 function furrybot.check_online(name, target)
30         if name == target then
31                 furrybot.ping_player_error(name, "You need to specify another player")
32         elseif furrybot.player_online(target) then
33                 return true
34         else
35                 furrybot.ping_player_error(name, "Player not online", target)
36         end
37 end
38
39 function furrybot.choose(list)
40         return list[math.random(#list)]
41 end
42
43 function furrybot.recieve(msg)
44         msg = minetest.strip_colors(msg)
45         if msg:find("<") == 1 then
46                 local idx = msg:find(">")
47                 local player = msg:sub(2, idx - 1)
48                 local message = msg:sub(idx + 3, #msg)
49                 if message:find("!") == 1 then
50                         local args = message:sub(2, #message):split(" ")
51                         local cmd = table.remove(args, 1)
52                         local func = furrybot.commands[cmd]
53                         if func then
54                                 func(player, unpack(args))
55                         else
56                                 furrybot.ping_player_error(player, "Invalid command", cmd)
57                         end
58                 end
59         end
60 end
61
62 function furrybot.commands.hug(name, target)
63         if furrybot.check_online(name, target) then
64                 furrybot.send(name .. " hugs " .. target .. ".")
65         end
66 end
67
68 furrybot.commands.cuddle = furrybot.commands.hug
69
70 function furrybot.commands.kiss(name, target)
71         if furrybot.check_online(name, target) then
72                 furrybot.send(name .. " kisses " .. target .. ".")
73         end
74 end
75
76 furrybot.target_list = {}
77
78 function furrybot.commands.bang(name, target)
79         if furrybot.check_online(name, target) then
80                 furrybot.target_list[target] = function()
81                         furrybot.send(ping(name) .. " and " .. ping(target) .. " are having sex! OwO")
82                 end,
83                 furrybot.ping_player(target, name .. " wants to have sex with you. Type !accept to accept or !deny to deny.")
84         end
85 end
86
87 furrybot.commands.sex = furrybot.commands.bang
88 furrybot.commands.fuck = furrybot.commands.bang
89
90 function furrybot.commands.accept(name)
91         local func = furrybot.target_list[name]
92         if func then
93                 func()
94         else
95                 furrybot.ping_player_error(name, "Nothing to accept")
96         end
97 end
98
99 function furrybot.commands.deny(name)
100         if furrybot.target_list[name] then
101                 furrybot.target_list[name] = nil
102                 furrybot.ping_player(name, "Denied request")
103         else
104                 furrybot.ping_player_error(name, "Nothing to deny")
105         end
106 end
107
108 function furrybot.commands.hit(name, target)
109         if furrybot.check_online(name, target) then
110                 furrybot.send(name .. " hits " .. target)
111         end
112 end
113
114 furrybot.commands.slap = furrybot.commands.hit
115 furrybot.commands.beat = furrybot.commands.hit
116
117 function furrybot.commands.help()
118         local keys = {}
119         for k in pairs(furrybot.commands) do
120                 table.insert(keys, k)
121         end
122         furrybot.send("Available commands: " .. table.concat(keys, ", "))
123 end
124
125 function furrybot.commands.verse(name)
126         local req = {
127                 url = "https://labs.bible.org/api/?type=json&passage=random",
128         }
129         local res = furrybot.http.fetch(req, function(res)
130                 if res.succeeded then
131                         local data = minetest.parse_json(res.data)[1]
132                         furrybot.send(data.text .. C("#00FFC3") .. "[" .. data.bookname .. " " .. data.chapter .. "," .. data.verse .. "]")
133                 else
134                         furrybot.ping_player_error(name, "Request failed with code", res.code)
135                 end
136         end)
137 end
138
139 function furrybot.commands.rolldice(name)
140         furrybot.ping_player(name, "rolled a dice and got a " .. C("#AAFF43") .. math.random(6))
141 end
142
143 function furrybot.commands.coinflip(name)
144         furrybot.ping_player(name, "flipped a coin and got " .. C("#AAFF43") .. furrybot.choose({"Heads", "Tails"}))
145 end
146
147 if furrybot.loaded then
148         furrybot.send("Reloaded")
149 else
150         furrybot.loaded = true
151 end