@nexint/lumina 0.0.0-stage → 0.7.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,128 @@
1
+ // Sample code to import a client and other constants.
2
+ /*
3
+ const { restart } = require('../../config.js');
4
+ const { helpMenu, readline, spawn, exitHandler, server } = require('../../index.js');
5
+ const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
6
+ */
7
+ const { frameName } = require('#config/core/framework');
8
+ const { getConfig } = require("#functions/core/getConfig");
9
+ const { restart } = getConfig("core/core");
10
+ const { pluginName } = getConfig("cosmetic/config");
11
+ const { date } = require("#functions/core/date");
12
+ const delay = require("#functions/core/delay");
13
+ const { frameworkDir, standalone, root } = require("#functions/core/dirs");
14
+ const { loadHelp } = require("#functions/core/load");
15
+ const { log } = require('#functions/core/log.mjs');
16
+ const { gracefulShutdown, refreshCycle, reload } = require('#functions/core/powerCycle');
17
+ const { spawn } = require('child_process');
18
+
19
+ module.exports = {
20
+ // Example of a help menu given by the bot.
21
+ // Input: none
22
+ // Expected Return: An array of strings.
23
+ help() {
24
+ return [
25
+ 'Built-in:',
26
+ '/help (page) - Opens this help menu.',
27
+ '/h (page) - Mirror of /help.',
28
+ 'The help menu can be expanded upon by other plugins.',
29
+ '/info (page) - Opens a neat info menu!',
30
+ 'This function can\'t be expanded upon by other plugins.',
31
+ '/reload - Mirror of /refresh',
32
+ '/refresh - Refreshes commands, etc.',
33
+ '/restart - Restarts the bot. Recommended when updating.',
34
+ '/rs - Mirror of /restart',
35
+ '/stop - Closes bot.',
36
+ '/end - Mirror of /stop.',
37
+ ];
38
+ },
39
+ // Example of a command in the CLI (command line) of the bot.
40
+ // Input: command given by the bot
41
+ // Expected Return: true or false depending on if the command was valid or not. Otherwise, this breaks the code used to determine if a command is valid or not.
42
+ async command(command) {
43
+ switch (command.split(" ")[0].toLowerCase()) {
44
+ case "/h":
45
+ case "/help":
46
+
47
+ let helpMenu = await loadHelp("console", frameworkDir);
48
+
49
+ if (!standalone) {
50
+ helpMenu = helpMenu.concat(await loadHelp("console", root));
51
+ }
52
+
53
+ // Help Menu
54
+ let page;
55
+ if (command.split(" ")[1] != undefined) {
56
+ page = Number(parseInt(command.split(" ")[1].toLowerCase(), 10));
57
+ if (isNaN(page) || page < 1) {
58
+ page = 1;
59
+ }
60
+ } else {
61
+ page = 1;
62
+ }
63
+ log.info(`\x1b[36m----------------- Help menu: (${page}/${Math.ceil((helpMenu.length) / 8)}) -----------------\x1b[0m`)
64
+ for (let i = (page - 1) * 8; i < Math.min(8 + ((page - 1) * 8), helpMenu.length); i++) {
65
+ log.info(helpMenu[i])
66
+ }
67
+ log.info(`\x1b[36m--------------------- Nex ${date.getFullYear()} ---------------------\x1b[0m`)
68
+ return true;
69
+ case "/info":
70
+ let info;
71
+ const infoList = [
72
+ 'Version control:',
73
+ 'The first number (X.0.0) is the major release.',
74
+ 'The second number (0.X.0) is the minor release.',
75
+ 'The third number (0.0.X) is the patch #.',
76
+ 'Breaking changes may occur between Prism 0.x releases.',
77
+ 'Compatibility is not guaranteed until v1.0.0.',
78
+ 'Built-in plugins aside from "built-in" are typically',
79
+ 'not updated as often, and may be unstable.',
80
+ 'What means what?',
81
+ `${frameName} version is the version of the NodeJS framework.`,
82
+ `${pluginName} version is the version of the plugins / deployment.`,
83
+ 'Dev is the dev instance, prod is the prod instance.',
84
+ 'Disclaimers:',
85
+ 'Third party plugins are not guaranteed to be supported.',
86
+ 'Prism is in active development and may introduce breaking changes.',
87
+ 'Have fun running this! - Nexint',
88
+ ];
89
+ if (command.split(" ")[1] != undefined) {
90
+ info = Number(parseInt(command.split(" ")[1].toLowerCase(), 10));
91
+ if (isNaN(info) || info < 1) {
92
+ info = 1;
93
+ }
94
+ } else {
95
+ info = 1;
96
+ }
97
+ log.info(`\x1b[36m----------------- Info menu: (${info}/${Math.ceil((infoList.length) / 8)}) -----------------\x1b[0m`)
98
+ for (let i = (info - 1) * 8; i < Math.min(8 + ((info - 1) * 8), infoList.length); i++) {
99
+ log.info(infoList[i])
100
+ }
101
+ log.info(`\x1b[36m--------------------- Nex ${date.getFullYear()} ---------------------\x1b[0m`)
102
+ return true;
103
+ case "/end":
104
+ case "/stop":
105
+ await gracefulShutdown("Console // Shutdown", true)
106
+ return false; // This should not run, but like
107
+ case "/rs":
108
+ case "/restart":
109
+ await gracefulShutdown("Console // Restart", false)
110
+ const child = spawn(restart, {
111
+ detached: true,
112
+ shell: true,
113
+ stdio: 'ignore'
114
+ })
115
+ child.unref()
116
+ await delay(1000)
117
+ process.exit(0);
118
+ case "/refresh":
119
+ await refreshCycle("Console // Refresh")
120
+ return true;
121
+ case "/reload":
122
+ await reload("Console // Reload")
123
+ return true;
124
+ default:
125
+ return false;
126
+ }
127
+ }
128
+ }
@@ -0,0 +1,81 @@
1
+ const { frameName, frameVer, frameID } = require('#config/core/framework');
2
+ const { getConfig } = require("#functions/core/getConfig");
3
+ const { pluginName, pluginVer } = getConfig("cosmetic/config");
4
+ const { date } = require('#functions/core/date');
5
+ const { log, getInfo, getSuccess, getWarn, getError, getFatal, getEvent } = require('#functions/core/log.mjs');
6
+ const { getUpdateVersion } = require('#functions/core/update');
7
+
8
+ module.exports = {
9
+ // Example of a help menu given by the bot.
10
+ // Input: none
11
+ // Expected Return: An array of strings.
12
+ help() {
13
+ return [
14
+ 'Developer:',
15
+ '/print (args) - Prints stuff to the console!',
16
+ 'By default, /print prints what possible arguments there are.',
17
+ ];
18
+ },
19
+ // Example of a command in the CLI (command line) of the bot.
20
+ // Input: command given by the bot, readline function, client
21
+ // Expected Return: true or false depending on if the command was valid or not. Otherwise, this breaks the code used to determine if a command is valid or not.
22
+ async command(command) {
23
+ switch (command.split(" ")[0].toLowerCase()) {
24
+ case "/print": // Prints various things. This mostly serves as a developer command.
25
+ let print;
26
+ if (command.split(" ")[1] != undefined) {
27
+ print = command.split(" ")[1].toLowerCase();
28
+ }
29
+ switch (print) {
30
+ case "v":
31
+ case 'version':
32
+ log.info(`\x1b[36m------------------- Version Info -------------------\x1b[0m`)
33
+ const [, latestVersion] = await getUpdateVersion() || [];
34
+ log.info("Printing the version...")
35
+ log.info(`${pluginName} Version: ${pluginVer}`)
36
+ log.info(`${frameName} Version: ${frameVer} (Version ID: ${frameID}) / Latest version ID: ${latestVersion}.`)
37
+ if (latestVersion > frameID) {
38
+ log.info(`${latestVersion - frameID} commits behind.`)
39
+ } else if (!latestVersion) {
40
+ log.warn(`Updates may or may not be working.`)
41
+ } else if (latestVersion < frameID) {
42
+ log.info(`${frameID - latestVersion} commits ahead.`)
43
+ } else {
44
+ log.info(`Running the latest version.`)
45
+ }
46
+ log.info(`\x1b[36m--------------------- Nex ${date.getFullYear()} ---------------------\x1b[0m`)
47
+ break;
48
+ case "b":
49
+ case 'build':
50
+ log.info(`\x1b[36m-------------------- Build Info --------------------\x1b[0m`)
51
+ log.info(`Printing the build!`)
52
+ log.info(`${pluginName} v${pluginVer}`)
53
+ log.info(`${frameName} v${frameVer}`)
54
+ log.info(`\x1b[36m--------------------- Nex ${date.getFullYear()} ---------------------\x1b[0m`)
55
+ break;
56
+ case 'l':
57
+ case 'logs':
58
+ log.info(`\x1b[36m--------------------- Log Info ---------------------\x1b[0m`)
59
+ log.info(`${getInfo() + getSuccess() + getWarn() + getError() + getFatal() + getEvent()} total events.`)
60
+ log.info(`${getInfo() - 1} info events.`)
61
+ log.info(`${getSuccess()} successful events.`)
62
+ log.info(`${getWarn()} warning events.`)
63
+ log.info(`${getError()} error events.`)
64
+ log.info(`${getFatal()} fatal events.`)
65
+ log.info(`${getEvent()} general events.`)
66
+ log.info(`\x1b[36m--------------------- Nex ${date.getFullYear()} ---------------------\x1b[0m`)
67
+ break;
68
+ default:
69
+ log.info(`Not a valid argument.`)
70
+ log.info(`Valid arguments:`)
71
+ log.info(`version - Prints version info`)
72
+ log.info(`build - Prints ${frameName} build`)
73
+ log.info(`logs - Prints the log events.`)
74
+ log.info(`\x1b[36m--------------------- Nex ${date.getFullYear()} ---------------------\x1b[0m`)
75
+ }
76
+ return true;
77
+ default:
78
+ return false;
79
+ }
80
+ }
81
+ }
@@ -0,0 +1,57 @@
1
+ const { clearDirectory } = require('#functions/core/clearCache');
2
+ const { date } = require('#functions/core/date');
3
+ const { load, queryFiles } = require('#functions/core/load');
4
+ const { log } = require('#functions/core/log.mjs');
5
+
6
+ module.exports = {
7
+ // Example of a help menu given by the bot.
8
+ // Input: none
9
+ // Expected Return: An array of strings.
10
+ help() {
11
+ return [
12
+ 'Developer:',
13
+ '/test: Test the bot features!',
14
+ 'By default, /print prints what possible arguments there are.',
15
+ ];
16
+ },
17
+ // Example of a command in the CLI (command line) of the bot.
18
+ // Input: command given by the bot, readline function, client
19
+ // Expected Return: true or false depending on if the command was valid or not. Otherwise, this breaks the code used to determine if a command is valid or not.
20
+ async command(command) {
21
+ switch (command.split(" ")[0].toLowerCase()) {
22
+ case "/test": // Prints various things. This mostly serves as a developer command.
23
+ let print;
24
+ if (command.split(" ")[1] != undefined) {
25
+ print = command.split(" ")[1].toLowerCase();
26
+ }
27
+ switch (print) {
28
+ case "a":
29
+ case 'all':
30
+ log.info(`\x1b[36m----------------------- Test -----------------------\x1b[0m`)
31
+ clearDirectory("test")
32
+ const testFiles = await load("test")
33
+ log.success(`Tested ${testFiles} cases.`)
34
+
35
+ log.info(`\x1b[36m--------------------- Nex ${date.getFullYear()} ---------------------\x1b[0m`)
36
+ break;
37
+ case "i":
38
+ case 'info':
39
+ log.info(`\x1b[36m-------------------- Build Info --------------------\x1b[0m`)
40
+ log.info(`Printing the test info!`)
41
+ const count = await queryFiles("test")
42
+ log.info(`Test files: ${count}`)
43
+ log.info(`\x1b[36m--------------------- Nex ${date.getFullYear()} ---------------------\x1b[0m`)
44
+ break;
45
+ default:
46
+ log.info(`Not a valid argument.`)
47
+ log.info(`Valid arguments:`)
48
+ log.info(`all - Test everything`)
49
+ log.info(`info - Information about the tester!`)
50
+ log.info(`\x1b[36m--------------------- Nex ${date.getFullYear()} ---------------------\x1b[0m`)
51
+ }
52
+ return true;
53
+ default:
54
+ return false;
55
+ }
56
+ }
57
+ }
@@ -0,0 +1,3 @@
1
+ # Databases
2
+
3
+ This directory holds databases that may be useful!
@@ -0,0 +1,3 @@
1
+ # Functions
2
+
3
+ This lists the functions that this server relies on lol
@@ -0,0 +1,34 @@
1
+ const { getConfig } = require("#functions/core/getConfig");
2
+ const { verbose } = getConfig("core/core");
3
+ const path = require("node:path");
4
+ const { log } = require("./log.mjs");
5
+
6
+ /**
7
+ * clear directory lol
8
+ * @param {String} dir
9
+ * @param {String} Root
10
+ * @returns
11
+ */
12
+ const clearDirectory = function (dir, root = process.cwd()) {
13
+ let cleared = 0;
14
+
15
+ for (const filename of Object.keys(require.cache)) {
16
+ const relative = path.relative(root, filename);
17
+
18
+ if (relative.startsWith(dir + path.sep)) {
19
+ delete require.cache[filename];
20
+
21
+ if (verbose) {
22
+ log.info(`Deleted cache for ${filename}`)
23
+ }
24
+
25
+ cleared += 1;
26
+ }
27
+ }
28
+
29
+ return cleared;
30
+ }
31
+
32
+ module.exports = {
33
+ clearDirectory
34
+ }
@@ -0,0 +1,4 @@
1
+ const date = new Date()
2
+ module.exports = {
3
+ date
4
+ }
@@ -0,0 +1,4 @@
1
+
2
+ const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
3
+
4
+ module.exports = delay
@@ -0,0 +1,12 @@
1
+ // Configure root directories
2
+ const path = require("node:path");
3
+
4
+ const root = process.cwd();
5
+ const frameworkDir = path.resolve(__dirname, "../..");
6
+ const standalone = root === frameworkDir;
7
+
8
+ module.exports = {
9
+ root,
10
+ frameworkDir,
11
+ standalone
12
+ };
@@ -0,0 +1,174 @@
1
+ import { styleText } from "node:util";
2
+ import dateCore from "./date.js";
3
+ import config from "#functions/core/getConfig";
4
+ import framework from "#config/core/framework";
5
+ import fs from "node:fs";
6
+ import path from "node:path";
7
+ import { createRequire } from "node:module";
8
+ import dirs from "#functions/core/dirs";
9
+
10
+ const { root, frameworkDir, standalone } = dirs;
11
+
12
+ const require = createRequire(import.meta.url);
13
+
14
+ const { getConfig } = config;
15
+ const { dateOptions, verbose } = getConfig("core/core");
16
+ const { frameName } = framework;
17
+ const { date } = dateCore;
18
+ const DateFormatter = new Intl.DateTimeFormat(undefined, dateOptions)
19
+ const SHUTDOWN_TIMEOUT = 5000;
20
+
21
+
22
+ const log = {
23
+
24
+ /**
25
+ * Info event
26
+ * @param {String} text
27
+ */
28
+ info: function (text) {
29
+ console.log(`[INFO] [${DateFormatter.format(Date.now())}] ${text}`);
30
+ },
31
+
32
+ /**
33
+ * Error event
34
+ * @param {String} text
35
+ * @param {Error} error
36
+ */
37
+ error: function (text, error) {
38
+
39
+ const messages = [
40
+ `[ERROR] [${DateFormatter.format(Date.now())}] ${text}`,
41
+ ]
42
+
43
+ for (const message of messages) {
44
+ console.error(styleText("redBright", message))
45
+ }
46
+
47
+ if (error) {
48
+ console.error(error)
49
+ }
50
+ },
51
+
52
+ /**
53
+ * Log a given event lol
54
+ * @param {String} text
55
+ */
56
+ event: function (text) {
57
+
58
+ const messages = [
59
+ `[EVENT] [${DateFormatter.format(Date.now())}] ${text}`,
60
+ ]
61
+
62
+ for (const message of messages) {
63
+ console.log(styleText("blueBright", message))
64
+ }
65
+ },
66
+ }
67
+
68
+
69
+
70
+ /**
71
+ * Close everything (shut it all down) safely.
72
+ * @param {String} dir
73
+ * @param {String} root
74
+ */
75
+ const close = async function (dir, root = process.cwd()) {
76
+
77
+ let success = true;
78
+
79
+ const dirPath = path.join(root, dir);
80
+
81
+ if (!fs.existsSync(dirPath)) {
82
+ return true;
83
+ }
84
+
85
+ const files = fs.readdirSync(dirPath, { recursive: true });
86
+ for (const file of files) {
87
+
88
+ // Only load JS files (trust)
89
+ if ([".js", ".cjs", ".mjs"].includes(path.extname(file).toLowerCase())) {
90
+
91
+ // Load the help menu?
92
+ try {
93
+ const filename = require.resolve(path.join(root, dir, file));
94
+ const cached = require.cache[filename];
95
+
96
+ if (!cached || !cached.loaded) {
97
+ continue;
98
+ }
99
+
100
+ const app = cached.exports;
101
+
102
+ if (typeof app.close === "function") {
103
+ await app.close();
104
+
105
+ if (verbose) {
106
+ log.info(`Closed file: ${path.join(dir, file)}`);
107
+ }
108
+ }
109
+ } catch (error) {
110
+ log.error("Failed to close app.")
111
+ log.error(`Problematic file: ${path.join(dir, file)}`, error)
112
+ success = false
113
+ }
114
+ }
115
+ }
116
+ return success;
117
+ }
118
+
119
+ let shutDownState = null;
120
+
121
+ /**
122
+ * Force Shutdown of bot
123
+ * @param {String} cause
124
+ */
125
+ const forceShutdown = async function (cause) {
126
+ if (shutDownState) {
127
+ return;
128
+ }
129
+ shutDownState = true;
130
+
131
+ // Hard deadline, independent of the cleanup promises.
132
+ const watchdog = setTimeout(() => {
133
+ try {
134
+ log.error("Forced shutdown exceeded its timeout.");
135
+ } catch {
136
+ process.exit(2)
137
+ }
138
+ process.exit(1);
139
+ }, SHUTDOWN_TIMEOUT);
140
+
141
+ log.info(`\x1b[36m--------------------- Nex ${date.getFullYear()} ---------------------\x1b[0m`)
142
+ log.event(`Forced Shutdown triggered by: ${cause}`)
143
+ log.event("Closing all apps...")
144
+ try {
145
+ let success = true;
146
+ let dirs = ["apps", "plugins", "preload"];
147
+ for (const dir of dirs) {
148
+ if (!await close(dir, root)) {
149
+ success = false
150
+ }
151
+ }
152
+ if (!standalone) {
153
+ let dirs = ["plugins", "preload"];
154
+ for (const dir of dirs) {
155
+ if (!await close(dir, frameworkDir)) {
156
+ success = false;
157
+ }
158
+ }
159
+ }
160
+ if (!success) {
161
+ log.error("One or more components failed to close.");
162
+ } else {
163
+ log.event("All components closed successfully.");
164
+ }
165
+ } catch (error) {
166
+ log.error(`${frameName} could not be force shut down properly.`, error)
167
+ process.exit(1);
168
+ }
169
+ log.event("Shutting down.")
170
+ log.info(`\x1b[36m----------------------------------------------------\x1b[0m`)
171
+ process.exit(1);
172
+ }
173
+
174
+ export { forceShutdown, log };
@@ -0,0 +1,32 @@
1
+ const fs = require("node:fs");
2
+ const path = require("node:path");
3
+ const { root, frameworkDir, standalone } = require("./dirs");
4
+
5
+ /**
6
+ * get config for the framework lol
7
+ * @param {String} name
8
+ * @returns {Object}
9
+ */
10
+ const getConfig = function (name) {
11
+
12
+ const frameworkPath = path.join(frameworkDir, "config", `${name}.js`);
13
+
14
+ const appPath = path.join(root, "config", `${name}.js`);
15
+
16
+ let config = {};
17
+
18
+ if (fs.existsSync(frameworkPath)) {
19
+ config = require(frameworkPath);
20
+ }
21
+
22
+ if (!standalone && fs.existsSync(appPath)) {
23
+ config = {
24
+ ...config,
25
+ ...require(appPath)
26
+ };
27
+ }
28
+
29
+ return config;
30
+ }
31
+
32
+ module.exports = { getConfig };
@@ -0,0 +1,36 @@
1
+ // I did not like the fact there was no gradients lol
2
+ // so I just forced AI to make it.
3
+
4
+ const { styleText } = require("node:util");
5
+
6
+ /**
7
+ * Apply a linear colour gradient to text.
8
+ * @param {string} text
9
+ * @param {string} from - Starting hex colour.
10
+ * @param {string} to - Ending hex colour.
11
+ * @returns {string}
12
+ */
13
+ const gradient = function (text, from, to) {
14
+ const start = Buffer.from(from.slice(1), "hex");
15
+ const end = Buffer.from(to.slice(1), "hex");
16
+
17
+ const chars = [...text];
18
+ let result = "";
19
+
20
+ for (let i = 0; i < chars.length; i++) {
21
+ const progress = i / Math.max(chars.length - 1, 1);
22
+ let color = "#";
23
+
24
+ // Calculate the RGB values
25
+ for (let j = 0; j < 3; j++) {
26
+ const value = Math.round(start[j] + (end[j] - start[j]) * progress);
27
+ color += value.toString(16).padStart(2, "0");
28
+ }
29
+
30
+ result += styleText(color, chars[i]);
31
+ }
32
+
33
+ return result;
34
+ }
35
+
36
+ module.exports = { gradient };