Archived
0
0
Fork 0

feat(commands): finished vm command

This commit is contained in:
Daryl Ronningen 2021-06-21 15:31:29 -07:00
parent b8176e0d5c
commit d6b2470a8b
Signed by: Daryl Ronningen
GPG key ID: FD23F0C934A5EC6B
5 changed files with 45 additions and 10 deletions

View file

@ -54,6 +54,7 @@
<TypeScriptCodeStyleSettings version="0"> <TypeScriptCodeStyleSettings version="0">
<option name="FORCE_SEMICOLON_STYLE" value="true" /> <option name="FORCE_SEMICOLON_STYLE" value="true" />
<option name="USE_PUBLIC_MODIFIER" value="true" /> <option name="USE_PUBLIC_MODIFIER" value="true" />
<option name="PREFER_AS_TYPE_CAST" value="true" />
<option name="USE_DOUBLE_QUOTES" value="false" /> <option name="USE_DOUBLE_QUOTES" value="false" />
<option name="FORCE_QUOTE_STYlE" value="true" /> <option name="FORCE_QUOTE_STYlE" value="true" />
<option name="ENFORCE_TRAILING_COMMA" value="WhenMultiline" /> <option name="ENFORCE_TRAILING_COMMA" value="WhenMultiline" />
@ -91,6 +92,7 @@
<option name="ALIGN_MULTILINE_FOR" value="false" /> <option name="ALIGN_MULTILINE_FOR" value="false" />
<option name="SPACE_BEFORE_IF_PARENTHESES" value="false" /> <option name="SPACE_BEFORE_IF_PARENTHESES" value="false" />
<option name="SPACE_BEFORE_CATCH_PARENTHESES" value="false" /> <option name="SPACE_BEFORE_CATCH_PARENTHESES" value="false" />
<option name="SPACE_BEFORE_SWITCH_PARENTHESES" value="false" />
<indentOptions> <indentOptions>
<option name="INDENT_SIZE" value="2" /> <option name="INDENT_SIZE" value="2" />
<option name="CONTINUATION_INDENT_SIZE" value="2" /> <option name="CONTINUATION_INDENT_SIZE" value="2" />
@ -113,6 +115,7 @@
<option name="ALIGN_MULTILINE_FOR" value="false" /> <option name="ALIGN_MULTILINE_FOR" value="false" />
<option name="SPACE_BEFORE_IF_PARENTHESES" value="false" /> <option name="SPACE_BEFORE_IF_PARENTHESES" value="false" />
<option name="SPACE_BEFORE_CATCH_PARENTHESES" value="false" /> <option name="SPACE_BEFORE_CATCH_PARENTHESES" value="false" />
<option name="SPACE_BEFORE_SWITCH_PARENTHESES" value="false" />
<indentOptions> <indentOptions>
<option name="INDENT_SIZE" value="2" /> <option name="INDENT_SIZE" value="2" />
<option name="CONTINUATION_INDENT_SIZE" value="2" /> <option name="CONTINUATION_INDENT_SIZE" value="2" />

View file

@ -50,6 +50,7 @@
"sodium": "^3.0.2", "sodium": "^3.0.2",
"terminal-link": "^3.0.0", "terminal-link": "^3.0.0",
"tslib": "^2.3.0", "tslib": "^2.3.0",
"typescript": "^4.3.4",
"utf-8-validate": "^5.0.5", "utf-8-validate": "^5.0.5",
"vm2": "^3.9.3", "vm2": "^3.9.3",
"zlib-sync": "^0.1.7" "zlib-sync": "^0.1.7"
@ -93,7 +94,6 @@
"standard-version": "^9.3.0", "standard-version": "^9.3.0",
"ts-node": "^10.0.0", "ts-node": "^10.0.0",
"tsconfig-paths": "^3.9.0", "tsconfig-paths": "^3.9.0",
"typescript": "^4.3.4",
"typescript-eslint-language-service": "^4.1.4" "typescript-eslint-language-service": "^4.1.4"
}, },
"eslintConfig": { "eslintConfig": {

View file

@ -14,27 +14,58 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with ArgonBot. If not, see <https: //www.gnu.org/licenses/>. * along with ArgonBot. If not, see <https: //www.gnu.org/licenses/>.
*/ */
import { VM } from 'vm2'; import { NodeVM } from 'vm2';
import { transpile } from 'typescript';
import Command from '@structures/command'; import Command from '@structures/command';
import type { Client, Message } from 'discord.js'; import type { Client, Message } from 'discord.js';
export class vm extends Command { export class vm extends Command {
public constructor(client: Client) { public constructor(client: Client, file: string) {
super(client); super(client, file);
} }
public run(message: Message, vmLang: string, code: string): void { public async run(message: Message, vmLang: string, ...codeArray: string[]): Promise<void> {
const javascriptVM = new VM({ eval: false, timeout: 1000, wasm: false }); const code = codeArray.join(' ');
const javascriptVM = new NodeVM({ eval: false, timeout: 1000, wasm: false, console: 'off' });
switch(vmLang.toLowerCase()) { switch(vmLang.toLowerCase()) {
case 'js': case 'js':
case 'javascript': case 'javascript':
message.reply('Computing JavaScript code... Please wait!'); await message.reply('Computing JavaScript code... Please wait!');
javascriptVM.run(code);
try {
let returnVal: string;
if(!code.includes('module.exports = '))
returnVal = await javascriptVM.run(`module.exports = (() => {${code}})();`);
else
returnVal = await javascriptVM.run(`(() => {${code}})()`);
await message.reply(`\`\`\`js\n${returnVal}\`\`\``);
} catch(err) {
await message.reply(`An error has occurred while executing JavaScript code!\n\`\`\`\n${err.message}\n\`\`\``);
}
break;
case 'ts':
case 'typescript':
await message.reply('Computing TypeScript code... Please wait!');
try {
const jsCode = transpile(code);
let returnVal: string;
if(!jsCode.includes('module.exports = '))
returnVal = await javascriptVM.run(`module.exports = (() => {${jsCode}}()`);
else
returnVal = await javascriptVM.run(`(() => {${jsCode}}()`);
await message.reply(`\`\`\`js\n${returnVal}\`\`\``);
} catch(err) {
await message.reply(`An error has occurred while executing TypeScript code!\n\`\`\`\n${err.message}\n\`\`\``);
}
break; break;
default: default:
message.reply('Unknown Language given!'); await message.reply('Unknown Language given!');
} }
} }
} }

View file

@ -85,5 +85,6 @@
}, },
"include": [ "include": [
"src/**/*.ts", "src/**/*.ts",
"*.json"
] ]
} }