Migrate to undici, try new method for getting image size/type/data

This commit is contained in:
Essem 2022-08-11 11:46:56 -05:00
parent 34ac7b3380
commit da6f95aad8
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
19 changed files with 113 additions and 140 deletions

View file

@ -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?";

View file

@ -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";

View file

@ -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?";

View file

@ -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";

View file

@ -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?";
}
}
}

View file

@ -1,4 +1,4 @@
import fetch from "node-fetch";
import { request } from "undici";
import Command from "../../classes/command.js";
class DonateCommand extends Command {
@ -10,7 +10,7 @@ class DonateCommand extends Command {
controller.abort();
}, 5000);
try {
const patrons = await fetch("https://projectlounge.pw/patrons", { signal: controller.signal }).then(data => data.json());
const patrons = await request("https://projectlounge.pw/patrons", { signal: controller.signal }).then(data => data.body.json());
clearTimeout(timeout);
prefix = "Thanks to the following patrons for their support:\n";
for (const patron of patrons) {

View file

@ -2,7 +2,7 @@ import paginator from "../../utils/pagination/pagination.js";
import { readFileSync } from "fs";
const { searx } = JSON.parse(readFileSync(new URL("../../config/servers.json", import.meta.url)));
import { random } from "../../utils/misc.js";
import fetch from "node-fetch";
import { request } from "undici";
import Command from "../../classes/command.js";
class ImageSearchCommand extends Command {
@ -12,7 +12,7 @@ class ImageSearchCommand extends Command {
if (!query || !query.trim()) return "You need to provide something to search for!";
await this.acknowledge();
const embeds = [];
const rawImages = await fetch(`${random(searx)}/search?format=json&safesearch=2&categories=images&q=!goi%20!ddi%20${encodeURIComponent(query)}`).then(res => res.json());
const rawImages = await request(`${random(searx)}/search?format=json&safesearch=2&categories=images&q=!goi%20!ddi%20${encodeURIComponent(query)}`).then(res => res.body.json());
if (rawImages.results.length === 0) return "I couldn't find any results!";
const images = rawImages.results.filter((val) => !val.img_src.startsWith("data:"));
for (const [i, value] of images.entries()) {

View file

@ -1,5 +1,5 @@
import urlCheck from "../../utils/urlcheck.js";
import fetch from "node-fetch";
import { request } from "undici";
import Command from "../../classes/command.js";
class LengthenCommand extends Command {
@ -8,8 +8,8 @@ class LengthenCommand extends Command {
const input = this.options.url ?? this.args.join(" ");
if (!input || !input.trim() || !urlCheck(input)) return "You need to provide a short URL to lengthen!";
if (urlCheck(input)) {
const url = await fetch(encodeURI(input), { redirect: "manual" });
return url.headers.get("location") || input;
const url = await request(encodeURI(input), { method: "HEAD" });
return url.headers.location || input;
} else {
return "That isn't a URL!";
}

View file

@ -1,5 +1,5 @@
import jsqr from "jsqr";
import fetch from "node-fetch";
import { request } from "undici";
import sharp from "sharp";
import { clean } from "../../utils/misc.js";
import Command from "../../classes/command.js";
@ -10,7 +10,7 @@ class QrReadCommand extends Command {
const image = await imageDetect(this.client, this.message, this.interaction, this.options);
if (image === undefined) return "You need to provide an image/GIF with a QR code to read!";
await this.acknowledge();
const data = Buffer.from(await (await fetch(image.path)).arrayBuffer());
const data = Buffer.from(await (await request(image.path)).body.arrayBuffer());
const rawData = await sharp(data).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
const qrBuffer = jsqr(rawData.data, rawData.info.width, rawData.info.height);
if (!qrBuffer) return "I couldn't find a QR code!";

View file

@ -1,4 +1,4 @@
import fetch from "node-fetch";
import { request } from "undici";
import { readFileSync } from "fs";
const { searx } = JSON.parse(readFileSync(new URL("../../config/servers.json", import.meta.url)));
import { random } from "../../utils/misc.js";
@ -11,7 +11,7 @@ class YouTubeCommand extends Command {
if (!query || !query.trim()) return "You need to provide something to search for!";
await this.acknowledge();
const messages = [];
const videos = await fetch(`${random(searx)}/search?format=json&safesearch=1&categories=videos&q=!youtube%20${encodeURIComponent(query)}`).then(res => res.json());
const videos = await request(`${random(searx)}/search?format=json&safesearch=1&categories=videos&q=!youtube%20${encodeURIComponent(query)}`).then(res => res.body.json());
if (videos.results.length === 0) return "I couldn't find any results!";
for (const [i, value] of videos.results.entries()) {
messages.push({ content: `Page ${i + 1} of ${videos.results.length}\n<:youtube:637020823005167626> **${value.title.replaceAll("*", "\\*")}**\nUploaded by **${value.author.replaceAll("*", "\\*")}**\n${value.url}` });

View file

@ -1,4 +1,4 @@
import fetch from "node-fetch";
import { request } from "undici";
import format from "format-duration";
import { nodes } from "../../utils/soundplayer.js";
import paginator from "../../utils/pagination/pagination.js";
@ -12,7 +12,7 @@ class QueueCommand extends MusicCommand {
if (!this.channel.permissionsOf(this.client.user.id).has("embedLinks")) return "I don't have the `Embed Links` permission!";
const player = this.connection;
const node = nodes.filter((val) => val.name === player.player.node.name)[0];
const tracks = await fetch(`http://${node.url}/decodetracks`, { method: "POST", body: JSON.stringify(this.queue), headers: { Authorization: node.auth, "Content-Type": "application/json" } }).then(res => res.json());
const tracks = await request(`http://${node.url}/decodetracks`, { method: "POST", body: JSON.stringify(this.queue), headers: { authorization: node.auth, "content-type": "application/json" } }).then(res => res.body.json());
const trackList = [];
const firstTrack = tracks.shift();
for (const [i, track] of tracks.entries()) {