Add timeouts to many projectlounge requests, fixed command bug

This commit is contained in:
Essem 2021-08-18 08:07:55 -05:00
parent 4684db06e8
commit 516570efe4
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
6 changed files with 60 additions and 22 deletions

View file

@ -4,15 +4,26 @@ const Command = require("../../classes/command.js");
class AncientCommand extends Command {
async run() {
this.client.sendChannelTyping(this.message.channel.id);
const data = await fetch("https://projectlounge.pw/meme/", { redirect: "manual" });
return {
embed: {
color: 16711680,
image: {
url: data.headers.get("location")
const controller = new AbortController(); // eslint-disable-line no-undef
const timeout = setTimeout(() => {
controller.abort();
}, 15000);
try {
const data = await fetch("https://projectlounge.pw/meme/", { redirect: "manual", signal: controller.signal });
clearTimeout(timeout);
return {
embed: {
color: 16711680,
image: {
url: data.headers.get("location")
}
}
};
} catch (e) {
if (e.name === "AbortError") {
return "I couldn't get a meme in time. Maybe try again?";
}
};
}
}
static description = "Gets a random ancient meme";

View file

@ -4,16 +4,27 @@ const Command = require("../../classes/command.js");
class CatCommand extends Command {
async run() {
this.client.sendChannelTyping(this.message.channel.id);
const data = await fetch("https://projectlounge.pw/cta/", { redirect: "manual" });
return {
embed: {
color: 16711680,
description: "Cat images are sourced from a subset of the [dm4catbot](https://twitter.com/dm4catbot) database.",
image: {
url: data.headers.get("location")
const controller = new AbortController(); // eslint-disable-line no-undef
const timeout = setTimeout(() => {
controller.abort();
}, 15000);
try {
const data = await fetch("https://projectlounge.pw/cta/", { redirect: "manual", signal: controller.signal });
clearTimeout(timeout);
return {
embed: {
color: 16711680,
description: "Cat images are sourced from a subset of the [dm4catbot](https://twitter.com/dm4catbot) database.",
image: {
url: data.headers.get("location")
}
}
};
} catch (e) {
if (e.name === "AbortError") {
return "I couldn't get a cat image in time. Maybe try again?";
}
};
}
}
static description = "Gets a random cat picture";