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 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}` });