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