misc.generate: add time/retries

This commit is contained in:
Cynthia Foxwell 2022-07-04 19:45:48 -06:00
parent 4df473339d
commit 8ccd08d56c

View file

@ -2,7 +2,12 @@ const Command = require("../lib/command.js");
const CATEGORY = "misc"; const CATEGORY = "misc";
const fetch = require("node-fetch"); const fetch = require("node-fetch");
const {hastebin, safeString, parseHtmlEntities} = require("../lib/utils.js"); const {
hastebin,
safeString,
parseHtmlEntities,
formatTime,
} = require("../lib/utils.js");
const GoogleImages = require("google-images"); const GoogleImages = require("google-images");
const imagesClient = new GoogleImages(hf.apikeys.gimg, hf.apikeys.google); const imagesClient = new GoogleImages(hf.apikeys.gimg, hf.apikeys.google);
@ -405,6 +410,9 @@ generate.helpText = "Generate images from prompt via craiyon";
generate.callback = async function (msg, line) { generate.callback = async function (msg, line) {
if (!line || line.length === 0) return "Arguments required."; if (!line || line.length === 0) return "Arguments required.";
const start = Date.now();
let retries = 0;
let request = await fetch("https://backend.craiyon.com/generate", { let request = await fetch("https://backend.craiyon.com/generate", {
method: "POST", method: "POST",
headers: GENERATE_HEADERS, headers: GENERATE_HEADERS,
@ -416,6 +424,7 @@ generate.callback = async function (msg, line) {
headers: GENERATE_HEADERS, headers: GENERATE_HEADERS,
body: JSON.stringify({prompt: line}), body: JSON.stringify({prompt: line}),
}); });
retries++;
} }
const data = await request.json(); const data = await request.json();
@ -426,6 +435,9 @@ generate.callback = async function (msg, line) {
const title = `Responses for "${safeString(line)}"`; const title = `Responses for "${safeString(line)}"`;
const out = { const out = {
content: `Generated in ${formatTime(Date.now() - start)}${
retries > 0 ? " with " + retries + " retries" : ""
}`,
embeds: [], embeds: [],
file: images, file: images,
}; };