2023-04-25 20:06:08 +00:00
|
|
|
// @ts-check
|
|
|
|
|
2023-03-05 16:05:35 +00:00
|
|
|
const repl = require("repl")
|
|
|
|
const util = require("util")
|
2024-09-12 05:11:35 +00:00
|
|
|
const {addbot} = require("../addbot")
|
2023-03-05 16:05:35 +00:00
|
|
|
|
|
|
|
const passthrough = require("./passthrough")
|
2023-10-02 08:48:06 +00:00
|
|
|
const {discord, config, sync, db} = passthrough
|
2023-05-04 20:25:00 +00:00
|
|
|
|
2024-09-12 05:11:35 +00:00
|
|
|
const data = sync.require("../test/data")
|
2023-05-08 11:37:51 +00:00
|
|
|
const createSpace = sync.require("./d2m/actions/create-space")
|
|
|
|
const createRoom = sync.require("./d2m/actions/create-room")
|
2023-05-08 12:58:46 +00:00
|
|
|
const registerUser = sync.require("./d2m/actions/register-user")
|
2023-05-08 11:37:51 +00:00
|
|
|
const mreq = sync.require("./matrix/mreq")
|
|
|
|
const api = sync.require("./matrix/api")
|
2023-08-24 23:44:58 +00:00
|
|
|
const file = sync.require("./matrix/file")
|
2023-07-03 12:39:42 +00:00
|
|
|
const sendEvent = sync.require("./m2d/actions/send-event")
|
2023-08-19 10:54:23 +00:00
|
|
|
const eventDispatcher = sync.require("./d2m/event-dispatcher")
|
2024-01-17 11:30:55 +00:00
|
|
|
const updatePins = sync.require("./d2m/actions/update-pins")
|
2024-01-19 12:01:34 +00:00
|
|
|
const speedbump = sync.require("./d2m/actions/speedbump")
|
2023-08-23 04:16:36 +00:00
|
|
|
const ks = sync.require("./matrix/kstate")
|
2023-05-04 20:25:00 +00:00
|
|
|
const guildID = "112760669178241024"
|
2023-03-05 16:05:35 +00:00
|
|
|
|
|
|
|
const extraContext = {}
|
|
|
|
|
2023-10-12 09:17:15 +00:00
|
|
|
if (process.stdin.isTTY) {
|
|
|
|
setImmediate(() => { // assign after since old extraContext data will get removed
|
|
|
|
if (!passthrough.repl) {
|
|
|
|
const cli = repl.start({ prompt: "", eval: customEval, writer: s => s })
|
|
|
|
Object.assign(cli.context, extraContext, passthrough)
|
|
|
|
passthrough.repl = cli
|
|
|
|
} else {
|
|
|
|
Object.assign(passthrough.repl.context, extraContext)
|
|
|
|
}
|
|
|
|
sync.addTemporaryListener(passthrough.repl, "exit", () => process.exit())
|
|
|
|
})
|
|
|
|
}
|
2023-03-05 16:05:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} input
|
|
|
|
* @param {import("vm").Context} _context
|
|
|
|
* @param {string} _filename
|
|
|
|
* @param {(err: Error | null, result: unknown) => unknown} callback
|
|
|
|
*/
|
|
|
|
async function customEval(input, _context, _filename, callback) {
|
|
|
|
let depth = 0
|
|
|
|
if (input === "exit\n") return process.exit()
|
2023-10-02 08:48:06 +00:00
|
|
|
if (input === "addbot\n") return callback(null, addbot())
|
2023-03-05 16:05:35 +00:00
|
|
|
if (input.startsWith(":")) {
|
|
|
|
const depthOverwrite = input.split(" ")[0]
|
|
|
|
depth = +depthOverwrite.slice(1)
|
|
|
|
input = input.slice(depthOverwrite.length + 1)
|
|
|
|
}
|
|
|
|
let result
|
|
|
|
try {
|
|
|
|
result = await eval(input)
|
|
|
|
const output = util.inspect(result, false, depth, true)
|
|
|
|
return callback(null, output)
|
|
|
|
} catch (e) {
|
2023-05-05 13:25:15 +00:00
|
|
|
return callback(null, util.inspect(e, false, 100, true))
|
2023-03-05 16:05:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sync.events.once(__filename, () => {
|
|
|
|
for (const key in extraContext) {
|
|
|
|
delete passthrough.repl.context[key]
|
|
|
|
}
|
|
|
|
})
|