slimp/main.js

73 lines
2 KiB
JavaScript

const { Client, Events, GatewayIntentBits } = require("discord.js");
const { token, id } = require("./config.json");
const { spawn } = require("child_process");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
client.once(Events.ClientReady, c => {
console.log(`okay cool, ${c.user.tag}`);
console.log(
`Invite link: https://discord.com/oauth2/authorize?client_id=${id}&permissions=3072&scope=bot`
);
});
client.login(token);
client.on("messageCreate", message => {
if (
message.author.id == id ||
message.author.id == "1117299835855450163" ||
!message.mentions.has(id)
)
return;
try {
let [so, se] = [false, false];
function done() {
if (!so || !se) return;
rules.length > 0 &&
message.reply(mk_message(rules, "`stderr:`", "ansi"));
message.reply(
out.length > 0
? mk_message(out, "`stdout:`")
: "no output. weird."
);
}
let process = spawn("./target/release/slimp", [message.content], {
stdio: "pipe",
});
let rules = "";
process.stderr.on("data", data => (rules += data.toString()));
process.stderr.on("close", () => ((se = true), done()));
let out = "";
process.stdout.on("data", data => (out += data.toString()));
process.stdout.on("close", () => ((so = true), done()));
let timeout = 1 * 60 * 1000;
setTimeout(
() => (
(out += `took more than ${timeout}ms so killed. sorry`),
process.kill()
),
timeout
);
} catch (e) {
console.error(e);
message.reply(":(");
}
});
function mk_message(str, stream = "", lang = "lisp") {
return `${stream}\n` + "```" + lang + "\n" + str.slice(0, 1975) + "```";
}