This commit is contained in:
amy 2025-04-12 00:21:37 +03:30
parent 4904b1d305
commit 030594b51e
No known key found for this signature in database
3 changed files with 167 additions and 16 deletions

133
src/commands/eval.ts Normal file
View file

@ -0,0 +1,133 @@
import {
ApplicationCommandType,
ContextMenuCommandBuilder,
ContextMenuCommandInteraction, escapeCodeBlock,
InteractionContextType,
Message,
Snowflake,
User, UserResolvable
} from "discord.js";
import { parse as acornParse } from 'acorn'
import { ContextCommand } from "../command.ts";
import {ModuleDeclaration, Statement} from "acorn";
import {generate} from "astring";
import {inspect} from "node:util";
function transformLastInBlock<T extends Statement | ModuleDeclaration>(
array: Array<T | Statement>) {
if (array) {
array[array.length - 1] = transformStatement<T | Statement>(array[array.length - 1])
}
}
function transformStatement<T extends Statement | ModuleDeclaration>(
ast: T): T | Statement {
switch (ast.type) {
case 'ExpressionStatement':
return {
type: 'ExpressionStatement',
start: 0,
end: 0,
expression: {
type: 'AssignmentExpression',
operator: '=',
start: 0,
end: 0,
left: {
start: 0,
end: 0,
type: 'Identifier',
name: '__ret'
},
right: ast.expression
}
}
case 'BlockStatement':
transformLastInBlock(ast.body)
break
case 'ForStatement':
case "WhileStatement":
case 'ForOfStatement':
case 'ForInStatement':
case 'DoWhileStatement':
case 'WithStatement':
ast.body = transformStatement(ast.body)
break
case 'IfStatement':
ast.consequent = transformStatement(ast.consequent)
if (ast.alternate)
ast.alternate = transformStatement(ast.alternate)
break
}
return ast
}
export default class Mock extends ContextCommand<Message> {
targetType: ApplicationCommandType.Message = ApplicationCommandType.Message;
contextDefinition: ContextMenuCommandBuilder =
new ContextMenuCommandBuilder()
.setName('eval')
.setType(ApplicationCommandType.Message)
async run(interaction: ContextMenuCommandInteraction, target: Message): Promise<void> {
await interaction.deferReply();
const match = target.content.match(/```js\n(.*?)```/s)
if (!match) {
await interaction.followUp("no codeblock found")
return
}
const code = match[1];
let ast = acornParse(code, {
ecmaVersion: 2020,
allowReturnOutsideFunction: true,
allowAwaitOutsideFunction: true,
})
if (ast.body) {
ast.body.unshift({
type: 'VariableDeclaration',
kind: 'let',
end: 0, start: 0,
declarations: [{
end: 0, start: 0,
id: {
start: 0,
end: 0,
type: 'Identifier',
name: '__ret'
},
type: "VariableDeclarator"
}]
})
transformLastInBlock(ast.body)
ast.body.push({
type: 'ReturnStatement',
end: 0, start: 0,
argument: {
type: 'Identifier',
end: 0,
start: 0,
name: '__ret'
}
})
}
const mappedCode = generate(ast)
const bindings: { name: string, value: any }[] = [
{ name: 'add100', value: (x: number) => x + 100 },
{ name: 'client', value: interaction.client },
{ name: 'interaction', value: interaction },
{ name: 'getUser', value: (snowflake: UserResolvable) => interaction.client.users.fetch(snowflake) },
]
const func = new Function(...bindings.map(it => it.name), `"use strict";\nreturn (async () => {\n${mappedCode}\n})();`,)
await interaction.editReply("running...")
let result
result = await func(...bindings.map(it => it.value))
if (typeof result === "undefined") {
await interaction.editReply("result was undefined, did you forget to return?")
} else {
await interaction.editReply('```js\n' + escapeCodeBlock(inspect(result)) + "\n```")
}
}
}