summaryrefslogtreecommitdiff
path: root/public/scripts.php
diff options
context:
space:
mode:
authormoderndevslulw <moderndevslulw@alright.party>2025-07-07 04:21:48 +0500
committermoderndevslulw <moderndevslulw@alright.party>2025-07-07 04:21:48 +0500
commitebc27728975cd669b7167e5631afbdf5ed2db432 (patch)
tree5135ce75351da4f11863c374b5020324b3629012 /public/scripts.php
parentee4df5bfd30a4e087331adc2b4b306286e5d419d (diff)
feat: show lua scripts
Diffstat (limited to 'public/scripts.php')
-rw-r--r--public/scripts.php151
1 files changed, 151 insertions, 0 deletions
diff --git a/public/scripts.php b/public/scripts.php
new file mode 100644
index 0000000..0f80be4
--- /dev/null
+++ b/public/scripts.php
@@ -0,0 +1,151 @@
+<?php
+include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/partials.php';
+include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/utils.php';
+include_once $_SERVER['DOCUMENT_ROOT'] . '/../vendor/autoload.php';
+include_once $_SERVER['DOCUMENT_ROOT'] . '/../config.php';
+
+use Raudius\Luar\Luar;
+
+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;
+}
+
+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 ($cmd_id = $_GET['id'] ?? false) {
+ $command = array_find(
+ $scripts,
+ fn($x) =>
+ $x['name'] == $cmd_id || (isset($x['aliases']) ? in_array($cmd_id, $x['aliases']) : false)
+ );
+
+ if (!$command) {
+ http_response_code(404);
+ exit;
+ }
+}
+
+if ($command) {
+ if (isset($command['description'])) {
+ $command['description_formatted'] = $parsedown->text($command['description']);
+ }
+}
+?>
+<html>
+
+<head>
+ <title><?= isset($command) ? "!{$command['name']} - Script" : 'Scripts' ?> - <?= BOT_USERNAME_FORMATTED ?></title>
+ <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
+ <link rel="stylesheet" href="/static/style.css">
+ <link rel="shortcut icon" href="/static/favicon.ico" type="image/x-icon">
+</head>
+
+<body>
+ <main>
+ <?php html_navigation_bar() ?>
+ <content>
+ <div class="row gap-8 background-colorful p-4" style="box-shadow: 0 2px 1px rgba(0, 0, 0, 0.25);z-index:1;">
+ <img src="/static/img/icons/script.png" alt="">
+ <h2>Scripts</h2>
+ </div>
+ <div class="row">
+ <?php if (!empty($scripts)): ?>
+ <!-- SIDE BAR -->
+ <div class="wiki-sidebar">
+ <?php foreach ($scripts as $script): ?>
+ <p><a href="/!<?= $script['name'] ?>"
+ title="<?= substr($script['description_text'] ?? '', 0, 64) . (strlen($script['description_text']) > 64 ? '...' : '') ?>"><?= $script['name'] ?></a>
+ </p>
+ <?php endforeach; ?>
+ </div>
+ <?php endif; ?>
+
+ <!-- WIKI CONTENT -->
+ <div class="wiki-content">
+ <?php if (isset($command)): ?>
+ <h1>&excl;<?= $command['name'] ?><a href="/!/<?= $command['name'] ?>.lua"><img
+ src="/static/img/icons/code.png" alt="[code]" title="Check the source code"></a></h1>
+ <table class="vertical">
+ <tr>
+ <th>Delay</th>
+ <td><?= format_timestamp($command['delay_sec']) ?></td>
+ </tr>
+ <?php if (!empty($command['subcommands'])): ?>
+ <tr>
+ <th>Subcommands</th>
+ <td>
+ <?= implode(', ', array_map(fn($x) => "<code>$x</code>", $command['subcommand'])) ?>
+ </td>
+ </tr>
+ <?php endif; ?>
+ <?php if (!empty($command['aliases'])): ?>
+ <tr>
+ <th>Aliases</th>
+ <td>
+ <?= implode(', ', array_map(fn($x) => "<code>$x</code>", $command['aliases'])) ?>
+ </td>
+ </tr>
+ <?php endif; ?>
+ <tr>
+ <th>Minimal rights</th>
+ <td><?= $command['minimal_rights'] ?></td>
+ </tr>
+ </table>
+ <div>
+ <?php if (isset($command['description_formatted'])): ?>
+ <?= $command['description_formatted'] ?>
+ <?php else: ?>
+ <p><i>No description.</i></p>
+ <?php endif; ?>
+ </div>
+ <?php else: ?>
+ <p><i>Select a script from the list on the left.</i></p>
+ <?php endif; ?>
+ </div>
+ </div>
+ </content>
+ </main>
+
+ <?php html_footer() ?>
+</body>
+
+</html> \ No newline at end of file