plant that actually works

This commit is contained in:
Cynthia Foxwell 2022-05-26 22:58:31 -06:00
parent 24bc79da43
commit c9702dded7
1 changed files with 40 additions and 6 deletions

View File

@ -49,17 +49,51 @@ utsuholights.callback = async function (msg, line, hex, bri) {
};
hf.registerCommand(utsuholights);
const JPEG_HEADER = Buffer.from([
0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46,
]);
async function fetchPlant() {
const res = await fetch("https://nuclear.utsuho.rocks/plant/");
const boundary = res.headers
.get("Content-Type")
.split(";")[1]
.trim()
.replace("boundary=", "--");
let buffer = Buffer.alloc(0);
let searchForNextBoundary = false;
return await new Promise((resolve) => {
res.body.on("data", function (data) {
if (!searchForNextBoundary) {
if (data.toString().startsWith(boundary)) {
buffer = Buffer.concat([buffer, data]);
searchForNextBoundary = true;
}
} else if (searchForNextBoundary) {
if (data.toString().startsWith(boundary)) {
res.body.end();
const length = Number(
buffer.toString().match(/Content-Length:.*?(\d+)/)[1]
);
const headerOffset = buffer.indexOf(JPEG_HEADER);
const data = buffer.slice(headerOffset, headerOffset + length);
resolve(data);
} else {
buffer = Buffer.concat([buffer, data]);
}
}
});
});
}
const plant = new Command("plant");
plant.category = CATEGORY;
plant.helpText = "Plant cam";
plant.callback = async function () {
try {
const image = Buffer.from(
await fetch("https://nuclear.utsuho.rocks/plant/").then((res) =>
res.arrayBuffer()
)
);
return {file: {file: image, name: "plant.jpg"}};
return {file: {file: await fetchPlant(), name: "plant.jpg"}};
} catch (err) {
return "<:trollhollow:851301241417498704> where plant (Encountered an error getting plant cam)";
}