HiddenPhox/src/modules/utsuholights.js

102 lines
2.9 KiB
JavaScript
Raw Normal View History

2021-08-12 19:32:06 +00:00
const Command = require("../lib/command.js");
const CATEGORY = "misc";
const fetch = require("node-fetch");
2022-05-22 19:21:09 +00:00
const {tinycolor} = require("@ctrl/tinycolor");
2021-08-12 19:32:06 +00:00
// taken from rot13.com
function rot(s, i) {
return s.replace(/[a-zA-Z]/g, function (c) {
return String.fromCharCode(
(c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + i) ? c : c - 26
);
});
}
// im making it "hard" to get cause im bored :^)
const LIGHTS_URL = "nUE0pUZ6Yl9hqJAfMJSlYaI0p3Ibol5lo2Aepl9fnJqbqN==";
const HEX_REGEX = /^#?([0-9a-fA-F]{1,2})([0-9a-fA-F]{1,2})([0-9a-fA-F]{1,2})$/;
let cachedLightsURL; // saving compute time :^)
const utsuholights = new Command("utsuholights");
utsuholights.category = CATEGORY;
utsuholights.helpText = "Utsuho Lights";
utsuholights.usage = "<hex> [brightness]";
utsuholights.callback = async function (msg, line, hex, bri) {
if (!hex) {
return "Hex color required.";
}
if (!HEX_REGEX.test(hex)) {
return "Could not determine hex color.";
}
2022-05-22 19:21:09 +00:00
const {r, g, b} = tinycolor(hex).toRgb();
2021-08-12 19:32:06 +00:00
if (!cachedLightsURL) {
cachedLightsURL = Buffer.from(rot(LIGHTS_URL, 13), "base64").toString(); // Wow! It's That Easy!
}
const response = await fetch(
`${cachedLightsURL}?r=${r}&g=${g}&b=${b}${bri ? `&bri=${bri}` : ""}`
);
if (response.status == 200) {
return {reaction: "\uD83D\uDC4C"};
} else {
return `:warning: An error occurred: \`${await response.text()}\``;
}
};
hf.registerCommand(utsuholights);
2022-05-27 03:44:06 +00:00
2022-05-27 04:58:31 +00:00
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]);
}
}
});
});
}
2022-05-27 03:44:06 +00:00
const plant = new Command("plant");
plant.category = CATEGORY;
plant.helpText = "Plant cam";
plant.callback = async function () {
try {
2022-05-27 04:58:31 +00:00
return {file: {file: await fetchPlant(), name: "plant.jpg"}};
2022-05-27 03:44:06 +00:00
} catch (err) {
return "<:trollhollow:851301241417498704> where plant (Encountered an error getting plant cam)";
}
};
hf.registerCommand(plant);