Skip to content

Instantly share code, notes, and snippets.

@MCJack123
Last active February 28, 2024 08:39
Show Gist options
  • Save MCJack123/afcc4549ec1de27f845f603efaa99b36 to your computer and use it in GitHub Desktop.
Save MCJack123/afcc4549ec1de27f845f603efaa99b36 to your computer and use it in GitHub Desktop.
Discord bot that executes Lua code in CraftOS-PC, and returns a screenshot of the result
// Requires https://gist.github.com/MCJack123/4eb02b57de9fcb9c6e62b9b459ada8e6
// Extract env-clean.tar into env-clean next to this file
const { Client, MessageAttachment } = require('discord.js');
const exec = require('child_process').exec;
const fs = require("fs-extra");
const http = require("https");
const client = new Client();
const init_script = (script, author) => `
term.clear()
term.setCursorPos(1, 1)
local fn, err = load([======[${script}]======], "=[code: ${author}]", "t", _ENV)
if fn then
local ok, err = pcall(fn)
if not ok then printError(err) end
else printError(err) end
sleep(0.1)
os.shutdown()`;
client.on('ready', () => { console.log('I am ready!'); client.user.setPresence({ activity: { name: "" } }) });
client.on('message', message => {
if (message.content.startsWith('!code')) {
const nonce = Math.floor(Math.random() * 4294967296);
fs.copySync("env-clean", "env-" + nonce);
message.channel.startTyping();
if (message.attachments.size > 0) {
const file = message.attachments.first();
if (file.size > 100000) {
message.channel.stopTyping();
message.channel.send("The file could not be run. Files must be less than 100 kB.");
} else if (!file.name.endsWith(".lua") && !file.name.endsWith(".txt")) {
message.channel.stopTyping();
message.channel.send("The file could not be run. The file is not a Lua file.");
} else {
http.get(file.url, res => {
if (res.statusCode != 200) {
print(res.statusMessage);
message.channel.stopTyping();
message.channel.send("The file could not be run. An error occurred while downloading the file.");
} else {
const handle = fs.openSync(`env-${nonce}/computer/0/${file.name}`, "w");
res.on("data", d => fs.write(handle, d));
res.on("end", () => {
fs.closeSync(handle);
exec(`echo | (craftos -d env-${nonce} --raw --exec '${init_script(`shell.run([[${file.name.replace(/'/g, `'"'"'`)}]])`, message.author.username)}' 2>/dev/null & echo $! >&2) | tail -n 3 | head -n 1 | ./rawpngrender`, { timeout: 5000, encoding: 'buffer' }, (err, stdout, stderr) => {
message.channel.stopTyping();
const pid = parseInt(stderr.toString("utf8"));
if (pid !== NaN) exec("kill -KILL " + pid);
fs.remove("env-" + nonce);
if (err) {
console.log(err, stderr.toString("utf8"));
if (err.killed) message.channel.send("That command took too long to run. Please try a smaller command.");
else message.channel.send("An error occurred while executing the command.");
} else {
const attachment = new MessageAttachment(stdout, 'screenshot.png');
message.channel.send("", attachment);
}
});
});
}
});
}
} else {
const script = message.content.substr(6).replace(/^\s*```.*\n*/, "").replace(/\n*```\s*$/, "").replace(/'/g, `'"'"'`);
exec(`echo | (craftos -d env-${nonce} --raw --exec '${init_script(script, message.author.username)}' 2>/dev/null & echo $! >&2) | tail -n 3 | head -n 1 | ./rawpngrender`, { timeout: 5000, encoding: 'buffer' }, (err, stdout, stderr) => {
message.channel.stopTyping();
const pid = parseInt(stderr.toString("utf8"));
if (pid !== NaN) exec("kill -KILL " + pid);
fs.remove("env-" + nonce);
if (err) {
console.log(err, stderr.toString("utf8"), pid);
if (err.killed) message.channel.send("That command took too long to run. Please try a smaller command.");
else message.channel.send("An error occurred while executing the command.");
} else {
const attachment = new MessageAttachment(stdout, 'screenshot.png');
message.channel.send("", attachment);
}
});
}
}
});
client.login(your token here);
env-clean/������������������������������������������������������������������������������������������0000755�0001750�0001750�00000000000�14037757476�010554� 5����������������������������������������������������������������������������������������������������ustar �pi������������������������������pi���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������env-clean/computer/���������������������������������������������������������������������������������0000755�0001750�0001750�00000000000�14037757476�012412� 5����������������������������������������������������������������������������������������������������ustar �pi������������������������������pi���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������env-clean/computer/0/�������������������������������������������������������������������������������0000755�0001750�0001750�00000000000�14037757476�012551� 5����������������������������������������������������������������������������������������������������ustar �pi������������������������������pi���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������env-clean/computer/0/.settings����������������������������������������������������������������������0000755�0001750�0001750�00000000040�14037761012�014364� 0����������������������������������������������������������������������������������������������������ustar �pi������������������������������pi���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������{
[ "motd.enable" ] = false,
}������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������env-clean/config/�����������������������������������������������������������������������������������0000755�0001750�0001750�00000000000�14040177114�011774� 5����������������������������������������������������������������������������������������������������ustar �pi������������������������������pi���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������env-clean/config/0.json�����������������������������������������������������������������������������0000755�0001750�0001750�00000000177�14037761012�013040� 0����������������������������������������������������������������������������������������������������ustar �pi������������������������������pi���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������{
"base64" : true,
"computerHeight" : 0,
"computerWidth" : 0,
"isColor" : true,
"startFullscreen" : false
}�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������env-clean/config/global.json������������������������������������������������������������������������0000755�0001750�0001750�00000003101�14040177113�014124� 0����������������������������������������������������������������������������������������������������ustar �pi������������������������������pi���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������{
"abortTimeout" : 17000,
"checkUpdates" : false,
"cliControlKeyMode" : 0,
"clockSpeed" : 20,
"computerSpaceLimit" : 1000000,
"configReadOnly" : true,
"customCharScale" : 0,
"customFontPath" : "",
"customFontScale" : 0,
"debug_enable" : true,
"defaultHeight" : 19,
"defaultWidth" : 51,
"default_computer_settings" : "motd.enable=false",
"disable_lua51_features" : false,
"extendMargins" : false,
"http_blacklist": [
"$private"
],
"http_enable" : true,
"http_max_download" : 16777216,
"http_max_requests" : 16,
"http_max_upload" : 4194304,
"http_max_websocket_message" : 65536,
"http_max_websockets" : 4,
"http_proxy_port" : 0,
"http_proxy_server" : "",
"http_timeout" : 30000,
"http_websocket_enabled" : true,
"http_whitelist": [
"*"
],
"ignoreHotkeys" : false,
"initialComputer" : 0,
"keepOpenOnShutdown" : false,
"lastVersion" : "v2.5.5",
"logErrors" : false,
"maxNotesPerTick" : 8,
"maxOpenPorts" : 128,
"maxRecordingTime" : 15,
"maximumFilesOpen" : 128,
"monitorsUseMouseEvents" : false,
"mount_mode" : 0,
"mouse_move_throttle" : -1,
"pluginData" : {
},
"preferredHardwareDriver" : "",
"recordingFPS" : 10,
"romReadOnly" : true,
"serverMode" : true,
"showFPS" : false,
"showMountPrompt" : true,
"skipUpdate" : "",
"snapToSize" : true,
"snooperEnabled" : false,
"standardsMode" : false,
"useHardwareRenderer" : false,
"useVsync" : false,
"vanilla" : false
}
�����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment