]> git.lizzy.rs Git - furrybot.git/blob - bot.lua
Add insult command
[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 not target then
31                 furrybot.ping_player_error(name, "You need to specify a player")
32         elseif name == target then
33                 furrybot.ping_player_error(name, "You need to specify a different player than yourself")
34         elseif furrybot.player_online(target) then
35                 return true
36         else
37                 furrybot.ping_player_error(name, "Player not online", target)
38         end
39 end
40
41 function furrybot.choose(list)
42         return list[math.random(#list)]
43 end
44
45 function furrybot.http_request(url, name, callback)
46         furrybot.http.fetch({url = url}, function(res)
47                 if res.succeeded then
48                         callback(res.data)
49                 else
50                         furrybot.ping_player_error(name, "Request failed with code", res.code)
51                 end
52         end)
53 end
54
55 function furrybot.json_http_request(url, name, callback)
56         furrybot.http_request(url, name, function(data)
57                 callback(minetest.parse_json(data)[1])
58         end)
59 end
60
61 function furrybot.recieve(msg)
62         msg = minetest.strip_colors(msg)
63         if msg:find("<") == 1 then
64                 local idx = msg:find(">")
65                 local player = msg:sub(2, idx - 1)
66                 local message = msg:sub(idx + 3, #msg)
67                 if message:find("!") == 1 then
68                         local args = message:sub(2, #message):split(" ")
69                         local cmd = table.remove(args, 1)
70                         local func = furrybot.commands[cmd]
71                         if func then
72                                 func(player, unpack(args))
73                         else
74                                 furrybot.ping_player_error(player, "Invalid command", cmd)
75                         end
76                 end
77         end
78 end
79
80 function furrybot.commands.hug(name, target)
81         if furrybot.check_online(name, target) then
82                 furrybot.send(name .. " hugs " .. target .. ".")
83         end
84 end
85
86 furrybot.commands.cuddle = furrybot.commands.hug
87
88 function furrybot.commands.kiss(name, target)
89         if furrybot.check_online(name, target) then
90                 furrybot.send(name .. " kisses " .. target .. ".")
91         end
92 end
93
94 furrybot.target_list = {}
95
96 function furrybot.commands.bang(name, target)
97         if furrybot.check_online(name, target) then
98                 furrybot.target_list[target] = function()
99                         furrybot.send(ping(name) .. " and " .. ping(target) .. " are having sex! OwO")
100                 end,
101                 furrybot.ping_player(target, name .. " wants to have sex with you. Type !accept to accept or !deny to deny.")
102         end
103 end
104
105 furrybot.commands.sex = furrybot.commands.bang
106 furrybot.commands.fuck = furrybot.commands.bang
107
108 function furrybot.commands.accept(name)
109         local func = furrybot.target_list[name]
110         if func then
111                 func()
112         else
113                 furrybot.ping_player_error(name, "Nothing to accept")
114         end
115 end
116
117 function furrybot.commands.deny(name)
118         if furrybot.target_list[name] then
119                 furrybot.target_list[name] = nil
120                 furrybot.ping_player(name, "Denied request")
121         else
122                 furrybot.ping_player_error(name, "Nothing to deny")
123         end
124 end
125
126 function furrybot.commands.hit(name, target)
127         if furrybot.check_online(name, target) then
128                 furrybot.send(name .. " hits " .. target)
129         end
130 end
131
132 furrybot.commands.slap = furrybot.commands.hit
133 furrybot.commands.beat = furrybot.commands.hit
134
135 function furrybot.commands.help()
136         local keys = {}
137         for k in pairs(furrybot.commands) do
138                 table.insert(keys, k)
139         end
140         furrybot.send("Available commands: " .. table.concat(keys, ", "))
141 end
142
143 function furrybot.commands.verse(name)
144         furrybot.json_http_request("https://labs.bible.org/api/?type=json&passage=random", name, function(data)
145                 furrybot.send(data.text .. C("#00FFC3") .. "[" .. data.bookname .. " " .. data.chapter .. "," .. data.verse .. "]")
146         end)
147 end
148
149 function furrybot.commands.define(name, word)
150         if word then
151                 furrybot.json_http_request("https://api.dictionaryapi.dev/api/v1/entries/en_US/" .. word, name, function(data)
152                         local meaning = data.meaning
153                         local selected = meaning.exclamation or meaning.noun or meaning.verb or meaning.adjective or meaning["transitive verb"] or meaning.adverb or meaning["relative adverb"]
154                         if not selected then
155                                 print(dump(meaning))
156                                 furrybot.ping_player_error(name, "Error in parsing response")
157                         else
158                                 furrybot.send(C("#00FFC3") .. word:sub(1, 1):upper() .. word:sub(2, #word):lower() .. ": " .. C("#FFFA00") .. selected[1].definition)
159                         end
160                 end)
161         else
162                 furrybot.ping_player_error(name, "You need to specify a word")
163         end
164 end
165
166 function furrybot.commands.insult(name, target)
167         if furrybot.check_online(name, target) then
168                 furrybot.http_request("https://insult.mattbas.org/api/insult", name, function(data)
169                         furrybot.ping_player(target, data)
170                 end)
171         end
172 end
173
174 function furrybot.commands.rolldice(name)
175         furrybot.ping_player(name, "rolled a dice and got a " .. C("#AAFF43") .. math.random(6))
176 end
177
178 function furrybot.commands.coinflip(name)
179         furrybot.ping_player(name, "flipped a coin and got " .. C("#AAFF43") .. furrybot.choose({"Heads", "Tails"}))
180 end
181
182 function furrybot.commands.status()
183 end
184
185 function furrybot.commands.cmd()
186 end
187
188 if furrybot.loaded then
189         furrybot.send("Reloaded")
190 else
191         furrybot.loaded = true
192 end