|
@@ -1,25 +1,50 @@
|
|
|
-import request from "request-promise-native";
|
|
|
import { Message } from "discord.js";
|
|
|
import { Command, CommandSet } from "src/model/command";
|
|
|
+import got from "got";
|
|
|
+import { logger } from "src/logging";
|
|
|
|
|
|
const rcgRe = /<input id="rcg_image".+value="([^"]+)".*\/>/i;
|
|
|
|
|
|
+interface XkcdResponse {
|
|
|
+ img: string;
|
|
|
+}
|
|
|
+
|
|
|
@CommandSet
|
|
|
export class Rcg {
|
|
|
+ async sendErrorMessage(msg: Message): Promise<void> {
|
|
|
+ const xkcdResult = await got.get<XkcdResponse>("https://xkcd.com/info.0.json", { responseType: "json" });
|
|
|
+ if(xkcdResult.statusCode != 200) {
|
|
|
+ await msg.channel.send(`${msg.author.toString()} Sorry, I couldn't get any comics :(.`);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ await msg.channel.send(`${msg.author.toString()} Sorry, I couldn't get a random comic! Here is today's XKCD instead:`, {
|
|
|
+ files: [ xkcdResult.body.img ]
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
@Command({
|
|
|
pattern: "random comic",
|
|
|
auth: false,
|
|
|
documentation: {description: "Generates a comic just for you!", example: "random comic"}
|
|
|
})
|
|
|
async randomComic(msg: Message): Promise<void> {
|
|
|
- const result = await request("http://explosm.net/rcg/view/?promo=false");
|
|
|
+ const result = await got.get("http://explosm.net/rcg/view/?promo=false");
|
|
|
|
|
|
- const regexResult = rcgRe.exec(result);
|
|
|
+ if (result.statusCode != 200) {
|
|
|
+ logger.error("Failed to get RCG. Got status: %s", result.statusCode);
|
|
|
+ await this.sendErrorMessage(msg);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const regexResult = rcgRe.exec(result.body);
|
|
|
|
|
|
- if(!regexResult)
|
|
|
+ if(!regexResult) {
|
|
|
+ logger.error("Could not find RCG from body. Got response body: %s", result.body);
|
|
|
+ await this.sendErrorMessage(msg);
|
|
|
return;
|
|
|
+ }
|
|
|
|
|
|
- msg.channel.send(`${msg.author.toString()} I find this very funny:`, {
|
|
|
+ await msg.channel.send(`${msg.author.toString()} I find this very funny:`, {
|
|
|
files: [ regexResult[1].trim() ]
|
|
|
});
|
|
|
}
|