From da6f95aad83b332d6bafcfb57900af8e9d4d063f Mon Sep 17 00:00:00 2001 From: Essem Date: Thu, 11 Aug 2022 11:46:56 -0500 Subject: [PATCH] Migrate to undici, try new method for getting image size/type/data --- commands/fun/ancient.js | 13 +++-------- commands/fun/bird.js | 26 +++++++++++++--------- commands/fun/cat.js | 13 +++-------- commands/fun/dog.js | 26 +++++++++++++--------- commands/fun/wikihow.js | 36 ++++++++++++++++-------------- commands/general/donate.js | 4 ++-- commands/general/image.js | 4 ++-- commands/general/lengthen.js | 6 ++--- commands/general/qrread.js | 4 ++-- commands/general/youtube.js | 4 ++-- commands/music/queue.js | 4 ++-- package.json | 2 +- pnpm-lock.yaml | 43 ++---------------------------------- utils/image-runner.js | 4 ++-- utils/image.js | 38 +++++++++++++++++++++---------- utils/imageConnection.js | 10 ++++----- utils/imagedetect.js | 8 +++---- utils/services/image.js | 4 ++-- utils/soundplayer.js | 4 ++-- 19 files changed, 113 insertions(+), 140 deletions(-) diff --git a/commands/fun/ancient.js b/commands/fun/ancient.js index 2ad0f4e..a80c632 100644 --- a/commands/fun/ancient.js +++ b/commands/fun/ancient.js @@ -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?"; diff --git a/commands/fun/bird.js b/commands/fun/bird.js index fb558d8..b22e067 100644 --- a/commands/fun/bird.js +++ b/commands/fun/bird.js @@ -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"; diff --git a/commands/fun/cat.js b/commands/fun/cat.js index 51d124a..7110f02 100644 --- a/commands/fun/cat.js +++ b/commands/fun/cat.js @@ -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?"; diff --git a/commands/fun/dog.js b/commands/fun/dog.js index 1b7158b..4b5fa44 100644 --- a/commands/fun/dog.js +++ b/commands/fun/dog.js @@ -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"; diff --git a/commands/fun/wikihow.js b/commands/fun/wikihow.js index 65f543f..c6722a6 100644 --- a/commands/fun/wikihow.js +++ b/commands/fun/wikihow.js @@ -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?"; + } } } diff --git a/commands/general/donate.js b/commands/general/donate.js index bf2e556..88a3a6f 100644 --- a/commands/general/donate.js +++ b/commands/general/donate.js @@ -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) { diff --git a/commands/general/image.js b/commands/general/image.js index b29f86e..7f92b5d 100644 --- a/commands/general/image.js +++ b/commands/general/image.js @@ -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()) { diff --git a/commands/general/lengthen.js b/commands/general/lengthen.js index 746b81e..735a2c5 100644 --- a/commands/general/lengthen.js +++ b/commands/general/lengthen.js @@ -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!"; } diff --git a/commands/general/qrread.js b/commands/general/qrread.js index 48c82c4..a9ca2bc 100644 --- a/commands/general/qrread.js +++ b/commands/general/qrread.js @@ -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!"; diff --git a/commands/general/youtube.js b/commands/general/youtube.js index cbe4472..50788b7 100644 --- a/commands/general/youtube.js +++ b/commands/general/youtube.js @@ -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}` }); diff --git a/commands/music/queue.js b/commands/music/queue.js index 148d361..ad1b389 100644 --- a/commands/music/queue.js +++ b/commands/music/queue.js @@ -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()) { diff --git a/package.json b/package.json index 148b55d..9be1a43 100644 --- a/package.json +++ b/package.json @@ -34,10 +34,10 @@ "jsqr": "^1.4.0", "node-addon-api": "^5.0.0", "node-emoji": "^1.11.0", - "node-fetch": "^3.2.10", "qrcode": "^1.5.1", "sharp": "^0.30.7", "shoukaku": "github:Deivu/shoukaku", + "undici": "^5.8.2", "winston": "^3.8.1", "winston-daily-rotate-file": "^4.7.1" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6c813b6..b195b65 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -21,11 +21,11 @@ specifiers: jsqr: ^1.4.0 node-addon-api: ^5.0.0 node-emoji: ^1.11.0 - node-fetch: ^3.2.10 pg: ^8.7.3 qrcode: ^1.5.1 sharp: ^0.30.7 shoukaku: github:Deivu/shoukaku + undici: ^5.8.2 uuid: ^8.3.2 winston: ^3.8.1 winston-daily-rotate-file: ^4.7.1 @@ -44,10 +44,10 @@ dependencies: jsqr: 1.4.0 node-addon-api: 5.0.0 node-emoji: 1.11.0 - node-fetch: 3.2.10 qrcode: 1.5.1 sharp: 0.30.7 shoukaku: github.com/Deivu/shoukaku/b6c724bf6e72a5dcf14beb6f51d35bc4b6a17647_bufferutil@4.0.6 + undici: 5.8.2 winston: 3.8.1 winston-daily-rotate-file: 4.7.1_winston@3.8.1 @@ -860,11 +860,6 @@ packages: which: 2.0.2 dev: true - /data-uri-to-buffer/4.0.0: - resolution: {integrity: sha512-Vr3mLBA8qWmcuschSLAOogKgQ/Jwxulv3RNE4FXnYWRGujzrRWQI4m12fQqRkwX06C0KanhLr4hK+GydchZsaA==} - engines: {node: '>= 12'} - dev: false - /debug/4.3.4: resolution: {integrity: sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==} engines: {node: '>=6.0'} @@ -1175,14 +1170,6 @@ packages: resolution: {integrity: sha512-OP2IUU6HeYKJi3i0z4A19kHMQoLVs4Hc+DPqqxI2h/DPZHTm/vjsfC6P0b4jCMy14XizLBqvndQ+UilD7707Jw==} dev: false - /fetch-blob/3.2.0: - resolution: {integrity: sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==} - engines: {node: ^12.20 || >= 14.13} - dependencies: - node-domexception: 1.0.0 - web-streams-polyfill: 3.2.1 - dev: false - /file-entry-cache/6.0.1: resolution: {integrity: sha512-7Gps/XWymbLk2QLYK4NzpMOrYjMhdIxXuIvy2QBsLE6ljuodKvdkWs/cpyJJ3CVIVpH0Oi1Hvg1ovbMzLdFBBg==} engines: {node: ^10.12.0 || >=12.0.0} @@ -1264,13 +1251,6 @@ packages: resolution: {integrity: sha512-ARqJ9qXm71pw3SGAY7bibf8lRLvltOXLjWjzzR3UrUjHu1zdeYpA/Z+u+ltdhrfRa440OjEsHNzdmuZViqqQWQ==} dev: false - /formdata-polyfill/4.0.10: - resolution: {integrity: sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==} - engines: {node: '>=12.20.0'} - dependencies: - fetch-blob: 3.2.0 - dev: false - /fs-constants/1.0.0: resolution: {integrity: sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==} dev: false @@ -1765,11 +1745,6 @@ packages: resolution: {integrity: sha512-CvkDw2OEnme7ybCykJpVcKH+uAOLV2qLqiyla128dN9TkEWfrYmxG6C2boDe5KcNQqZF3orkqzGgOMvZ/JNekA==} dev: false - /node-domexception/1.0.0: - resolution: {integrity: sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==} - engines: {node: '>=10.5.0'} - dev: false - /node-emoji/1.11.0: resolution: {integrity: sha512-wo2DpQkQp7Sjm2A0cq+sN7EHKO6Sl0ctXeBdFZrL9T9+UywORbufTcTZxom8YqpLQt/FqNMUkOpkZrJVYSKD3A==} dependencies: @@ -1788,15 +1763,6 @@ packages: whatwg-url: 5.0.0 dev: false - /node-fetch/3.2.10: - resolution: {integrity: sha512-MhuzNwdURnZ1Cp4XTazr69K0BTizsBroX7Zx3UgDSVcZYKF/6p0CBe4EUb/hLqmzVhl0UpYfgRljQ4yxE+iCxA==} - engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} - dependencies: - data-uri-to-buffer: 4.0.0 - fetch-blob: 3.2.0 - formdata-polyfill: 4.0.10 - dev: false - /node-gyp-build/4.5.0: resolution: {integrity: sha512-2iGbaQBV+ITgCz76ZEjmhUKAKVf7xfY1sRl4UiKQspfZMH2h06SyhNsnSVy50cwkFQDGLyif6m/6uFXHkOZ6rg==} hasBin: true @@ -2678,11 +2644,6 @@ packages: spdx-expression-parse: 3.0.1 dev: true - /web-streams-polyfill/3.2.1: - resolution: {integrity: sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==} - engines: {node: '>= 8'} - dev: false - /webidl-conversions/3.0.1: resolution: {integrity: sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==} dev: false diff --git a/utils/image-runner.js b/utils/image-runner.js index a5b4243..b29a011 100644 --- a/utils/image-runner.js +++ b/utils/image-runner.js @@ -1,6 +1,6 @@ import { createRequire } from "module"; import { isMainThread, parentPort, workerData } from "worker_threads"; -import fetch from "node-fetch"; +import { request } from "undici"; import path from "path"; import { fileURLToPath } from "url"; @@ -31,7 +31,7 @@ export default function run(object) { buffer: Buffer.alloc(0), fileExtension: "nogif" }); - promise = fetch(object.path).then(res => res.arrayBuffer()).then(buf => Buffer.from(buf)); + promise = request(object.path).then(res => res.body.arrayBuffer()).then(buf => Buffer.from(buf)); } // Convert from a MIME type (e.g. "image/png") to something ImageMagick understands (e.g. "png"). // Don't set `type` directly on the object we are passed as it will be read afterwards. diff --git a/utils/image.js b/utils/image.js index 32cdedf..d2465a6 100644 --- a/utils/image.js +++ b/utils/image.js @@ -1,4 +1,4 @@ -import fetch from "node-fetch"; +import { request } from "undici"; import fs from "fs"; import { fileTypeFromBuffer, fileTypeFromFile } from "file-type"; @@ -19,26 +19,40 @@ export async function getType(image, extraReturnTypes) { return undefined; } let type; - const controller = new AbortController(); // eslint-disable-line no-undef + const controller = new AbortController(); const timeout = setTimeout(() => { controller.abort(); - }, 25000); + }, 3000); try { - const imageRequest = await fetch(image, { - signal: controller.signal, headers: { - "Range": "bytes=0-1023" - } + const imageRequest = await request(image, { + signal: controller.signal, + method: "HEAD" }); clearTimeout(timeout); - const size = imageRequest.headers.has("Content-Range") ? imageRequest.headers.get("Content-Range").split("/")[1] : imageRequest.headers.get("Content-Length"); + const size = imageRequest.headers["content-range"] ? imageRequest.headers["content-range"].split("/")[1] : imageRequest.headers["content-length"]; if (parseInt(size) > 26214400 && extraReturnTypes) { // 25 MB type = "large"; return type; } - const imageBuffer = await imageRequest.arrayBuffer(); - const imageType = await fileTypeFromBuffer(imageBuffer); - if (imageType && formats.includes(imageType.mime)) { - type = imageType.mime; + const typeHeader = imageRequest.headers["content-type"]; + if (typeHeader) { + type = typeHeader; + } else { + const timeout = setTimeout(() => { + controller.abort(); + }, 3000); + const bufRequest = await request(image, { + signal: controller.signal, + headers: { + range: "bytes=0-1023" + } + }); + clearTimeout(timeout); + const imageBuffer = await bufRequest.body.arrayBuffer(); + const imageType = await fileTypeFromBuffer(imageBuffer); + if (imageType && formats.includes(imageType.mime)) { + type = imageType.mime; + } } } catch (error) { if (error.name === "AbortError") { diff --git a/utils/imageConnection.js b/utils/imageConnection.js index 00b1ef8..239c1c6 100644 --- a/utils/imageConnection.js +++ b/utils/imageConnection.js @@ -1,4 +1,4 @@ -import fetch from "node-fetch"; +import { request } from "undici"; import WebSocket from "ws"; import * as logger from "./logger.js"; import { setTimeout } from "timers/promises"; @@ -125,12 +125,12 @@ class ImageConnection { } async getOutput(jobid) { - const req = await fetch(`${this.httpurl}?id=${jobid}`, { + const req = await request(`${this.httpurl}?id=${jobid}`, { headers: { - "Authentication": this.auth || undefined + authentication: this.auth || undefined } }); - const contentType = req.headers.get("Content-Type"); + const contentType = req.headers["content-type"]; let type; switch (contentType) { case "image/gif": @@ -149,7 +149,7 @@ class ImageConnection { type = contentType; break; } - return { buffer: Buffer.from(await req.arrayBuffer()), type }; + return { buffer: Buffer.from(await req.body.arrayBuffer()), type }; } async do(op, id, data) { diff --git a/utils/imagedetect.js b/utils/imagedetect.js index c97581a..df50d23 100644 --- a/utils/imagedetect.js +++ b/utils/imagedetect.js @@ -1,4 +1,4 @@ -import fetch from "node-fetch"; +import { request } from "undici"; import { getType } from "./image.js"; const tenorURLs = [ @@ -52,8 +52,8 @@ const getImage = async (image, image2, video, extraReturnTypes, gifv = false, ty // Tenor doesn't let us access a raw GIF without going through their API, // so we use that if there's a key in the config if (process.env.TENOR !== "") { - const data = await fetch(`https://tenor.googleapis.com/v2/posts?ids=${image2.split("-").pop()}&media_filter=gif&limit=1&key=${process.env.TENOR}`); - if (data.status === 429) { + const data = await request(`https://tenor.googleapis.com/v2/posts?ids=${image2.split("-").pop()}&media_filter=gif&limit=1&key=${process.env.TENOR}`); + if (data.statusCode === 429) { if (extraReturnTypes) { payload.type = "tenorlimit"; return payload; @@ -61,7 +61,7 @@ const getImage = async (image, image2, video, extraReturnTypes, gifv = false, ty return; } } - const json = await data.json(); + const json = await data.body.json(); if (json.error) throw Error(json.error); payload.path = json.results[0].media_formats.gif.url; } diff --git a/utils/services/image.js b/utils/services/image.js index dabdb4e..876b7c5 100644 --- a/utils/services/image.js +++ b/utils/services/image.js @@ -6,7 +6,7 @@ import { fileURLToPath } from "url"; import { Worker } from "worker_threads"; import { createRequire } from "module"; import { createServer } from "http"; -import fetch from "node-fetch"; +import { request } from "undici"; import EventEmitter from "events"; // only requiring this to work around an issue regarding worker threads @@ -219,7 +219,7 @@ class ImageWorker extends BaseServiceWorker { } } else if (process.env.API_TYPE === "azure") { object.callback = `${process.env.AZURE_CALLBACK_URL}:${this.port}/callback`; - const response = await fetch(`${process.env.AZURE_URL}/api/orchestrators/ImageOrchestrator`, { method: "POST", body: JSON.stringify(object) }).then(r => r.json()); + const response = await request(`${process.env.AZURE_URL}/api/orchestrators/ImageOrchestrator`, { method: "POST", body: JSON.stringify(object) }).then(r => r.body.json()); const event = new EventEmitter(); this.jobs.set(response.id, event); return await this.waitForAzure(event); diff --git a/utils/soundplayer.js b/utils/soundplayer.js index ad29382..dd22613 100644 --- a/utils/soundplayer.js +++ b/utils/soundplayer.js @@ -1,5 +1,5 @@ import * as logger from "./logger.js"; -import fetch from "node-fetch"; +import { request } from "undici"; import fs from "fs"; import format from "format-duration"; import { Shoukaku, Connectors } from "shoukaku"; @@ -19,7 +19,7 @@ export async function checkStatus() { const newNodes = []; for (const node of nodes) { try { - const response = await fetch(`http://${node.url}/version`, { headers: { Authorization: node.auth } }).then(res => res.text()); + const response = await request(`http://${node.url}/version`, { headers: { authorization: node.auth } }).then(res => res.body.text()); if (response) newNodes.push(node); } catch { logger.error(`Failed to get status of Lavalink node ${node.host}.`);