mrmBot-Matrix/commands/fun/xkcd.js

31 lines
829 B
JavaScript
Raw Normal View History

import fetch from "node-fetch";
import Command from "../../classes/command.js";
class XKCDCommand extends Command {
async run() {
const url = this.args.length > 0 && this.args[0].match(/^\d+$/) ? `http://xkcd.com/${this.args[0]}/info.0.json` : "http://xkcd.com/info.0.json";
try {
const request = await fetch(url);
const json = await request.json();
2021-11-10 04:09:10 +00:00
return {
embeds: [{
title: json.safe_title,
url: `https://xkcd.com/${json.num}`,
color: 16711680,
description: json.alt,
image: {
url: json.img
}
2021-11-10 04:09:10 +00:00
}]
};
} catch {
return "I couldn't get that XKCD!";
}
}
static description = "Gets an XKCD comic";
static arguments = ["{id}"];
static slashAllowed = false;
}
export default XKCDCommand;