slimp/main.js

74 lines
2 KiB
JavaScript
Raw Permalink Normal View History

2023-06-12 11:40:38 -04:00
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 => {
2023-06-12 21:11:30 -04:00
if (
message.author.id == id ||
message.author.id == "1117299835855450163" ||
2023-06-13 10:36:22 -04:00
message.author.id == "1115433649010114670" ||
2023-06-12 21:11:30 -04:00
!message.mentions.has(id)
)
return;
2023-06-12 11:40:38 -04:00
try {
2023-06-12 21:11:30 -04:00
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."
);
}
2023-06-12 11:40:38 -04:00
let process = spawn("./target/release/slimp", [message.content], {
stdio: "pipe",
});
2023-06-12 21:11:30 -04:00
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
2023-06-12 11:40:38 -04:00
);
} catch (e) {
console.error(e);
message.reply(":(");
}
});
2023-06-12 21:11:30 -04:00
function mk_message(str, stream = "", lang = "lisp") {
return `${stream}\n` + "```" + lang + "\n" + str.slice(0, 1975) + "```";
2023-06-12 11:40:38 -04:00
}