HiddenPhox/src/modules/utsuholights.js

51 lines
1.5 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");
const colorcolor = require("colorcolor");
// 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.";
}
const [_, r, g, b] = colorcolor(hex, "rgb").match(/rgb\((\d+),(\d+),(\d+)\)/);
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);