Files
xiao/commands/util/eval.js
T
2024-03-30 01:02:07 -04:00

118 lines
3.6 KiB
JavaScript

// Credit: https://github.com/discordjs/Commando/blob/master/src/commands/util/eval.js
const util = require('util');
/* eslint-disable no-unused-vars, prefer-destructuring */
const discord = require('discord.js');
const Util = require('../../util/Util');
/* eslint-enable no-unused-vars, prefer-destructuring */
const tags = require('common-tags');
const Command = require('../../framework/Command');
const nl = '!!NL!!';
const nlPattern = new RegExp(nl, 'g');
module.exports = class EvalCommand extends Command {
constructor(client) {
super(client, {
name: 'eval',
group: 'util',
memberName: 'eval',
description: 'Executes JavaScript code.',
details: 'Only the bot owner(s) may use this command.',
ownerOnly: true,
guarded: true,
args: [
{
key: 'script',
type: 'string'
}
]
});
this.lastResult = null;
Object.defineProperty(this, '_sensitivePattern', { value: null, configurable: true });
}
run(msg, args) {
// Make a bunch of helpers
/* eslint-disable no-unused-vars, prefer-destructuring */
const message = msg;
const client = msg.client;
const lastResult = this.lastResult;
const doReply = val => {
if (val instanceof Error) {
msg.reply(`Callback error: \`${val}\``);
} else {
const result = this.makeResultMessages(val, process.hrtime(this.hrStart));
if (Array.isArray(result)) {
for (const item of result) msg.reply(item);
} else {
msg.reply(result);
}
}
};
/* eslint-enable no-unused-vars, prefer-destructuring */
// Remove any surrounding code blocks before evaluation
if (args.script.startsWith('```') && args.script.endsWith('```')) {
args.script = args.script.replace(/(^.*?\s)|(\n.*$)/g, '');
}
// Run the code and measure its execution time
let hrDiff;
try {
const hrStart = process.hrtime();
this.lastResult = eval(args.script);
hrDiff = process.hrtime(hrStart);
} catch (err) {
return msg.reply(`Error while evaluating: \`${err}\``);
}
// Prepare for callback time and respond
this.hrStart = process.hrtime();
const result = this.makeResultMessages(this.lastResult, hrDiff, args.script);
if (Array.isArray(result)) {
return result.map(item => msg.reply(item));
} else {
return msg.reply(result);
}
}
makeResultMessages(result, hrDiff, input = null) {
const inspected = util.inspect(result, { depth: 0 })
.replace(nlPattern, '\n')
.replace(this.sensitivePattern, '--snip--');
const split = inspected.split('\n');
const last = inspected.length - 1;
const prependPart = inspected[0] !== '{' && inspected[0] !== '[' && inspected[0] !== '\'' ? split[0] : inspected[0];
const appendPart = inspected[last] !== '}' && inspected[last] !== ']' && inspected[last] !== '\''
? split[split.length - 1]
: inspected[last];
const prepend = `\`\`\`javascript\n${prependPart}\n`;
const append = `\n${appendPart}\n\`\`\``;
if (input) {
return Util.splitMessage(tags.stripIndents`
*Executed in ${hrDiff[0] > 0 ? `${hrDiff[0]}s ` : ''}${hrDiff[1] / 1000000}ms.*
\`\`\`javascript
${inspected}
\`\`\`
`, { maxLength: 1900, prepend, append });
} else {
return Util.splitMessage(tags.stripIndents`
*Callback executed after ${hrDiff[0] > 0 ? `${hrDiff[0]}s ` : ''}${hrDiff[1] / 1000000}ms.*
\`\`\`javascript
${inspected}
\`\`\`
`, { maxLength: 1900, prepend, append });
}
}
get sensitivePattern() {
if (!this._sensitivePattern) {
let pattern = '';
if (this.client.token) pattern += Util.escapeRegex(this.client.token);
Object.defineProperty(this, '_sensitivePattern', { value: new RegExp(pattern, 'gi'), configurable: false });
}
return this._sensitivePattern;
}
};