From 23383a8a635e6a71fe0c972d942f40ad1af7f776 Mon Sep 17 00:00:00 2001 From: moderndevslulw Date: Tue, 8 Jul 2025 14:05:24 +0500 Subject: fix: build metadata for scripts only when there are changes --- .gitignore | 2 ++ lib/utils.php | 69 ++++++++++++++++++++++++++++++++++++++++++++ public/scripts.php | 84 ++++++++++++++---------------------------------------- 3 files changed, 93 insertions(+), 62 deletions(-) diff --git a/.gitignore b/.gitignore index 61cc25c..2476444 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ config.php /vendor/ +scriptmetadata.json +*.ini \ No newline at end of file diff --git a/lib/utils.php b/lib/utils.php index 6de3dfb..639eed4 100644 --- a/lib/utils.php +++ b/lib/utils.php @@ -1,4 +1,7 @@ 1 ? "s" : ""); } +} + +function parse_lua_script($value) +{ + if ($value instanceof \Raudius\Luar\Interpreter\LuarObject\Literal) { + return $value->getValue(); + } else if ($value instanceof \Raudius\Luar\Interpreter\LuarObject\Table) { + return parse_lua_script($value->getValue()); + } + + if (is_array($value)) { + $out = []; + foreach ($value as $k => $v) { + if ($k == 'handle') { + continue; + } + $out[$k] = parse_lua_script($v); + } + return $out; + } + + return $value; +} + +function build_script_metadata(string $folder_path) +{ + $file_path = $_SERVER["DOCUMENT_ROOT"] . "/../scriptmetadata.json"; + $should_create = is_dir($folder_path); + $files = glob("$folder_path/*.lua"); + + if (file_exists($file_path)) { + $file_last_change = filemtime($file_path); + $script_last_change = 0; + foreach ($files as $file) { + $l = filemtime($file); + if ($l > $script_last_change) { + $l = $script_last_change; + } + } + $should_create = $script_last_change > $file_last_change; + } + + if ($should_create) { + $scripts = []; + + $luar = new Luar(); + $parsedown = new Parsedown(); + + foreach ($files as $file) { + $contents = file_get_contents($file); + + $result = $luar->eval($contents); + $script = parse_lua_script($result); + if (!isset($script["description"])) { + $script["description"] = "_No description provided._"; + } + $script['description_text'] = str_replace(PHP_EOL, ' ', $script['description']); + $script['description_formatted'] = $parsedown->text($script['description']); + + array_push($scripts, $script); + } + + file_put_contents($file_path, json_encode($scripts, JSON_UNESCAPED_SLASHES)); + } + + return true; } \ No newline at end of file diff --git a/public/scripts.php b/public/scripts.php index 0f80be4..988b4bc 100644 --- a/public/scripts.php +++ b/public/scripts.php @@ -1,79 +1,39 @@ getValue(); - } else if ($value instanceof \Raudius\Luar\Interpreter\LuarObject\Table) { - return parse_lua_script($value->getValue()); - } - - if (is_array($value)) { - $out = []; - foreach ($value as $k => $v) { - if ($k == 'handle') { - continue; - } - $out[$k] = parse_lua_script($v); - } - return $out; - } - - return $value; -} - if (empty(LUA_SCRIPT_DIRECTORY)) { http_response_code(403); exit; } -$command = null; -$scripts = []; - -// loading lua scripts -$luar = new Luar(); -$parsedown = new Parsedown(); -$file_paths = glob(LUA_SCRIPT_DIRECTORY . '/*.lua'); -foreach ($file_paths as $file_path) { - $script = file_get_contents($file_path); - - $result = $luar->eval($script); - - $script = parse_lua_script($result); - $script['description_text'] = str_replace(PHP_EOL, ' ', $script['description']); - - array_push($scripts, $script); +if (!build_script_metadata(LUA_SCRIPT_DIRECTORY)) { + http_response_code(500); + exit("Failed to build a metadata for scripts."); } +$script = null; +$scripts = json_decode(file_get_contents($_SERVER["DOCUMENT_ROOT"] . '/../scriptmetadata.json'), true); + if ($cmd_id = $_GET['id'] ?? false) { - $command = array_find( + $script = array_find( $scripts, fn($x) => $x['name'] == $cmd_id || (isset($x['aliases']) ? in_array($cmd_id, $x['aliases']) : false) ); - if (!$command) { + if (!$script) { http_response_code(404); exit; } } -if ($command) { - if (isset($command['description'])) { - $command['description_formatted'] = $parsedown->text($command['description']); - } -} ?> - <?= isset($command) ? "!{$command['name']} - Script" : 'Scripts' ?> - <?= BOT_USERNAME_FORMATTED ?> + <?= isset($script) ? "!{$script['name']} - Script" : 'Scripts' ?> - <?= BOT_USERNAME_FORMATTED ?> @@ -91,9 +51,9 @@ if ($command) {
- -

+ +

@@ -101,38 +61,38 @@ if ($command) {
- -

! +

![code]

- + - + - + - +
Delay
Subcommands - "$x", $command['subcommand'])) ?> + "$x", $script['subcommands'])) ?>
Aliases - "$x", $command['aliases'])) ?> + "$x", $script['aliases'])) ?>
Minimal rights
- - + +

No description.

-- cgit v1.2.3