mrmBot-Matrix/commands/fun/wikihow.js

34 lines
1 KiB
JavaScript
Raw Normal View History

import { request } from "undici";
import Command from "../../classes/command.js";
class WikihowCommand extends Command {
async run() {
2022-04-22 14:42:11 +00:00
await this.acknowledge();
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") {
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;