]> git.lizzy.rs Git - furrybot.git/blob - bot.lua
Add uwu command
[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         roleplay = 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.parse_message(player, message, discord)
40         if message:find("!") == 1 then
41                 local args = message:sub(2, #message):split(" ")
42                 local cmd = table.remove(args, 1)
43                 local func = furrybot.commands[cmd]
44                 if func then
45                         if furrybot.unsafe_commands[cmd] and discord then
46                                 furrybot.error_message(player, "Sorry, you cannot run this command from discord: ", cmd)
47                         else
48                                 func(player, unpack(args))
49                         end
50                 else
51                         furrybot.error_message(player, "Invalid command", cmd)
52                 end
53         end
54 end
55
56 function furrybot.reload()
57         local func, err = env.loadfile("clientmods/furrybot/bot.lua")
58         if func then
59                 local old_fb = table.copy(furrybot)
60                 local status, init = pcall(func)
61
62                 if status then
63                         status, init = init(http, env, storage)
64                 end
65
66                 if not status then
67                         furrybot = old_fb
68                         return false, furrybot.colors.error .. "Error: " .. furrybot.colors.detail .. init
69                 end
70         else
71                 return false, furrybot.colors.error .. "Syntax error: " .. furrybot.colors.detail .. err
72         end
73 end
74
75 function furrybot.player_online(name)
76         for _, n in ipairs(minetest.get_player_names()) do
77                 if name == n then
78                         return true
79                 end
80         end
81 end
82
83 function furrybot.online_or_error(name, other, allow_self)
84         if not other then
85                 furrybot.error_message(name, "You need to specify a player")
86         elseif name == other and not allow_self then
87                 furrybot.error_message(name, "You need to specify a different player than yourself")
88         elseif furrybot.player_online(other) then
89                 return true
90         else
91                 furrybot.error_message(name, "Player not online", other)
92         end
93 end
94
95 function furrybot.choose(list, color)
96         return furrybot.colors.random .. list[math.random(#list)] .. color
97 end
98
99 function furrybot.random(min, max, color)
100         return furrybot.colors.random .. math.random(min, max) .. color
101 end
102
103 function furrybot.http_request(url, name, callback)
104         http.fetch({url = url}, function(res)
105                 if res.succeeded then
106                         callback(res.data)
107                 else
108                         furrybot.error_message(name, "Request failed with code", res.code)
109                 end
110         end)
111 end
112
113 function furrybot.json_http_request(url, name, callback)
114         furrybot.http_request(url, name, function(raw)
115                 local data = minetest.parse_json(raw)
116                 callback(data[1] or data)
117         end)
118 end
119
120 function furrybot.strrandom(str, seed, ...)
121         local v = 0
122         local pr = PseudoRandom(seed)
123         for i = 1, #str do
124                 v = v + str:byte(i) * pr:next()
125         end
126         return PseudoRandom(v):next(...)
127 end
128
129 function furrybot.repeat_string(str, times)
130         local msg = ""
131         for i = 1, times do
132                 msg = msg .. str
133         end
134         return msg
135 end
136
137 function furrybot.interactive_roleplay_command(action)
138         return function(name, target)
139                 if furrybot.online_or_error(name, target) then
140                         furrybot.send(name .. " " .. action .. " " .. target .. ".", furrybot.colors.roleplay)
141                 end
142         end
143 end
144
145 function furrybot.solo_roleplay_command(action)
146         return function(name)
147                 furrybot.send(name .. " " .. action .. ".", furrybot.colors.roleplay)
148         end
149 end
150
151 function furrybot.request_command(on_request, on_accept)
152         return function(name, target)
153                 if furrybot.online_or_error(name, target) and on_request(name, target) ~= false then
154                         furrybot.requests[target] = {
155                                 origin = name,
156                                 func = on_accept,
157                         }
158                 end
159         end
160 end
161
162 -- General purpose commands
163
164 function furrybot.commands.help()
165         local keys = {}
166         for k in pairs(furrybot.commands) do
167                 table.insert(keys, k)
168         end
169         furrybot.send("Available commands: " .. table.concat(keys, ", "), furrybot.colors.system)
170 end
171
172 function furrybot.commands.accept(name)
173         local tbl = furrybot.requests[name]
174         if tbl then
175                 furrybot.requests[name] = nil
176                 tbl.func(tbl.origin, name)
177         else
178                 furrybot.error_message(name, "Nothing to accept")
179         end
180 end
181 furrybot.unsafe_commands.accept = true
182
183 function furrybot.commands.deny(name)
184         local tbl = furrybot.requests[name]
185         if tbl then
186                 furrybot.requests[name] = nil
187                 furrybot.ping_message(name, "Denied request", furrybot.colors.system)
188         else
189                 furrybot.error_message(name, "Nothing to deny")
190         end
191 end
192 furrybot.unsafe_commands.deny = true
193
194 -- don't bug players that are running ClamityBot commands from discord
195 function furrybot.commands.status()
196 end
197
198 function furrybot.commands.cmd()
199 end
200
201 return function(_http, _env, _storage)
202         http, env, storage = _http, _env, _storage
203
204         for _, f in ipairs {"nsfw", "roleplay", "death", "economy", "random", "http"} do
205                 local func, err = env.loadfile("clientmods/furrybot/" .. f .. ".lua")
206
207                 if not func then
208                         return false, err
209                 end
210
211                 func()(http, env, storage)
212         end
213
214         furrybot.send("FurryBot - " .. C("#170089") .. "https://github.com/EliasFleckenstein03/furrybot", furrybot.colors.system)
215
216         if furrybot.loaded then
217                 furrybot.send("Reloaded", furrybot.colors.system)
218         else
219                 furrybot.loaded = true
220         end
221
222         return true
223 end