mrmBot-Matrix/commands/fun/wikihow.js

35 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-03-15 14:09:09 +00:00
import { request } from "undici";
import Command from "../../classes/command.js";
class WikihowCommand extends Command {
async run() {
2023-03-17 00:23:01 +00:00
// await this.acknowledge();
2023-03-15 14:09:09 +00:00
const controller = new AbortController();
const timeout = setTimeout(() => {
controller.abort();
}, 15000);
try {
const req = await request("https://www.wikihow.com/api.php?action=query&generator=random&prop=imageinfo&format=json&iiprop=url&grnnamespace=6", { signal: controller.signal });
clearTimeout(timeout);
const json = await req.body.json();
const id = Object.keys(json.query.pages)[0];
const data = json.query.pages[id];
if (data.imageinfo) {
return json.query.pages[id].imageinfo[0].url;
} else {
return await this.run();
}
} catch (e) {
if (e.name === "AbortError") {
this.success = false;
return "I couldn't get a WikiHow image in time. Maybe try again?";
}
}
}
static description = "Gets a random WikiHow image";
static aliases = ["wiki"];
}
export default WikihowCommand;