script_loader.lua

main 60 lines · 1.9 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1local context = G.botContext
2
3context.loadScript = function(path, onLoadCallback)
4 if type(path) ~= 'string' then
5 return context.error("Invalid path for loadScript: " .. tostring(path))
6 end
7 if path:lower():find("http") == 1 then
8 return context.loadRemoteScript(path)
9 end
10 if not g_resources.fileExists(path) then
11 return context.error("File " .. path .. " doesn't exist")
12 end
13
14 local status, result = pcall(function()
15 assert(load(g_resources.readFileContents(path), path, nil, context))()
16 end)
17 if not status then
18 return context.error("Error while loading script from: " .. path .. ":\n" .. result)
19 end
20 if onLoadCallback then
21 onLoadCallback()
22 end
23end
24
25context.loadRemoteScript = function(url, onLoadCallback)
26 if type(url) ~= 'string' or url:lower():find("http") ~= 1 then
27 return context.error("Invalid url for loadRemoteScript: " .. tostring(url))
28 end
29
30 HTTP.get(url, function(data, err)
31 if err or data:len() == 0 then
32 -- try to load from cache
33 if type(context.storage.scriptsCache) ~= 'table' then
34 context.storage.scriptsCache = {}
35 end
36 local cache = context.storage.scriptsCache[url]
37 if cache and type(cache) == 'string' and cache:len() > 0 then
38 data = cache
39 else
40 return context.error("Can't load script from: " .. url .. ", error: " .. err)
41 end
42 end
43
44 local status, result = pcall(function()
45 assert(load(data, url, nil, context))()
46 end)
47 if not status then
48 return context.error("Error while loading script from: " .. url .. ":\n" .. result)
49 end
50 -- cache script
51 if type(context.storage.scriptsCache) ~= 'table' then
52 context.storage.scriptsCache = {}
53 end
54 context.storage.scriptsCache[url] = data
55 if onLoadCallback then
56 onLoadCallback()
57 end
58 end)
59end
60
Top