This commit is contained in:
NoLogicAlan committed 2026-05-14 22:58:42 +03:00
1 parent 0f526c2d38
commit 9e8e401b0e
1 file changed
+58 -3
+58 -3
View File
@@ -81,7 +81,7 @@ async function clean(value) {
let output; let output;
try { try {
output = typeof value === "string" ? value : inspect(value, { depth: 1, compact: false }); output = typeof value === "string" ? value : inspect(value, { depth: 4, compact: false, maxArrayLength: 50, maxStringLength: 500, breakLength: 80 });
} catch (err) { } catch (err) {
output = `[Inspection Error]: ${err.message}`; output = `[Inspection Error]: ${err.message}`;
} }
@@ -91,6 +91,34 @@ async function clean(value) {
.replace(/@/g, "@\u200b") || "undefined"; // Fallback if output is empty .replace(/@/g, "@\u200b") || "undefined"; // Fallback if output is empty
} }
function isSingleExpression(code) {
const trimmed = code.trim();
// Empty code
if (!trimmed) return false;
// Block statements — user is writing a full function body
if (/^(if|for|while|do|switch|try|catch|finally|with)\s*[\({]/.test(trimmed)) return false;
// Declarations — always multi-statement style
if (/^\s*(const|let|var|function\s|class\s)/.test(trimmed)) return false;
// Contains semicolons that look like statement separators (but not inside strings)
// Strip string literals to avoid false positives from semicolons inside strings
const stripped = trimmed
.replace(/'(?:[^'\\]|\\.)*'/g, '""') // single-quoted strings
.replace(/"(?:[^"\\]|\\.)*"/g, '""') // double-quoted strings
.replace(/`(?:[^`\\]|\\.)*`/g, '""'); // template literals (simplified)
if (stripped.includes(";")) return false;
// Contains newlines that suggest multiple statements
// (but allow multi-line method chains like `.foo()\n.bar()`)
const withoutChains = trimmed.replace(/\.\s*\n/g, ".");
if (withoutChains.includes("\n")) return false;
return true;
}
/** /**
* Executes the code and returns the raw data instead of a formatted string, * Executes the code and returns the raw data instead of a formatted string,
* so we can paginate it inside Embeds later. * so we can paginate it inside Embeds later.
@@ -100,7 +128,13 @@ async function runEval(expression, context) {
let result, isError = false, type = "undefined"; let result, isError = false, type = "undefined";
try { try {
result = await eval(`(async function() { ${expression} })`).call(context); // Auto-return single expressions so users don't need to write `return`
const code = isSingleExpression(expression)
? `return (${expression});`
: expression;
// Use eval() — runs in module scope so `this` is the context passed via .call()
result = await eval(`(async function() { ${code} })`).call(context);
type = result === null ? "null" : typeof result; type = result === null ? "null" : typeof result;
} catch (e) { } catch (e) {
result = e; result = e;
@@ -127,7 +161,28 @@ export const command = new CommandBuilder()
export async function run(msg, data) { export async function run(msg, data) {
const expression = data.get("expression").value; const expression = data.get("expression").value;
const context = Object.assign({ message: msg }, this);
// Build a rich context with shorthand variables so eval can access
// everything easily via `this`. `this` = Remix instance merged with
// the extra shortcuts below.
// this.client → Fluxer Client
// this.guilds → this.client.guilds
// this.channels → this.client.channels
// this.users → this.client.users
// this.players → PlayerManager
// this.settings → SettingsManager
// this.msg → the Message wrapper
// this.message → the raw fluxer message object
const context = Object.assign({
message: msg?.message,
msg,
// Shorthand aliases for convenience
guilds: this.client?.guilds,
channels: this.client?.channels,
users: this.client?.users,
players: this.players,
settings: this.settingsMgr,
}, this);
// 1. Run the evaluation // 1. Run the evaluation
const { output, isError, type, elapsed } = await runEval(expression, context); const { output, isError, type, elapsed } = await runEval(expression, context);