Migrate to undici, try new method for getting image size/type/data
This commit is contained in:
parent
34ac7b3380
commit
da6f95aad8
19 changed files with 113 additions and 140 deletions
|
@ -1,4 +1,4 @@
|
|||
import fetch from "node-fetch";
|
||||
import { request } from "undici";
|
||||
import Command from "../../classes/command.js";
|
||||
|
||||
class AncientCommand extends Command {
|
||||
|
@ -9,16 +9,9 @@ class AncientCommand extends Command {
|
|||
controller.abort();
|
||||
}, 15000);
|
||||
try {
|
||||
const data = await fetch("https://projectlounge.pw/meme/", { redirect: "manual", signal: controller.signal });
|
||||
const data = await request("https://projectlounge.pw/meme/", { method: "HEAD", signal: controller.signal });
|
||||
clearTimeout(timeout);
|
||||
return {
|
||||
embeds: [{
|
||||
color: 16711680,
|
||||
image: {
|
||||
url: data.headers.get("location")
|
||||
}
|
||||
}]
|
||||
};
|
||||
return data.headers.location;
|
||||
} catch (e) {
|
||||
if (e.name === "AbortError") {
|
||||
return "I couldn't get a meme in time. Maybe try again?";
|
||||
|
|
|
@ -1,19 +1,23 @@
|
|||
import fetch from "node-fetch";
|
||||
import { request } from "undici";
|
||||
import Command from "../../classes/command.js";
|
||||
|
||||
class BirdCommand extends Command {
|
||||
async run() {
|
||||
await this.acknowledge();
|
||||
const imageData = await fetch("http://shibe.online/api/birds");
|
||||
const json = await imageData.json();
|
||||
return {
|
||||
embeds: [{
|
||||
color: 16711680,
|
||||
image: {
|
||||
url: json[0]
|
||||
}
|
||||
}]
|
||||
};
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => {
|
||||
controller.abort();
|
||||
}, 15000);
|
||||
try {
|
||||
const imageData = await request("http://shibe.online/api/birds", { signal: controller.signal });
|
||||
clearTimeout(timeout);
|
||||
const json = await imageData.body.json();
|
||||
return json[0];
|
||||
} catch (e) {
|
||||
if (e.name === "AbortError") {
|
||||
return "I couldn't get a bird image in time. Maybe try again?";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static description = "Gets a random bird picture";
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import fetch from "node-fetch";
|
||||
import { request } from "undici";
|
||||
import Command from "../../classes/command.js";
|
||||
|
||||
class CatCommand extends Command {
|
||||
|
@ -9,16 +9,9 @@ class CatCommand extends Command {
|
|||
controller.abort();
|
||||
}, 15000);
|
||||
try {
|
||||
const data = await fetch("https://projectlounge.pw/cta/", { redirect: "manual", signal: controller.signal });
|
||||
const data = await request("https://projectlounge.pw/cta/", { method: "HEAD", signal: controller.signal });
|
||||
clearTimeout(timeout);
|
||||
return {
|
||||
embeds: [{
|
||||
color: 16711680,
|
||||
image: {
|
||||
url: data.headers.get("location")
|
||||
}
|
||||
}]
|
||||
};
|
||||
return data.headers.location;
|
||||
} catch (e) {
|
||||
if (e.name === "AbortError") {
|
||||
return "I couldn't get a cat image in time. Maybe try again?";
|
||||
|
|
|
@ -1,19 +1,23 @@
|
|||
import fetch from "node-fetch";
|
||||
import { request } from "undici";
|
||||
import Command from "../../classes/command.js";
|
||||
|
||||
class DogCommand extends Command {
|
||||
async run() {
|
||||
await this.acknowledge();
|
||||
const imageData = await fetch("https://dog.ceo/api/breeds/image/random");
|
||||
const json = await imageData.json();
|
||||
return {
|
||||
embeds: [{
|
||||
color: 16711680,
|
||||
image: {
|
||||
url: json.message
|
||||
}
|
||||
}]
|
||||
};
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => {
|
||||
controller.abort();
|
||||
}, 15000);
|
||||
try {
|
||||
const imageData = await request("https://dog.ceo/api/breeds/image/random", { signal: controller.signal });
|
||||
clearTimeout(timeout);
|
||||
const json = await imageData.body.json();
|
||||
return json.message;
|
||||
} catch (e) {
|
||||
if (e.name === "AbortError") {
|
||||
return "I couldn't get a dog image in time. Maybe try again?";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static description = "Gets a random dog picture";
|
||||
|
|
|
@ -1,24 +1,28 @@
|
|||
import fetch from "node-fetch";
|
||||
import { request } from "undici";
|
||||
import Command from "../../classes/command.js";
|
||||
|
||||
class WikihowCommand extends Command {
|
||||
async run() {
|
||||
await this.acknowledge();
|
||||
const request = await fetch("https://www.wikihow.com/api.php?action=query&generator=random&prop=imageinfo&format=json&iiprop=url&grnnamespace=6");
|
||||
const json = await request.json();
|
||||
const id = Object.keys(json.query.pages)[0];
|
||||
const data = json.query.pages[id];
|
||||
if (data.imageinfo) {
|
||||
return {
|
||||
embeds: [{
|
||||
color: 16711680,
|
||||
image: {
|
||||
url: json.query.pages[id].imageinfo[0].url
|
||||
}
|
||||
}]
|
||||
};
|
||||
} else {
|
||||
return await this.run();
|
||||
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?";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue