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