misc: add generate command

This commit is contained in:
Cynthia Foxwell 2022-07-04 19:19:57 -06:00
parent dc03de59c4
commit a3a693a727

View file

@ -394,3 +394,39 @@ shodan.callback = async function (msg, line) {
}; };
}; };
hf.registerCommand(shodan); hf.registerCommand(shodan);
const GENERATE_HEADERS = {
Accept: "application/json",
"Content-Type": "application/json",
};
const generate = new Command("generate");
generate.category = CATEGORY;
generate.helpText = "Generate images from prompt via craiyon";
generate.callback = async function (msg, line) {
if (!line || line.length === 0) return "Arguments required.";
let request = await fetch("https://backend.craiyon.com/generate", {
headers: GENERATE_HEADERS,
body: JSON.stringify({prompt: line}),
});
while (request.status !== 200) {
request = await fetch("https://backend.craiyon.com/generate", {
headers: GENERATE_HEADERS,
body: JSON.stringify({prompt: line}),
});
}
const data = await request.json();
const images = data.images.map((img) => Buffer.from(img, "base64"));
for (const index in images) {
const img = images[index];
await msg.channel.createMessage(
`Response ${Number(index) + 1} for \`${safeString(line)}\`:`,
{file: img, name: `${index}.jpg`}
);
}
return null;
};
hf.registerCommand(generate);